Skip to content

Commit d29c39f

Browse files
authored
[v3.x] Worker profile cleanup (#8764)
1 parent 4ffa1e4 commit d29c39f

File tree

14 files changed

+93
-77
lines changed

14 files changed

+93
-77
lines changed

src/WebJobs.Script/Environment/EnvironmentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static bool IsV2CompatibilityMode(this IEnvironment environment)
134134
return isFunctionsV2CompatibilityMode || isV2ExtensionVersion;
135135
}
136136

137-
public static bool IsV2CompatibileOnV3Extension(this IEnvironment environment)
137+
public static bool IsV2CompatibleOnV3Extension(this IEnvironment environment)
138138
{
139139
string compatModeString = environment.GetEnvironmentVariable(FunctionsV2CompatibilityModeKey);
140140
bool.TryParse(compatModeString, out bool isFunctionsV2CompatibilityMode);

src/WebJobs.Script/Host/DebugStateProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public DebugStateProvider(IEnvironment environment, IOptionsMonitor<ScriptApplic
2525
_debugModeEvent = eventManager.OfType<DebugNotification>()
2626
.Subscribe(evt => LastDebugNotify = evt.NotificationTime);
2727
_diagnosticModeEvent = eventManager.OfType<DiagnosticNotification>()
28-
.Subscribe(evt => LastDiagnosticNotify = evt.NotificationTime);
28+
.Subscribe(evt => LastDiagnosticNotify = evt.NotificationTime);
2929

3030
_scriptOptions = scriptOptions;
3131
_scriptOptions.OnChange(_ => InitializeLastNotificationTimes());

src/WebJobs.Script/Workers/Profiles/EnvironmentCondition.cs renamed to src/WebJobs.Script/Workers/Profiles/Conditions/EnvironmentCondition.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
using System;
55
using System.ComponentModel.DataAnnotations;
66
using System.Text.RegularExpressions;
7-
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
87
using Microsoft.Extensions.Logging;
98

10-
namespace Microsoft.Azure.WebJobs.Script.Workers
9+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
1110
{
1211
/// <summary>
1312
/// An implementation of an <see cref="IWorkerProfileCondition"/> that checks if
@@ -51,7 +50,7 @@ public bool Evaluate()
5150
return false;
5251
}
5352

54-
_logger.LogDebug($"Evaluating EnvironmentCondition with name: '{Name}', value: '{value}' and expression: '{Expression}'");
53+
_logger.LogDebug("Evaluating EnvironmentCondition with name: '{name}', value: '{value}' and expression: '{expression}'", Name, value, Expression);
5554

5655
return _regex.IsMatch(value);
5756
}

src/WebJobs.Script/Workers/Profiles/FalseCondition.cs renamed to src/WebJobs.Script/Workers/Profiles/Conditions/FalseCondition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
namespace Microsoft.Azure.WebJobs.Script.Workers
4+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
55
{
66
/// <summary>
77
/// An implementation of an <see cref="IWorkerProfileCondition"/> that always evaluates to false.

src/WebJobs.Script/Workers/Profiles/HostPropertyCondition.cs renamed to src/WebJobs.Script/Workers/Profiles/Conditions/HostPropertyCondition.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
using System.Linq;
77
using System.Text.RegularExpressions;
88
using Microsoft.Azure.WebJobs.Script.Config;
9-
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
109
using Microsoft.Extensions.Logging;
1110

12-
namespace Microsoft.Azure.WebJobs.Script.Workers
11+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
1312
{
1413
/// <summary>
1514
/// An implementation of an <see cref="IWorkerProfileCondition"/> that checks if different host properties
@@ -39,7 +38,7 @@ public HostPropertyCondition(ILogger logger, ISystemRuntimeInformation systemRun
3938
Validate();
4039
}
4140

42-
public enum HostProperty
41+
private enum HostProperty
4342
{
4443
Sku,
4544
Platform,
@@ -68,27 +67,27 @@ public bool Evaluate()
6867
return false;
6968
}
7069

71-
_logger.LogDebug($"Evaluating HostPropertyCondition with name: {Name}, value: {value} and expression {Expression}");
70+
_logger.LogDebug("Evaluating HostPropertyCondition with name: {name}, value: {value} and expression {expression}", Name, value, Expression);
7271

7372
return _regex.IsMatch(value);
7473
}
7574

76-
// Validates if condition parametrs meet expected values, fail if they don't
75+
// Validates if condition parameters meet expected values, fail if they don't
7776
private void Validate()
7877
{
7978
if (string.IsNullOrEmpty(Name))
8079
{
81-
throw new ValidationException($"HostPropertyCondition {nameof(Name)} cannot be empty.");
80+
throw new ValidationException($"HostPropertyCondition {nameof(Name)} cannot be empty.");
8281
}
8382

8483
if (!Enum.GetNames(typeof(HostProperty)).Any(x => x.ToLower().Contains(Name.ToLower())))
8584
{
86-
throw new ValidationException($"HostPropertyCondition {nameof(Name)} is not a valid host property name.");
85+
throw new ValidationException($"HostPropertyCondition {nameof(Name)} is not a valid host property name.");
8786
}
8887

8988
if (string.IsNullOrEmpty(Expression))
9089
{
91-
throw new ValidationException($"HostPropertyCondition {nameof(Expression)} cannot be empty.");
90+
throw new ValidationException($"HostPropertyCondition {nameof(Expression)} cannot be empty.");
9291
}
9392

9493
try
@@ -97,7 +96,7 @@ private void Validate()
9796
}
9897
catch
9998
{
100-
throw new ValidationException($"HostPropertyCondition {nameof(Expression)} must be a valid regular expression.");
99+
throw new ValidationException($"HostPropertyCondition {nameof(Expression)} must be a valid regular expression.");
101100
}
102101
}
103102
}

