@@ -50,12 +50,6 @@ public ScriptHostManager(ScriptHostConfiguration config, ScriptSettingsManager s
5050 _scriptHostFactory = scriptHostFactory ;
5151 }
5252
53- /// <summary>
54- /// Returns true if the <see cref="ScriptHost"/> is up and running and ready to
55- /// process requests.
56- /// </summary>
57- public virtual bool IsRunning { get ; private set ; }
58-
5953 public virtual ScriptHost Instance
6054 {
6155 get
@@ -65,10 +59,28 @@ public virtual ScriptHost Instance
6559 }
6660
6761 /// <summary>
68- /// Gets the last host <see cref="Exception"/> that has occurred.
62+ /// Gets or sets the current state of the host.
63+ /// </summary>
64+ public virtual ScriptHostState State { get ; private set ; }
65+
66+ /// <summary>
67+ /// Gets or sets the last host <see cref="Exception"/> that has occurred.
6968 /// </summary>
7069 public virtual Exception LastError { get ; private set ; }
7170
71+ /// <summary>
72+ /// Returns a value indicating whether the host can accept function invoke requests.
73+ /// </summary>
74+ /// <remarks>
75+ /// The host doesn't have to be fully started for it to allow direct function invocations
76+ /// to be processed.
77+ /// </remarks>
78+ /// <returns>True if the host can accept invoke requests, false otherwise.</returns>
79+ public bool CanInvoke ( )
80+ {
81+ return State == ScriptHostState . Created || State == ScriptHostState . Running ;
82+ }
83+
7284 public void RunAndBlock ( CancellationToken cancellationToken = default ( CancellationToken ) )
7385 {
7486 // Start the host and restart it if requested. Host Restarts will happen when
@@ -78,7 +90,12 @@ public virtual ScriptHost Instance
7890 ScriptHost newInstance = null ;
7991 try
8092 {
81- IsRunning = false ;
93+ // if we were in an error state retain that,
94+ // otherwise move to default
95+ if ( State != ScriptHostState . Error )
96+ {
97+ State = ScriptHostState . Default ;
98+ }
8299
83100 // Create a new host config, but keep the host id from existing one
84101 _config . HostConfig = new JobHostConfiguration
@@ -87,9 +104,16 @@ public virtual ScriptHost Instance
87104 } ;
88105 OnInitializeConfig ( _config ) ;
89106 newInstance = _scriptHostFactory . Create ( _settingsManager , _config ) ;
90-
91107 _traceWriter = newInstance . TraceWriter ;
92108
109+ _currentInstance = newInstance ;
110+ lock ( _liveInstances )
111+ {
112+ _liveInstances . Add ( newInstance ) ;
113+ }
114+
115+ OnHostCreated ( ) ;
116+
93117 if ( _traceWriter != null )
94118 {
95119 string message = string . Format ( "Starting Host (HostId={0}, Version={1}, ProcessId={2}, Debug={3})" ,
@@ -101,15 +125,11 @@ public virtual ScriptHost Instance
101125 // log any function initialization errors
102126 LogErrors ( newInstance ) ;
103127
104- lock ( _liveInstances )
105- {
106- _liveInstances . Add ( newInstance ) ;
107- }
108- _currentInstance = newInstance ;
109128 OnHostStarted ( ) ;
110129
111- // only after ALL initialization is complete do we set this flag
112- IsRunning = true ;
130+ // only after ALL initialization is complete do we set the
131+ // state to Running
132+ State = ScriptHostState . Running ;
113133 LastError = null ;
114134
115135 // Wait for a restart signal. This event will automatically reset.
@@ -131,7 +151,7 @@ public virtual ScriptHost Instance
131151 }
132152 catch ( Exception ex )
133153 {
134- IsRunning = false ;
154+ State = ScriptHostState . Error ;
135155 LastError = ex ;
136156
137157 // We need to keep the host running, so we catch and log any errors
@@ -221,15 +241,10 @@ public async Task StopAsync()
221241 _stopEvent . Set ( ) ;
222242 ScriptHost [ ] instances = GetLiveInstancesAndClear ( ) ;
223243
224- Task [ ] tasksStop = Array . ConvertAll ( instances , instance => instance . StopAsync ( ) ) ;
244+ Task [ ] tasksStop = Array . ConvertAll ( instances , p => StopAndDisposeAsync ( p ) ) ;
225245 await Task . WhenAll ( tasksStop ) ;
226246
227- foreach ( var instance in instances )
228- {
229- instance . Dispose ( ) ;
230- }
231-
232- IsRunning = false ;
247+ State = ScriptHostState . Default ;
233248 }
234249 catch
235250 {
@@ -242,6 +257,22 @@ public void Stop()
242257 StopAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
243258 }
244259
260+ private static async Task StopAndDisposeAsync ( ScriptHost instance )
261+ {
262+ try
263+ {
264+ await instance . StopAsync ( ) ;
265+ }
266+ catch
267+ {
268+ // best effort
269+ }
270+ finally
271+ {
272+ instance . Dispose ( ) ;
273+ }
274+ }
275+
245276 private ScriptHost [ ] GetLiveInstancesAndClear ( )
246277 {
247278 ScriptHost [ ] instances ;
@@ -258,6 +289,11 @@ protected virtual void OnInitializeConfig(ScriptHostConfiguration config)
258289 {
259290 }
260291
292+ protected virtual void OnHostCreated ( )
293+ {
294+ State = ScriptHostState . Created ;
295+ }
296+
261297 protected virtual void OnHostStarted ( )
262298 {
263299 var metricsLogger = _config . HostConfig . GetService < IMetricsLogger > ( ) ;
0 commit comments