Skip to content

Commit 16ef27c

Browse files
committed
Moving InStandbyMode to WebScriptHostManager
1 parent e9cbeab commit 16ef27c

File tree

5 files changed

+49
-54
lines changed

5 files changed

+49
-54
lines changed

src/WebJobs.Script.WebHost/App_Start/WebHostResolver.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static void EnsureInitialized(WebHostSettings settings)
108108
{
109109
// standby mode can only change from true to false
110110
// When standby mode changes, we reset all instances
111-
var standbyMode = ScriptHost.InStandbyMode;
111+
var standbyMode = WebScriptHostManager.InStandbyMode;
112112
if (!standbyMode)
113113
{
114114
if (_activeHostManager == null)
@@ -182,8 +182,7 @@ private static ScriptHostConfiguration GetScriptHostConfiguration(string scriptP
182182
{
183183
RootScriptPath = scriptPath,
184184
RootLogPath = logPath,
185-
FileLoggingEnabled = true,
186-
FileWatchingEnabled = !ScriptHost.InStandbyMode
185+
FileLoggingEnabled = true
187186
};
188187

189188
// If running on Azure Web App, derive the host ID from the site name

src/WebJobs.Script.WebHost/Handlers/EnsureHostRunningHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
3333
// some routes do not require the host to be running (most do)
3434
// in standby mode, we don't want to wait for host start
3535
bool bypassHostCheck = request.RequestUri.LocalPath.Trim('/').ToLowerInvariant().EndsWith("admin/host/status") ||
36-
ScriptHost.InStandbyMode;
36+
WebScriptHostManager.InStandbyMode;
3737

3838
if (!scriptHostManager.Initialized)
3939
{

src/WebJobs.Script.WebHost/WebScriptHostManager.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost
2626
public class WebScriptHostManager : ScriptHostManager
2727
{
2828
private static Lazy<MethodInfo> _getWebHookDataMethod = new Lazy<MethodInfo>(CreateGetWebHookDataMethodInfo);
29+
private static bool? _standbyMode;
2930
private readonly IMetricsLogger _metricsLogger;
3031
private readonly SecretManager _secretManager;
3132
private readonly WebHostSettings _webHostSettings;
@@ -54,7 +55,7 @@ public bool Initialized
5455
{
5556
get
5657
{
57-
if (ScriptHost.InStandbyMode)
58+
if (InStandbyMode)
5859
{
5960
return _warmupComplete;
6061
}
@@ -65,6 +66,27 @@ public bool Initialized
6566
}
6667
}
6768

69+
public static bool InStandbyMode
70+
{
71+
get
72+
{
73+
// once set, never reset
74+
if (_standbyMode != null)
75+
{
76+
return _standbyMode.Value;
77+
}
78+
if (Environment.GetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE") == "1")
79+
{
80+
return true;
81+
}
82+
83+
// no longer standby mode
84+
_standbyMode = false;
85+
86+
return _standbyMode.Value;
87+
}
88+
}
89+
6890
public async Task<HttpResponseMessage> HandleRequestAsync(FunctionDescriptor function, HttpRequestMessage request, CancellationToken cancellationToken)
6991
{
7092
// All authentication is assumed to have been done on the request
@@ -94,7 +116,7 @@ public void Initialize()
94116
{
95117
lock (_syncLock)
96118
{
97-
if (ScriptHost.InStandbyMode)
119+
if (InStandbyMode)
98120
{
99121
if (!_warmupComplete)
100122
{
@@ -208,6 +230,12 @@ protected override void Dispose(bool disposing)
208230
base.Dispose(disposing);
209231
}
210232

233+
// this is for testing only
234+
internal static void ResetStandbyMode()
235+
{
236+
_standbyMode = null;
237+
}
238+
211239
private static MethodInfo CreateGetWebHookDataMethodInfo()
212240
{
213241
return typeof(WebHookHandlerContextExtensions).GetMethod("GetDataOrDefault", BindingFlags.Public | BindingFlags.Static);

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7-
using System.Configuration;
87
using System.Diagnostics;
98
using System.Globalization;
109
using System.IO;
@@ -32,7 +31,6 @@ namespace Microsoft.Azure.WebJobs.Script
3231
public class ScriptHost : JobHost
3332
{
3433
private const string HostAssemblyName = "ScriptHost";
35-
private static bool? _standbyMode;
3634
private readonly AutoResetEvent _restartEvent = new AutoResetEvent(false);
3735
private string _instanceId;
3836
private Action<FileSystemEventArgs> _restart;
@@ -50,27 +48,6 @@ protected ScriptHost(ScriptHostConfiguration scriptConfig)
5048

5149
public event EventHandler IsPrimaryChanged;
5250

53-
public static bool InStandbyMode
54-
{
55-
get
56-
{
57-
// once set, never reset
58-
if (_standbyMode != null)
59-
{
60-
return _standbyMode.Value;
61-
}
62-
if (Environment.GetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE") == "1")
63-
{
64-
return true;
65-
}
66-
67-
// no longer standby mode
68-
_standbyMode = false;
69-
70-
return _standbyMode.Value;
71-
}
72-
}
73-
7451
public string InstanceId
7552
{
7653
get
@@ -109,12 +86,6 @@ public AutoResetEvent RestartEvent
10986
}
11087
}
11188

112-
// this is for testing only
113-
public static void ResetStandbyMode()
114-
{
115-
_standbyMode = null;
116-
}
117-
11889
internal void AddFunctionError(string functionName, string error)
11990
{
12091
functionName = Utility.GetFunctionShortName(functionName);
@@ -253,21 +224,18 @@ protected virtual void Initialize()
253224
ScriptConfig.HostConfig.AddService<IMetricsLogger>(new MetricsLogger());
254225
}
255226

256-
if (!InStandbyMode)
227+
var storageString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
228+
if (storageString == null)
257229
{
258-
var storageString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
259-
if (storageString == null)
260-
{
261-
// Disable core storage
262-
ScriptConfig.HostConfig.StorageConnectionString = null;
263-
}
264-
else
265-
{
266-
// Create the lease manager that will keep handle the primary host blob lease acquisition and renewal
267-
// and subscribe for change notifications.
268-
_blobLeaseManager = BlobLeaseManager.Create(storageString, TimeSpan.FromSeconds(15), ScriptConfig.HostConfig.HostId, InstanceId, TraceWriter);
269-
_blobLeaseManager.HasLeaseChanged += BlobLeaseManagerHasLeaseChanged;
270-
}
230+
// Disable core storage
231+
ScriptConfig.HostConfig.StorageConnectionString = null;
232+
}
233+
else
234+
{
235+
// Create the lease manager that will keep handle the primary host blob lease acquisition and renewal
236+
// and subscribe for change notifications.
237+
_blobLeaseManager = BlobLeaseManager.Create(storageString, TimeSpan.FromSeconds(15), ScriptConfig.HostConfig.HostId, InstanceId, TraceWriter);
238+
_blobLeaseManager.HasLeaseChanged += BlobLeaseManagerHasLeaseChanged;
271239
}
272240

273241
List<FunctionDescriptorProvider> descriptionProviders = new List<FunctionDescriptorProvider>()

test/WebJobs.Script.Tests/StandbyModeTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ public void InStandbyMode_ReturnsExpectedValue()
1717
using (new TestEnvironment())
1818
{
1919
// initially false
20-
Assert.Equal(false, ScriptHost.InStandbyMode);
20+
Assert.Equal(false, WebScriptHostManager.InStandbyMode);
2121
}
2222

2323
using (new TestEnvironment())
2424
{
2525
Environment.SetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE", "1");
26-
Assert.Equal(true, ScriptHost.InStandbyMode);
26+
Assert.Equal(true, WebScriptHostManager.InStandbyMode);
2727

2828
Environment.SetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE", "0");
29-
Assert.Equal(false, ScriptHost.InStandbyMode);
29+
Assert.Equal(false, WebScriptHostManager.InStandbyMode);
3030

3131
// test only set one way
3232
Environment.SetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE", "1");
33-
Assert.Equal(false, ScriptHost.InStandbyMode);
33+
Assert.Equal(false, WebScriptHostManager.InStandbyMode);
3434
}
3535
}
3636

@@ -148,7 +148,7 @@ public void Dispose()
148148

149149
private void Reset()
150150
{
151-
ScriptHost.ResetStandbyMode();
151+
WebScriptHostManager.ResetStandbyMode();
152152
WebHostResolver.Reset();
153153
Environment.SetEnvironmentVariable("WEBSITE_PLACEHOLDER_MODE", null);
154154
}

0 commit comments

Comments
 (0)