Skip to content

Commit a2c2693

Browse files
committed
Isolation for placeholder mode files
1 parent acdc441 commit a2c2693

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,11 @@ internal void EnsureInitialized(WebHostSettings settings)
114114
if (!WebScriptHostManager.InStandbyMode)
115115
{
116116
// standby mode can only change from true to false
117-
// When standby mode changes, we reset all instances
117+
// when standby mode changes, we reset all instances
118118
if (_activeHostManager == null)
119119
{
120+
_settingsManager.Reset();
121+
120122
_activeScriptHostConfig = CreateScriptHostConfiguration(settings);
121123
_activeHostManager = new WebScriptHostManager(_activeScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
122124
_activeReceiverManager = new WebHookReceiverManager(_activeHostManager.SecretManager);
@@ -139,15 +141,15 @@ internal void EnsureInitialized(WebHostSettings settings)
139141
_standbyScriptHostConfig = null;
140142
_standbyHostManager = null;
141143
_standbyReceiverManager = null;
142-
_settingsManager.Reset();
143144
}
144145
}
145146
else
146147
{
147148
if (_standbyHostManager == null)
148149
{
149-
_standbyScriptHostConfig = CreateScriptHostConfiguration(settings, true);
150-
_standbyHostManager = new WebScriptHostManager(_standbyScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, settings);
150+
var standbySetings = CreateStandbySettings(settings);
151+
_standbyScriptHostConfig = CreateScriptHostConfiguration(standbySetings, true);
152+
_standbyHostManager = new WebScriptHostManager(_standbyScriptHostConfig, _secretManagerFactory, _eventManager, _settingsManager, standbySetings);
151153
_standbyReceiverManager = new WebHookReceiverManager(_standbyHostManager.SecretManager);
152154

153155
InitializeFileSystem();
@@ -156,6 +158,26 @@ internal void EnsureInitialized(WebHostSettings settings)
156158
}
157159
}
158160

161+
internal static WebHostSettings CreateStandbySettings(WebHostSettings settings)
162+
{
163+
// we need to create a new copy of the settings to avoid modifying
164+
// the global settings
165+
// important that we use paths that are different than the configured paths
166+
// to ensure that placeholder files are isolated
167+
string tempRoot = Path.GetTempPath();
168+
var standbySettings = new WebHostSettings
169+
{
170+
LogPath = Path.Combine(tempRoot, @"Functions\Standby\Logs"),
171+
ScriptPath = Path.Combine(tempRoot, @"Functions\Standby\WWWRoot"),
172+
SecretsPath = Path.Combine(tempRoot, @"Functions\Standby\Secrets"),
173+
LoggerFactoryBuilder = settings.LoggerFactoryBuilder,
174+
TraceWriter = settings.TraceWriter,
175+
IsSelfHost = settings.IsSelfHost
176+
};
177+
178+
return standbySettings;
179+
}
180+
159181
internal static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSettings settings, bool inStandbyMode = false)
160182
{
161183
var scriptHostConfig = new ScriptHostConfiguration
@@ -170,7 +192,6 @@ internal static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSet
170192

171193
if (inStandbyMode)
172194
{
173-
scriptHostConfig.RootScriptPath = Path.Combine(Path.GetTempPath(), "Functions", "Standby");
174195
scriptHostConfig.FileLoggingMode = FileLoggingMode.DebugOnly;
175196
scriptHostConfig.HostConfig.StorageConnectionString = null;
176197
scriptHostConfig.HostConfig.DashboardConnectionString = null;

test/WebJobs.Script.Tests.Integration/Host/StandbyManagerTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public StandbyManagerTests()
2525
{
2626
_settingsManager = ScriptSettingsManager.Instance;
2727
_traceWriter = new TestTraceWriter(TraceLevel.Info);
28+
WebScriptHostManager.ResetStandbyMode();
2829
}
2930

3031
[Fact]
@@ -81,7 +82,7 @@ public async Task StandbyMode_EndToEnd()
8182
var webHostSettings = new WebHostSettings
8283
{
8384
IsSelfHost = true,
84-
LogPath = Path.Combine(testRootPath, "Logs"),
85+
LogPath = testRootPath,
8586
SecretsPath = Path.Combine(testRootPath, "Secrets"),
8687
ScriptPath = testRootPath,
8788
TraceWriter = traceWriter
@@ -95,7 +96,7 @@ public async Task StandbyMode_EndToEnd()
9596
TestHelpers.WaitForWebHost(httpClient);
9697

9798
var traces = traceWriter.Traces.ToArray();
98-
Assert.Equal($"Creating StandbyMode placeholder function directory ({Path.GetTempPath()}Functions\\Standby)", traces[0].Message);
99+
Assert.Equal($"Creating StandbyMode placeholder function directory ({Path.GetTempPath()}Functions\\Standby\\WWWRoot)", traces[0].Message);
99100
Assert.Equal("StandbyMode placeholder function directory created", traces[1].Message);
100101

101102
// issue warmup request and verify
@@ -123,12 +124,16 @@ public async Task StandbyMode_EndToEnd()
123124

124125
await Task.Delay(2000);
125126

127+
// verify secrets directory
128+
126129
// verify logs
127130
string[] logLines = traceWriter.Traces.Select(p => p.Message).ToArray();
128131
Assert.Equal(2, logLines.Count(p => p.Contains("Host is in standby mode")));
129132
Assert.Equal(1, logLines.Count(p => p.Contains("Stopping Host")));
130133
Assert.Equal(2, logLines.Count(p => p.Contains("Executed 'Functions.WarmUp' (Succeeded")));
131134
Assert.Equal(1, logLines.Count(p => p.Contains("Starting host specialization")));
135+
136+
WebScriptHostManager.ResetStandbyMode();
132137
}
133138
}
134139
}

test/WebJobs.Script.Tests.Integration/Host/StandbyModeTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public StandbyModeTests()
2727

2828
_webHostResolver = new WebHostResolver(_settingsManager, new TestSecretManagerFactory(false), eventManagerMock.Object);
2929
_traceWriter = new TestTraceWriter(TraceLevel.Info);
30+
31+
WebScriptHostManager.ResetStandbyMode();
3032
}
3133

3234
[Fact]

test/WebJobs.Script.Tests/WebHostResolverTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public void CreateScriptHostConfiguration_StandbyMode_ReturnsExpectedConfigurati
4848
{
4949
var settings = new WebHostSettings
5050
{
51-
IsSelfHost = true
51+
IsSelfHost = true,
52+
ScriptPath = Path.Combine(Path.GetTempPath(), "Functions", "Standby")
5253
};
5354

5455
var config = WebHostResolver.CreateScriptHostConfiguration(settings, true);
5556

5657
Assert.Equal(FileLoggingMode.DebugOnly, config.FileLoggingMode);
5758
Assert.Null(config.HostConfig.StorageConnectionString);
5859
Assert.Null(config.HostConfig.DashboardConnectionString);
59-
Assert.Equal(Path.Combine(Path.GetTempPath(), "Functions", "Standby"), config.RootScriptPath);
6060
}
6161
}
6262
}

0 commit comments

Comments
 (0)