@@ -145,7 +145,7 @@ public virtual ScriptHost Instance
145
145
/// <returns>True if the host can accept invoke requests, false otherwise.</returns>
146
146
public bool CanInvoke ( )
147
147
{
148
- return State == ScriptHostState . Created || State == ScriptHostState . Running ;
148
+ return State == ScriptHostState . Initialized || State == ScriptHostState . Running ;
149
149
}
150
150
151
151
public void RunAndBlock ( CancellationToken cancellationToken = default ( CancellationToken ) )
@@ -164,7 +164,7 @@ public bool CanInvoke()
164
164
State = ScriptHostState . Default ;
165
165
}
166
166
167
- OnHostStarting ( ) ;
167
+ OnCreatingHost ( ) ;
168
168
169
169
// Create a new host config, but keep the host id from existing one
170
170
_config . HostConfig = new JobHostConfiguration ( _settingsManager . Configuration )
@@ -173,6 +173,8 @@ public bool CanInvoke()
173
173
} ;
174
174
OnInitializeConfig ( _config ) ;
175
175
newInstance = _scriptHostFactory . Create ( _environment , EventManager , _settingsManager , _config , _loggerFactoryBuilder ) ;
176
+ newInstance . HostInitialized += OnHostInitialized ;
177
+ newInstance . HostStarted += OnHostStarted ;
176
178
_traceWriter = newInstance . TraceWriter ;
177
179
_logger = newInstance . Logger ;
178
180
@@ -183,8 +185,6 @@ public bool CanInvoke()
183
185
_hostStartCount ++ ;
184
186
}
185
187
186
- OnHostCreated ( ) ;
187
-
188
188
string extensionVersion = _settingsManager . GetSetting ( EnvironmentSettingNames . FunctionsExtensionVersion ) ;
189
189
string hostId = newInstance . ScriptConfig . HostConfig . HostId ;
190
190
string message = $ "Starting Host (HostId={ hostId } , Version={ ScriptHost . Version } , ProcessId={ Process . GetCurrentProcess ( ) . Id } , Debug={ newInstance . InDebugMode } , ConsecutiveErrors={ consecutiveErrorCount } , StartupCount={ _hostStartCount } , FunctionsExtensionVersion={ extensionVersion } )";
@@ -196,11 +196,6 @@ public bool CanInvoke()
196
196
// log any function initialization errors
197
197
LogErrors ( newInstance ) ;
198
198
199
- OnHostStarted ( ) ;
200
-
201
- // only after ALL initialization is complete do we set the
202
- // state to Running
203
- State = ScriptHostState . Running ;
204
199
LastError = null ;
205
200
consecutiveErrorCount = 0 ;
206
201
_restartDelayTokenSource = null ;
@@ -262,6 +257,16 @@ public bool CanInvoke()
262
257
while ( ! _stopped && ! cancellationToken . IsCancellationRequested ) ;
263
258
}
264
259
260
+ private void OnHostInitialized ( object sender , EventArgs e )
261
+ {
262
+ OnHostInitialized ( ) ;
263
+ }
264
+
265
+ private void OnHostStarted ( object sender , EventArgs e )
266
+ {
267
+ OnHostStarted ( ) ;
268
+ }
269
+
265
270
private Task CreateRestartBackoffDelay ( int consecutiveErrorCount )
266
271
{
267
272
_restartDelayTokenSource = new CancellationTokenSource ( ) ;
@@ -296,6 +301,9 @@ private static void LogErrors(ScriptHost host)
296
301
/// <param name="forceStop">Forces the call to stop and dispose of the instance, even if it isn't present in the live instances collection.</param>
297
302
private async Task Orphan ( ScriptHost instance , bool forceStop = false )
298
303
{
304
+ instance . HostInitialized -= OnHostInitialized ;
305
+ instance . HostStarted -= OnHostStarted ;
306
+
299
307
lock ( _liveInstances )
300
308
{
301
309
bool removed = _liveInstances . Remove ( instance ) ;
@@ -397,20 +405,28 @@ protected virtual void OnInitializeConfig(ScriptHostConfiguration config)
397
405
}
398
406
}
399
407
400
- protected virtual void OnHostCreated ( )
408
+ protected virtual void OnHostInitialized ( )
401
409
{
402
- State = ScriptHostState . Created ;
410
+ State = ScriptHostState . Initialized ;
403
411
}
404
412
405
- protected virtual void OnHostStarting ( )
413
+ /// <summary>
414
+ /// Called after the host has been started.
415
+ /// </summary>
416
+ protected virtual void OnHostStarted ( )
406
417
{
407
- IsHostHealthy ( throwWhenUnhealthy : true ) ;
418
+ var metricsLogger = _config . HostConfig . GetService < IMetricsLogger > ( ) ;
419
+ metricsLogger . LogEvent ( new HostStarted ( Instance ) ) ;
420
+
421
+ State = ScriptHostState . Running ;
408
422
}
409
423
410
- protected virtual void OnHostStarted ( )
424
+ /// <summary>
425
+ /// Called immediately before we begin creating the host.
426
+ /// </summary>
427
+ protected virtual void OnCreatingHost ( )
411
428
{
412
- var metricsLogger = _config . HostConfig . GetService < IMetricsLogger > ( ) ;
413
- metricsLogger . LogEvent ( new HostStarted ( Instance ) ) ;
429
+ IsHostHealthy ( throwWhenUnhealthy : true ) ;
414
430
}
415
431
416
432
public void Dispose ( )
0 commit comments