@@ -50,12 +50,6 @@ public ScriptHostManager(ScriptHostConfiguration config, ScriptSettingsManager s
50
50
_scriptHostFactory = scriptHostFactory ;
51
51
}
52
52
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
-
59
53
public virtual ScriptHost Instance
60
54
{
61
55
get
@@ -65,10 +59,28 @@ public virtual ScriptHost Instance
65
59
}
66
60
67
61
/// <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.
69
68
/// </summary>
70
69
public virtual Exception LastError { get ; private set ; }
71
70
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
+
72
84
public void RunAndBlock ( CancellationToken cancellationToken = default ( CancellationToken ) )
73
85
{
74
86
// Start the host and restart it if requested. Host Restarts will happen when
@@ -78,7 +90,12 @@ public virtual ScriptHost Instance
78
90
ScriptHost newInstance = null ;
79
91
try
80
92
{
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
+ }
82
99
83
100
// Create a new host config, but keep the host id from existing one
84
101
_config . HostConfig = new JobHostConfiguration
@@ -87,9 +104,16 @@ public virtual ScriptHost Instance
87
104
} ;
88
105
OnInitializeConfig ( _config ) ;
89
106
newInstance = _scriptHostFactory . Create ( _settingsManager , _config ) ;
90
-
91
107
_traceWriter = newInstance . TraceWriter ;
92
108
109
+ _currentInstance = newInstance ;
110
+ lock ( _liveInstances )
111
+ {
112
+ _liveInstances . Add ( newInstance ) ;
113
+ }
114
+
115
+ OnHostCreated ( ) ;
116
+
93
117
if ( _traceWriter != null )
94
118
{
95
119
string message = string . Format ( "Starting Host (HostId={0}, Version={1}, ProcessId={2}, Debug={3})" ,
@@ -101,15 +125,11 @@ public virtual ScriptHost Instance
101
125
// log any function initialization errors
102
126
LogErrors ( newInstance ) ;
103
127
104
- lock ( _liveInstances )
105
- {
106
- _liveInstances . Add ( newInstance ) ;
107
- }
108
- _currentInstance = newInstance ;
109
128
OnHostStarted ( ) ;
110
129
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 ;
113
133
LastError = null ;
114
134
115
135
// Wait for a restart signal. This event will automatically reset.
@@ -131,7 +151,7 @@ public virtual ScriptHost Instance
131
151
}
132
152
catch ( Exception ex )
133
153
{
134
- IsRunning = false ;
154
+ State = ScriptHostState . Error ;
135
155
LastError = ex ;
136
156
137
157
// We need to keep the host running, so we catch and log any errors
@@ -221,15 +241,10 @@ public async Task StopAsync()
221
241
_stopEvent . Set ( ) ;
222
242
ScriptHost [ ] instances = GetLiveInstancesAndClear ( ) ;
223
243
224
- Task [ ] tasksStop = Array . ConvertAll ( instances , instance => instance . StopAsync ( ) ) ;
244
+ Task [ ] tasksStop = Array . ConvertAll ( instances , p => StopAndDisposeAsync ( p ) ) ;
225
245
await Task . WhenAll ( tasksStop ) ;
226
246
227
- foreach ( var instance in instances )
228
- {
229
- instance . Dispose ( ) ;
230
- }
231
-
232
- IsRunning = false ;
247
+ State = ScriptHostState . Default ;
233
248
}
234
249
catch
235
250
{
@@ -242,6 +257,22 @@ public void Stop()
242
257
StopAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
243
258
}
244
259
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
+
245
276
private ScriptHost [ ] GetLiveInstancesAndClear ( )
246
277
{
247
278
ScriptHost [ ] instances ;
@@ -258,6 +289,11 @@ protected virtual void OnInitializeConfig(ScriptHostConfiguration config)
258
289
{
259
290
}
260
291
292
+ protected virtual void OnHostCreated ( )
293
+ {
294
+ State = ScriptHostState . Created ;
295
+ }
296
+
261
297
protected virtual void OnHostStarted ( )
262
298
{
263
299
var metricsLogger = _config . HostConfig . GetService < IMetricsLogger > ( ) ;
0 commit comments