src/WebJobs.Script/Workers/Profiles/IWorkerProfileCondition.cs renamed to src/WebJobs.Script/Workers/Profiles/Conditions/IWorkerProfileCondition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
namespace Microsoft.Azure.WebJobs.Script.Workers
4+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
55
{
66
/// <summary>
77
/// Interface for different types of profile conditions
88
/// </summary>
99
public interface IWorkerProfileCondition
1010
{
1111
/// <summary>
12-
/// Check if different condition type meet their criteria
12+
/// Checks if a conditions criteria is being met
1313
/// </summary>
1414
bool Evaluate();
1515
}

src/WebJobs.Script/Workers/Profiles/IWorkerProfileManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
66
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
77

8-
namespace Microsoft.Azure.WebJobs.Script.Workers
8+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
99
{
1010
/// <summary>
1111
/// Interface to regulate profile operations through the profile manager

src/WebJobs.Script/Workers/Profiles/WorkerDescriptionProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.ComponentModel.DataAnnotations;
77
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
88

9-
namespace Microsoft.Azure.WebJobs.Script.Workers
9+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
1010
{
1111
/// <summary>
1212
/// Class that holds data of a profile

src/WebJobs.Script/Workers/Profiles/WorkerProfileConditionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Azure.WebJobs.Script.Workers.Profiles;
66
using Microsoft.Extensions.Logging;
77

8-
namespace Microsoft.Azure.WebJobs.Script.Workers
8+
namespace Microsoft.Azure.WebJobs.Script.Workers.Profiles
99
{
1010
internal sealed class WorkerProfileConditionProvider : IWorkerProfileConditionProvider
1111
{

src/WebJobs.Script/Workers/Rpc/Configuration/RpcWorkerConfigFactory.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,32 @@ internal class RpcWorkerConfigFactory
2929
private Dictionary<string, RpcWorkerConfig> _workerDescriptionDictionary = new Dictionary<string, RpcWorkerConfig>();
3030

3131
public RpcWorkerConfigFactory(IConfiguration config,
32-
ILogger logger,
33-
ISystemRuntimeInformation systemRuntimeInfo,
34-
IEnvironment environment,
35-
IMetricsLogger metricsLogger)
32+
ILogger logger,
33+
ISystemRuntimeInformation systemRuntimeInfo,
34+
IEnvironment environment,
35+
IMetricsLogger metricsLogger)
3636
{
3737
_config = config ?? throw new ArgumentNullException(nameof(config));
3838
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3939
_systemRuntimeInformation = systemRuntimeInfo ?? throw new ArgumentNullException(nameof(systemRuntimeInfo));
4040
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
4141
_metricsLogger = metricsLogger;
4242
_workerRuntime = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName);
43-
string assemblyLocalPath = Path.GetDirectoryName(new Uri(typeof(RpcWorkerConfigFactory).Assembly.CodeBase).LocalPath);
43+
44+
var conditionProviders = new List<IWorkerProfileConditionProvider>
45+
{
46+
new WorkerProfileConditionProvider(_logger, _environment)
47+
};
48+
49+
_profileManager = new WorkerProfileManager(_logger, conditionProviders);
50+
4451
WorkersDirPath = GetDefaultWorkersDirectory(Directory.Exists);
4552
var workersDirectorySection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{WorkerConstants.WorkersDirectorySectionName}");
53+
4654
if (!string.IsNullOrEmpty(workersDirectorySection.Value))
4755
{
4856
WorkersDirPath = workersDirectorySection.Value;
4957
}
50-
var conditionProviders = new List<IWorkerProfileConditionProvider>
51-
{
52-
new WorkerProfileConditionProvider(_logger, _environment)
53-
};
54-
_profileManager = new WorkerProfileManager(_logger, conditionProviders);
5558
}
5659

5760
public string WorkersDirPath { get; }
@@ -85,7 +88,7 @@ internal void BuildWorkerProviderDictionary()
8588

8689
internal void AddProviders()
8790
{
88-
_logger.LogDebug($"Workers Directory set to: {WorkersDirPath}");
91+
_logger.LogDebug("Workers Directory set to: {WorkersDirPath}", WorkersDirPath);
8992

9093
foreach (var workerDir in Directory.EnumerateDirectories(WorkersDirPath))
9194
{
@@ -118,13 +121,16 @@ internal void AddProvider(string workerDir)
118121
try
119122
{
120123
string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName);
124+
121125
if (!File.Exists(workerConfigPath))
122126
{
123-
_logger.LogDebug($"Did not find worker config file at: {workerConfigPath}");
127+
_logger.LogDebug("Did not find worker config file at: {workerConfigPath}", workerConfigPath);
124128
return;
125129
}
130+
131+
_logger.LogDebug("Found worker config: {workerConfigPath}", workerConfigPath);
132+
126133
// Parse worker config file
127-
_logger.LogDebug($"Found worker config: {workerConfigPath}");
128134
string json = File.ReadAllText(workerConfigPath);
129135
JObject workerConfig = JObject.Parse(json);
130136
RpcWorkerDescription workerDescription = workerConfig.Property(WorkerConstants.WorkerDescription).Value.ToObject<RpcWorkerDescription>();
@@ -142,7 +148,7 @@ internal void AddProvider(string workerDir)
142148
}
143149
}
144150

145-
// Check if any appsettings are provided for that langauge
151+
// Check if any app settings are provided for that language
146152
var languageSection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{workerDescription.Language}");
147153
workerDescription.Arguments = workerDescription.Arguments ?? new List<string>();
148154
GetWorkerDescriptionFromAppSettings(workerDescription, languageSection);
@@ -165,21 +171,25 @@ internal void AddProvider(string workerDir)
165171
ExecutablePath = workerDescription.DefaultExecutablePath,
166172
WorkerPath = workerDescription.DefaultWorkerPath
167173
};
174+
168175
arguments.ExecutableArguments.AddRange(workerDescription.Arguments);
176+
169177
var rpcWorkerConfig = new RpcWorkerConfig()
170178
{
171179
Description = workerDescription,
172180
Arguments = arguments,
173181
CountOptions = workerProcessCount,
174182
};
183+
175184
_workerDescriptionDictionary[workerDescription.Language] = rpcWorkerConfig;
176185
ReadLanguageWorkerFile(arguments.WorkerPath);
177-
_logger.LogDebug($"Added WorkerConfig for language: {workerDescription.Language}");
186+
187+
_logger.LogDebug("Added WorkerConfig for language: {language}", workerDescription.Language);
178188
}
179189
}
180-
catch (Exception ex)
190+
catch (Exception ex) when (!ex.IsFatal())
181191
{
182-
_logger?.LogError(ex, $"Failed to initialize worker provider for: {workerDir}");
192+
_logger.LogError(ex, "Failed to initialize worker provider for: {workerDir}", workerDir);
183193
}
184194
}
185195
}
@@ -194,6 +204,7 @@ private List<WorkerDescriptionProfile> ReadWorkerDescriptionProfiles(JToken prof
194204
}
195205

