-
Notifications
You must be signed in to change notification settings - Fork 464
Refactor code to move the current logic to search for WorkerConfigs to a default worker configuration resolver #11229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
5cf51b1
e644d3c
6dfc898
1c98a16
f20fa53
b0dc43f
ad13ac2
a566a9f
76ccdc9
0de1312
b036344
190531d
a484c96
ad6a445
6317c0b
0daca28
c53f0fc
95bf08f
70040bd
b80e363
7324929
ec1929d
c816553
fe19a38
2726cc7
92ec61b
2da1eb5
0ee0871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO.Abstractions; | ||
using Microsoft.Azure.WebJobs.Script.Diagnostics.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Workers.Rpc.Configuration | ||
{ | ||
// This class resolves worker configurations by scanning the "workers" directory within the Host for worker config files. | ||
internal sealed class DefaultWorkerConfigurationResolver : IWorkerConfigurationResolver | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IOptionsMonitor<WorkerConfigurationResolverOptions> _workerConfigurationResolverOptions; | ||
private readonly IFileSystem _fileSystem; | ||
|
||
public DefaultWorkerConfigurationResolver(ILoggerFactory loggerFactory, | ||
IFileSystem fileSystem, | ||
IOptionsMonitor<WorkerConfigurationResolverOptions> workerConfigurationResolverOptions) | ||
{ | ||
ArgumentNullException.ThrowIfNull(loggerFactory); | ||
_logger = loggerFactory.CreateLogger(ScriptConstants.LogCategoryWorkerConfig); | ||
_workerConfigurationResolverOptions = workerConfigurationResolverOptions ?? throw new ArgumentNullException(nameof(workerConfigurationResolverOptions)); | ||
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); | ||
} | ||
|
||
public WorkerConfigurationInfo GetConfigurationInfo() | ||
{ | ||
surgupta-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var workersRootDirPath = _workerConfigurationResolverOptions.CurrentValue.WorkersRootDirPath; | ||
_logger.DefaultWorkersDirectoryPath(workersRootDirPath); | ||
|
||
var workerConfigPaths = new List<string>(); | ||
|
||
foreach (var workerDir in _fileSystem.Directory.EnumerateDirectories(workersRootDirPath)) | ||
{ | ||
string workerConfigPath = _fileSystem.Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName); | ||
|
||
if (_fileSystem.File.Exists(workerConfigPath)) | ||
{ | ||
workerConfigPaths.Add(workerDir); | ||
} | ||
} | ||
|
||
return new WorkerConfigurationInfo(_workerConfigurationResolverOptions.CurrentValue.WorkersRootDirPath, workerConfigPaths); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||
using System.Threading.Tasks; | ||||
using Microsoft.Azure.WebJobs.Script.Diagnostics; | ||||
using Microsoft.Azure.WebJobs.Script.Workers.Profiles; | ||||
using Microsoft.Azure.WebJobs.Script.Workers.Rpc.Configuration; | ||||
using Microsoft.Extensions.Configuration; | ||||
using Microsoft.Extensions.Logging; | ||||
|
||||
|
@@ -26,6 +27,7 @@ internal class RpcWorkerConfigFactory | |||
private readonly IMetricsLogger _metricsLogger; | ||||
private readonly string _workerRuntime; | ||||
private readonly IEnvironment _environment; | ||||
private readonly IWorkerConfigurationResolver _workerConfigurationResolver; | ||||
private readonly JsonSerializerOptions _jsonSerializerOptions = new() | ||||
{ | ||||
PropertyNameCaseInsensitive = true | ||||
|
@@ -38,7 +40,8 @@ public RpcWorkerConfigFactory(IConfiguration config, | |||
ISystemRuntimeInformation systemRuntimeInfo, | ||||
IEnvironment environment, | ||||
IMetricsLogger metricsLogger, | ||||
IWorkerProfileManager workerProfileManager) | ||||
IWorkerProfileManager workerProfileManager, | ||||
IWorkerConfigurationResolver workerConfigurationResolver) | ||||
{ | ||||
_config = config ?? throw new ArgumentNullException(nameof(config)); | ||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||||
|
@@ -47,17 +50,10 @@ public RpcWorkerConfigFactory(IConfiguration config, | |||
_metricsLogger = metricsLogger ?? throw new ArgumentNullException(nameof(metricsLogger)); | ||||
_profileManager = workerProfileManager ?? throw new ArgumentNullException(nameof(workerProfileManager)); | ||||
_workerRuntime = _environment.GetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName); | ||||
|
||||
WorkersDirPath = GetDefaultWorkersDirectory(Directory.Exists); | ||||
var workersDirectorySection = _config.GetSection($"{RpcWorkerConstants.LanguageWorkersSectionName}:{WorkerConstants.WorkersDirectorySectionName}"); | ||||
|
||||
if (!string.IsNullOrEmpty(workersDirectorySection.Value)) | ||||
{ | ||||
WorkersDirPath = workersDirectorySection.Value; | ||||
} | ||||
_workerConfigurationResolver = workerConfigurationResolver ?? throw new ArgumentNullException(nameof(workerConfigurationResolver)); | ||||
} | ||||
|
||||
public string WorkersDirPath { get; } | ||||
public string WorkersDirPath { get; private set; } | ||||
|
||||
public IList<RpcWorkerConfig> GetConfigs() | ||||
{ | ||||
|
@@ -68,20 +64,6 @@ public IList<RpcWorkerConfig> GetConfigs() | |||
} | ||||
} | ||||
|
||||
internal static string GetDefaultWorkersDirectory(Func<string, bool> directoryExists) | ||||
{ | ||||
#pragma warning disable SYSLIB0012 // Type or member is obsolete | ||||
string assemblyLocalPath = Path.GetDirectoryName(new Uri(typeof(RpcWorkerConfigFactory).Assembly.CodeBase).LocalPath); | ||||
#pragma warning restore SYSLIB0012 // Type or member is obsolete | ||||
string workersDirPath = Path.Combine(assemblyLocalPath, RpcWorkerConstants.DefaultWorkersDirectoryName); | ||||
if (!directoryExists(workersDirPath)) | ||||
{ | ||||
// Site Extension. Default to parent directory | ||||
workersDirPath = Path.Combine(Directory.GetParent(assemblyLocalPath).FullName, RpcWorkerConstants.DefaultWorkersDirectoryName); | ||||
} | ||||
return workersDirPath; | ||||
} | ||||
|
||||
internal void BuildWorkerProviderDictionary() | ||||
{ | ||||
AddProviders(); | ||||
|
@@ -90,15 +72,13 @@ internal void BuildWorkerProviderDictionary() | |||
|
||||
internal void AddProviders() | ||||
{ | ||||
_logger.LogDebug("Workers Directory set to: {WorkersDirPath}", WorkersDirPath); | ||||
var workerConfigurationInfo = _workerConfigurationResolver.GetConfigurationInfo(); | ||||
var workerConfigs = workerConfigurationInfo.WorkerConfigPaths; | ||||
WorkersDirPath = workerConfigurationInfo.WorkersRootDirPath; | ||||
|
||||
foreach (var workerDir in Directory.EnumerateDirectories(WorkersDirPath)) | ||||
foreach (var workerConfig in workerConfigs) | ||||
{ | ||||
string workerConfigPath = Path.Combine(workerDir, RpcWorkerConstants.WorkerConfigFileName); | ||||
if (File.Exists(workerConfigPath)) | ||||
{ | ||||
AddProvider(workerDir); | ||||
} | ||||
AddProvider(workerConfig); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass the root worker dir path into this method instead of storing it in a field. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is another method in this class azure-functions-host/src/WebJobs.Script/Workers/Rpc/Configuration/RpcWorkerConfigFactory.cs Line 60 in f6cdf90
|
||||
} | ||||
} | ||||
|
||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Workers.Rpc.Configuration | ||
{ | ||
internal record WorkerConfigurationInfo(string WorkersRootDirPath, IReadOnlyList<string> WorkerConfigPaths); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Azure.WebJobs.Hosting; | ||
|
||
namespace Microsoft.Azure.WebJobs.Script.Workers.Rpc.Configuration | ||
{ | ||
public sealed class WorkerConfigurationResolverOptions : IOptionsFormatter | ||
{ | ||
/// <summary> | ||
/// Gets or sets the workers directory path within the Host or defined by IConfiguration. | ||
/// </summary> | ||
public string WorkersRootDirPath { get; set; } | ||
|
||
/// <inheritdoc> | ||
/// Implements the Format method from IOptionsFormatter interface. | ||
/// </inheritdoc> | ||
public string Format() | ||
{ | ||
return JsonSerializer.Serialize(this, typeof(WorkerConfigurationResolverOptions), ConfigResolverOptionsJsonSerializerContext.Default); | ||
} | ||
} | ||
|
||
[JsonSourceGenerationOptions(WriteIndented = true, GenerationMode = JsonSourceGenerationMode.Serialization)] | ||
[JsonSerializable(typeof(WorkerConfigurationResolverOptions))] | ||
internal partial class ConfigResolverOptionsJsonSerializerContext : JsonSerializerContext; | ||
surgupta-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.