Skip to content

Commit 8cd9b39

Browse files
committed
Host state tracking improvements
1 parent b4b7afd commit 8cd9b39

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<PackageReference Include="Microsoft.Azure.AppService.Proxy.Client.Contract" Version="0.3.1.1">
3838
<NoWarn>NU1701</NoWarn>
3939
</PackageReference>
40-
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11077" />
40+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11084" />
4141
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta3" />
4242
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.0-beta3">
4343
<NoWarn>NU1701</NoWarn>

src/WebJobs.Script.WebHost/WebScriptHostManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected override void OnInitializeConfig(ScriptHostConfiguration config)
205205
hostConfig.DashboardConnectionString = null;
206206
}
207207

208-
protected override void OnHostCreated()
208+
protected override void OnHostInitialized()
209209
{
210210
if (InStandbyMode)
211211
{
@@ -214,7 +214,7 @@ protected override void OnHostCreated()
214214

215215
InitializeHttp();
216216

217-
base.OnHostCreated();
217+
base.OnHostInitialized();
218218
}
219219

220220
protected override void OnHostStarted()

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ protected internal ScriptHost(IScriptHostEnvironment environment,
108108
_proxyClient = proxyClient;
109109
}
110110

111+
public event EventHandler HostInitialized;
112+
113+
public event EventHandler HostStarted;
114+
111115
public event EventHandler IsPrimaryChanged;
112116

113117
public string InstanceId
@@ -1897,6 +1901,20 @@ internal static string GetAssemblyFileVersion(Assembly assembly)
18971901
return fileVersionAttr?.Version ?? "Unknown";
18981902
}
18991903

1904+
protected override void OnHostInitialized()
1905+
{
1906+
HostInitialized?.Invoke(this, EventArgs.Empty);
1907+
1908+
base.OnHostInitialized();
1909+
}
1910+
1911+
protected override void OnHostStarted()
1912+
{
1913+
HostStarted?.Invoke(this, EventArgs.Empty);
1914+
1915+
base.OnHostStarted();
1916+
}
1917+
19001918
protected override void Dispose(bool disposing)
19011919
{
19021920
if (disposing)

src/WebJobs.Script/Host/ScriptHostManager.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public virtual ScriptHost Instance
145145
/// <returns>True if the host can accept invoke requests, false otherwise.</returns>
146146
public bool CanInvoke()
147147
{
148-
return State == ScriptHostState.Created || State == ScriptHostState.Running;
148+
return State == ScriptHostState.Initialized || State == ScriptHostState.Running;
149149
}
150150

151151
public void RunAndBlock(CancellationToken cancellationToken = default(CancellationToken))
@@ -164,7 +164,7 @@ public bool CanInvoke()
164164
State = ScriptHostState.Default;
165165
}
166166

167-
OnHostStarting();
167+
OnCreatingHost();
168168

169169
// Create a new host config, but keep the host id from existing one
170170
_config.HostConfig = new JobHostConfiguration(_settingsManager.Configuration)
@@ -173,6 +173,8 @@ public bool CanInvoke()
173173
};
174174
OnInitializeConfig(_config);
175175
newInstance = _scriptHostFactory.Create(_environment, EventManager, _settingsManager, _config, _loggerFactoryBuilder);
176+
newInstance.HostInitialized += OnHostInitialized;
177+
newInstance.HostStarted += OnHostStarted;
176178
_traceWriter = newInstance.TraceWriter;
177179
_logger = newInstance.Logger;
178180

@@ -183,8 +185,6 @@ public bool CanInvoke()
183185
_hostStartCount++;
184186
}
185187

186-
OnHostCreated();
187-
188188
string extensionVersion = _settingsManager.GetSetting(EnvironmentSettingNames.FunctionsExtensionVersion);
189189
string hostId = newInstance.ScriptConfig.HostConfig.HostId;
190190
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()
196196
// log any function initialization errors
197197
LogErrors(newInstance);
198198

199-
OnHostStarted();
200-
201-
// only after ALL initialization is complete do we set the
202-
// state to Running
203-
State = ScriptHostState.Running;
204199
LastError = null;
205200
consecutiveErrorCount = 0;
206201
_restartDelayTokenSource = null;
@@ -262,6 +257,16 @@ public bool CanInvoke()
262257
while (!_stopped && !cancellationToken.IsCancellationRequested);
263258
}
264259

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+
265270
private Task CreateRestartBackoffDelay(int consecutiveErrorCount)
266271
{
267272
_restartDelayTokenSource = new CancellationTokenSource();
@@ -296,6 +301,9 @@ private static void LogErrors(ScriptHost host)
296301
/// <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>
297302
private async Task Orphan(ScriptHost instance, bool forceStop = false)
298303
{
304+
instance.HostInitialized -= OnHostInitialized;
305+
instance.HostStarted -= OnHostStarted;
306+
299307
lock (_liveInstances)
300308
{
301309
bool removed = _liveInstances.Remove(instance);
@@ -397,20 +405,28 @@ protected virtual void OnInitializeConfig(ScriptHostConfiguration config)
397405
}
398406
}
399407

400-
protected virtual void OnHostCreated()
408+
protected virtual void OnHostInitialized()
401409
{
402-
State = ScriptHostState.Created;
410+
State = ScriptHostState.Initialized;
403411
}
404412

405-
protected virtual void OnHostStarting()
413+
/// <summary>
414+
/// Called after the host has been started.
415+
/// </summary>
416+
protected virtual void OnHostStarted()
406417
{
407-
IsHostHealthy(throwWhenUnhealthy: true);
418+
var metricsLogger = _config.HostConfig.GetService<IMetricsLogger>();
419+
metricsLogger.LogEvent(new HostStarted(Instance));
420+
421+
State = ScriptHostState.Running;
408422
}
409423

410-
protected virtual void OnHostStarted()
424+
/// <summary>
425+
/// Called immediately before we begin creating the host.
426+
/// </summary>
427+
protected virtual void OnCreatingHost()
411428
{
412-
var metricsLogger = _config.HostConfig.GetService<IMetricsLogger>();
413-
metricsLogger.LogEvent(new HostStarted(Instance));
429+
IsHostHealthy(throwWhenUnhealthy: true);
414430
}
415431

416432
public void Dispose()

src/WebJobs.Script/Host/ScriptHostState.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ public enum ScriptHostState
1111
Default,
1212

1313
/// <summary>
14-
/// The host has been created and can accept direct function
15-
/// invocations, but listeners are not yet started.
14+
/// The host has been fully initialized and can accept direct function
15+
/// invocations. All functions have been indexed. Listeners may not yet
16+
/// be not yet running.
1617
/// </summary>
17-
Created,
18+
Initialized,
1819

1920
/// <summary>
2021
/// The host is fully running.

src/WebJobs.Script/WebJobs.Script.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022">
2626
<PrivateAssets>None</PrivateAssets>
2727
</PackageReference>
28-
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11077" />
28+
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11084" />
2929
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta3" />
3030
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.0-beta3">
3131
<NoWarn>NU1701</NoWarn>

0 commit comments

Comments
 (0)