196206
var descriptionProfiles = new List<WorkerDescriptionProfile>(profiles.Count);
207+
197208
try
198209
{
199210
foreach (var profile in profiles)
@@ -205,7 +216,7 @@ private List<WorkerDescriptionProfile> ReadWorkerDescriptionProfiles(JToken prof
205216
if (!_profileManager.TryCreateWorkerProfileCondition(descriptor, out IWorkerProfileCondition condition))
206217
{
207218
// Failed to resolve condition. This profile will be disabled using a mock false condition
208-
_logger?.LogInformation($"Profile {profile.ProfileName} is disabled. Cannot resolve the profile condition {descriptor.Type}");
219+
_logger.LogInformation("Profile {name} is disabled. Cannot resolve the profile condition {condition}", profile.ProfileName, descriptor.Type);
209220
condition = new FalseCondition();
210221
}
211222

@@ -219,6 +230,7 @@ private List<WorkerDescriptionProfile> ReadWorkerDescriptionProfiles(JToken prof
219230
{
220231
throw new FormatException("Failed to parse profiles in worker config.");
221232
}
233+
222234
return descriptionProfiles;
223235
}
224236

@@ -286,31 +298,33 @@ internal bool ShouldAddWorkerConfig(string workerDescriptionLanguage)
286298

287299
if (_environment.IsMultiLanguageRuntimeEnvironment())
288300
{
289-
_logger.LogInformation($"Found multi-language runtime environment. Starting WorkerConfig for language: {workerDescriptionLanguage}");
301+
_logger.LogInformation("Found multi-language runtime environment. Starting WorkerConfig for language: {workerDescriptionLanguage}", workerDescriptionLanguage);
290302
return true;
291303
}
292304

293305
if (!string.IsNullOrEmpty(_workerRuntime))
294306
{
295-
_logger.LogDebug($"EnvironmentVariable {RpcWorkerConstants.FunctionWorkerRuntimeSettingName}: {_workerRuntime}");
307+
_logger.LogDebug("EnvironmentVariable {functionWorkerRuntimeSettingName}: {workerRuntime}", RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _workerRuntime);
296308
if (_workerRuntime.Equals(workerDescriptionLanguage, StringComparison.OrdinalIgnoreCase))
297309
{
298310
return true;
299311
}
312+
300313
// After specialization only create worker provider for the language set by FUNCTIONS_WORKER_RUNTIME env variable
301-
_logger.LogInformation($"{RpcWorkerConstants.FunctionWorkerRuntimeSettingName} set to {_workerRuntime}. Skipping WorkerConfig for language:{workerDescriptionLanguage}");
314+
_logger.LogInformation("{FUNCTIONS_WORKER_RUNTIME} set to {workerRuntime}. Skipping WorkerConfig for language: {workerDescriptionLanguage}", RpcWorkerConstants.FunctionWorkerRuntimeSettingName, _workerRuntime, workerDescriptionLanguage);
302315
return false;
303316
}
317+
304318
return true;
305319
}
306320

307-
internal void ReadLanguageWorkerFile(string workerPath)
321+
private void ReadLanguageWorkerFile(string workerPath)
308322
{
309-
if (_environment.IsPlaceholderModeEnabled() &&
310-
!string.IsNullOrEmpty(_workerRuntime) &&
311-
File.Exists(workerPath))
323+
if (_environment.IsPlaceholderModeEnabled()
324+
&& !string.IsNullOrEmpty(_workerRuntime)
325+
&& File.Exists(workerPath))
312326
{
313-
// Read lanaguage worker file to avoid disk reads during specialization. This is only to page-in bytes.
327+
// Read language worker file to avoid disk reads during specialization. This is only to page-in bytes.
314328
File.ReadAllBytes(workerPath);
315329
}
316330
}

0 commit comments

Comments
 (0)