Skip to content

Commit 3124c02

Browse files
committed
Fixing Host ID initialization (ensuring it is set when FastLogger is created)
1 parent bbcb538 commit 3124c02

File tree

7 files changed

+123
-73
lines changed

7 files changed

+123
-73
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private static void ReinitializeAppSettings()
148148
}
149149
}
150150

151-
private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSettings settings)
151+
internal static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSettings settings)
152152
{
153153
InitializeFileSystem(settings.ScriptPath);
154154

@@ -157,9 +157,12 @@ private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSett
157157
RootScriptPath = settings.ScriptPath,
158158
RootLogPath = settings.LogPath,
159159
FileLoggingMode = FileLoggingMode.DebugOnly,
160-
TraceWriter = settings.TraceWriter
160+
TraceWriter = settings.TraceWriter,
161+
IsSelfHost = settings.IsSelfHost
161162
};
162163

164+
scriptHostConfig.HostConfig.HostId = Utility.GetDefaultHostId(_settingsManager, scriptHostConfig);
165+
163166
return scriptHostConfig;
164167
}
165168

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected virtual void Initialize()
278278

279279
if (string.IsNullOrEmpty(ScriptConfig.HostConfig.HostId))
280280
{
281-
ScriptConfig.HostConfig.HostId = GetDefaultHostId(_settingsManager, ScriptConfig);
281+
ScriptConfig.HostConfig.HostId = Utility.GetDefaultHostId(_settingsManager, ScriptConfig);
282282
}
283283
if (string.IsNullOrEmpty(ScriptConfig.HostConfig.HostId))
284284
{
@@ -1304,44 +1304,6 @@ internal static void ApplyApplicationInsightsConfig(JObject configJson, ScriptHo
13041304
}
13051305
}
13061306

1307-
internal static string GetDefaultHostId(ScriptSettingsManager settingsManager, ScriptHostConfiguration scriptConfig)
1308-
{
1309-
// We're setting the default here on the newly created configuration
1310-
// If the user has explicitly set the HostID via host.json, it will overwrite
1311-
// what we set here
1312-
string hostId = null;
1313-
if (scriptConfig.IsSelfHost)
1314-
{
1315-
// When running locally, derive a stable host ID from machine name
1316-
// and root path. We use a hash rather than the path itself to ensure
1317-
// IDs differ (due to truncation) between folders that may share the same
1318-
// root path prefix.
1319-
// Note that such an ID won't work in distributed scenarios, so should
1320-
// only be used for local/CLI scenarios.
1321-
string sanitizedMachineName = Environment.MachineName
1322-
.Where(char.IsLetterOrDigit)
1323-
.Aggregate(new StringBuilder(), (b, c) => b.Append(c)).ToString();
1324-
hostId = $"{sanitizedMachineName}-{Math.Abs(scriptConfig.RootScriptPath.GetHashCode())}";
1325-
}
1326-
else if (!string.IsNullOrEmpty(settingsManager.AzureWebsiteUniqueSlotName))
1327-
{
1328-
// If running on Azure Web App, derive the host ID from unique site slot name
1329-
hostId = settingsManager.AzureWebsiteUniqueSlotName;
1330-
}
1331-
1332-
if (!string.IsNullOrEmpty(hostId))
1333-
{
1334-
if (hostId.Length > ScriptConstants.MaximumHostIdLength)
1335-
{
1336-
// Truncate to the max host name length if needed
1337-
hostId = hostId.Substring(0, ScriptConstants.MaximumHostIdLength);
1338-
}
1339-
}
1340-
1341-
// Lowercase and trim any trailing '-' as they can cause problems with queue names
1342-
return hostId?.ToLowerInvariant().TrimEnd('-');
1343-
}
1344-
13451307
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
13461308
{
13471309
HandleHostError((Exception)e.ExceptionObject);

src/WebJobs.Script/Utility.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,44 @@ public static string GetFunctionShortName(string functionName)
140140
return functionName;
141141
}
142142

143+
internal static string GetDefaultHostId(ScriptSettingsManager settingsManager, ScriptHostConfiguration scriptConfig)
144+
{
145+
// We're setting the default here on the newly created configuration
146+
// If the user has explicitly set the HostID via host.json, it will overwrite
147+
// what we set here
148+
string hostId = null;
149+
if (scriptConfig.IsSelfHost)
150+
{
151+
// When running locally, derive a stable host ID from machine name
152+
// and root path. We use a hash rather than the path itself to ensure
153+
// IDs differ (due to truncation) between folders that may share the same
154+
// root path prefix.
155+
// Note that such an ID won't work in distributed scenarios, so should
156+
// only be used for local/CLI scenarios.
157+
string sanitizedMachineName = Environment.MachineName
158+
.Where(char.IsLetterOrDigit)
159+
.Aggregate(new StringBuilder(), (b, c) => b.Append(c)).ToString();
160+
hostId = $"{sanitizedMachineName}-{Math.Abs(scriptConfig.RootScriptPath.GetHashCode())}";
161+
}
162+
else if (!string.IsNullOrEmpty(settingsManager.AzureWebsiteUniqueSlotName))
163+
{
164+
// If running on Azure Web App, derive the host ID from unique site slot name
165+
hostId = settingsManager.AzureWebsiteUniqueSlotName;
166+
}
167+
168+
if (!string.IsNullOrEmpty(hostId))
169+
{
170+
if (hostId.Length > ScriptConstants.MaximumHostIdLength)
171+
{
172+
// Truncate to the max host name length if needed
173+
hostId = hostId.Substring(0, ScriptConstants.MaximumHostIdLength);
174+
}
175+
}
176+
177+
// Lowercase and trim any trailing '-' as they can cause problems with queue names
178+
return hostId?.ToLowerInvariant().TrimEnd('-');
179+
}
180+
143181
public static string FlattenException(Exception ex, Func<string, string> sourceFormatter = null, bool includeSource = true)
144182
{
145183
StringBuilder flattenedErrorsBuilder = new StringBuilder();

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,38 +1214,6 @@ public void IsFunction_ReturnsExpectedResult()
12141214
Assert.False(host.IsFunction(null));
12151215
}
12161216

1217-
[Fact]
1218-
public void GetDefaultHostId_SelfHost_ReturnsExpectedResult()
1219-
{
1220-
var config = new ScriptHostConfiguration
1221-
{
1222-
IsSelfHost = true,
1223-
RootScriptPath = @"c:\testing\FUNCTIONS-TEST\test$#"
1224-
};
1225-
var scriptSettingsManagerMock = new Mock<ScriptSettingsManager>(MockBehavior.Strict);
1226-
1227-
string hostId = ScriptHost.GetDefaultHostId(scriptSettingsManagerMock.Object, config);
1228-
string sanitizedMachineName = Environment.MachineName
1229-
.Where(char.IsLetterOrDigit)
1230-
.Aggregate(new StringBuilder(), (b, c) => b.Append(c)).ToString().ToLowerInvariant();
1231-
Assert.Equal($"{sanitizedMachineName}-789851553", hostId);
1232-
}
1233-
1234-
[Theory]
1235-
[InlineData("TEST-FUNCTIONS--", "test-functions")]
1236-
[InlineData("TEST-FUNCTIONS-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "test-functions-xxxxxxxxxxxxxxxxx")]
1237-
[InlineData("TEST-FUNCTIONS-XXXXXXXXXXXXXXXX-XXXX", "test-functions-xxxxxxxxxxxxxxxx")] /* 32nd character is a '-' */
1238-
[InlineData(null, null)]
1239-
public void GetDefaultHostId_AzureHost_ReturnsExpectedResult(string input, string expected)
1240-
{
1241-
var config = new ScriptHostConfiguration();
1242-
var scriptSettingsManagerMock = new Mock<ScriptSettingsManager>(MockBehavior.Strict);
1243-
scriptSettingsManagerMock.SetupGet(p => p.AzureWebsiteUniqueSlotName).Returns(() => input);
1244-
1245-
string hostId = ScriptHost.GetDefaultHostId(scriptSettingsManagerMock.Object, config);
1246-
Assert.Equal(expected, hostId);
1247-
}
1248-
12491217
public class AssemblyMock : Assembly
12501218
{
12511219
public override object[] GetCustomAttributes(Type attributeType, bool inherit)

test/WebJobs.Script.Tests/UtilityTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Dynamic;
8+
using System.Linq;
89
using System.Reflection;
910
using System.Text;
1011
using System.Threading;
1112
using System.Threading.Tasks;
13+
using Microsoft.Azure.WebJobs.Script.Config;
14+
using Moq;
1215
using Newtonsoft.Json.Linq;
1316
using Xunit;
1417

@@ -307,5 +310,37 @@ public void IsNullable_ReturnsExpectedResult(Type type, bool expected)
307310
{
308311
Assert.Equal(expected, Utility.IsNullable(type));
309312
}
313+
314+
[Theory]
315+
[InlineData("TEST-FUNCTIONS--", "test-functions")]
316+
[InlineData("TEST-FUNCTIONS-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "test-functions-xxxxxxxxxxxxxxxxx")]
317+
[InlineData("TEST-FUNCTIONS-XXXXXXXXXXXXXXXX-XXXX", "test-functions-xxxxxxxxxxxxxxxx")] /* 32nd character is a '-' */
318+
[InlineData(null, null)]
319+
public void GetDefaultHostId_AzureHost_ReturnsExpectedResult(string input, string expected)
320+
{
321+
var config = new ScriptHostConfiguration();
322+
var scriptSettingsManagerMock = new Mock<ScriptSettingsManager>(MockBehavior.Strict);
323+
scriptSettingsManagerMock.SetupGet(p => p.AzureWebsiteUniqueSlotName).Returns(() => input);
324+
325+
string hostId = Utility.GetDefaultHostId(scriptSettingsManagerMock.Object, config);
326+
Assert.Equal(expected, hostId);
327+
}
328+
329+
[Fact]
330+
public void GetDefaultHostId_SelfHost_ReturnsExpectedResult()
331+
{
332+
var config = new ScriptHostConfiguration
333+
{
334+
IsSelfHost = true,
335+
RootScriptPath = @"c:\testing\FUNCTIONS-TEST\test$#"
336+
};
337+
var scriptSettingsManagerMock = new Mock<ScriptSettingsManager>(MockBehavior.Strict);
338+
339+
string hostId = Utility.GetDefaultHostId(scriptSettingsManagerMock.Object, config);
340+
string sanitizedMachineName = Environment.MachineName
341+
.Where(char.IsLetterOrDigit)
342+
.Aggregate(new StringBuilder(), (b, c) => b.Append(c)).ToString().ToLowerInvariant();
343+
Assert.Equal($"{sanitizedMachineName}-789851553", hostId);
344+
}
310345
}
311346
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.Azure.WebJobs.Script.Config;
7+
using Microsoft.Azure.WebJobs.Script.WebHost;
8+
using Moq;
9+
using Xunit;
10+
11+
namespace Microsoft.Azure.WebJobs.Script.Tests
12+
{
13+
public class WebHostResolverTests
14+
{
15+
[Fact]
16+
public void GetScriptHostConfiguration_SetsHostId()
17+
{
18+
var environmentSettings = new Dictionary<string, string>
19+
{
20+
{ EnvironmentSettingNames.AzureWebsiteName, "testsite" },
21+
{ EnvironmentSettingNames.AzureWebsiteSlotName, "production" },
22+
};
23+
24+
using (var environment = new TestScopedEnvironmentVariable(environmentSettings))
25+
{
26+
var settingsManager = new ScriptSettingsManager();
27+
var secretManagerFactoryMock = new Mock<ISecretManagerFactory>();
28+
var resolver = new WebHostResolver(settingsManager, secretManagerFactoryMock.Object);
29+
30+
var settings = new WebHostSettings
31+
{
32+
ScriptPath = @"c:\some\path",
33+
LogPath = @"c:\log\path",
34+
SecretsPath = @"c:\secrets\path"
35+
};
36+
37+
ScriptHostConfiguration configuration = resolver.GetScriptHostConfiguration(settings);
38+
39+
Assert.Equal("testsite", configuration.HostConfig.HostId);
40+
}
41+
}
42+
}
43+
}

test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@
541541
<Compile Include="WebHooks\DynamicWebHookReceiverConfigTests.cs" />
542542
<Compile Include="Handlers\WebScriptHostHandlerTests.cs" />
543543
<Compile Include="WebHooks\WebHookReceiverManagerTests.cs" />
544+
<Compile Include="WebHostResolverTests.cs" />
544545
<Compile Include="WebScriptHostRequestManagerTests.cs" />
545546
<AdditionalFiles Include="..\..\stylecop.json">
546547
<Link>stylecop.json</Link>

0 commit comments

Comments
 (0)