forked from Azure/azure-functions-host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerConfigFactory.cs
220 lines (201 loc) · 11 KB
/
WorkerConfigFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Azure.WebJobs.Script.Abstractions;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
namespace Microsoft.Azure.WebJobs.Script.Rpc
{
// Gets fully configured WorkerConfigs from IWorkerProviders
internal class WorkerConfigFactory
{
private readonly IConfiguration _config;
private readonly ILogger _logger;
private Dictionary<string, IWorkerProvider> _workerProviderDictionary = new Dictionary<string, IWorkerProvider>();
public WorkerConfigFactory(IConfiguration config, ILogger logger)
{
_config = config;
_logger = logger;
WorkersDirPath = Path.Combine(Path.GetDirectoryName(new Uri(typeof(WorkerConfigFactory).Assembly.CodeBase).LocalPath), LanguageWorkerConstants.DefaultWorkersDirectoryName);
var workersDirectorySection = _config.GetSection($"{LanguageWorkerConstants.LanguageWorkersSectionName}:{LanguageWorkerConstants.WorkersDirectorySectionName}");
if (!string.IsNullOrEmpty(workersDirectorySection.Value))
{
WorkersDirPath = workersDirectorySection.Value;
}
}
public string WorkersDirPath { get; }
public IEnumerable<WorkerConfig> GetConfigs(IEnumerable<IWorkerProvider> providers)
{
foreach (var provider in providers)
{
var description = provider.GetDescription();
_logger.LogTrace($"Worker path for language worker {description.Language}: {description.WorkerDirectory}");
var arguments = new WorkerProcessArguments()
{
ExecutablePath = description.DefaultExecutablePath,
WorkerPath = description.GetWorkerPath()
};
if (description.Language.Equals(LanguageWorkerConstants.JavaLanguageWorkerName))
{
arguments.ExecutablePath = GetExecutablePathForJava(description.DefaultExecutablePath);
}
if (provider.TryConfigureArguments(arguments, _config, _logger))
{
yield return new WorkerConfig()
{
Description = description,
Arguments = arguments
};
}
else
{
_logger.LogError($"Could not configure language worker {description.Language}.");
}
}
}
public List<IWorkerProvider> GetWorkerProviders(ILogger logger, ScriptSettingsManager settingsManager = null, string language = null)
{
AddProviders(logger, language);
AddProvidersFromAppSettings(logger);
return _workerProviderDictionary.Values.ToList();
}
internal void AddProviders(ILogger logger, string language = null)
{
var providers = new List<IWorkerProvider>();
logger.LogTrace($"Workers Directory set to: {WorkersDirPath}");
if (!string.IsNullOrEmpty(language))
{
logger.LogInformation($"Reading Worker config for the language: {language}");
AddProvider(Path.Combine(WorkersDirPath, language), logger);
}
else
{
logger.LogTrace($"Loading worker providers from the workers directory: {WorkersDirPath}");
foreach (var workerDir in Directory.EnumerateDirectories(WorkersDirPath))
{
string workerConfigPath = Path.Combine(workerDir, LanguageWorkerConstants.WorkerConfigFileName);
if (File.Exists(workerConfigPath))
{
AddProvider(workerDir, logger);
}
}
}
}
internal void AddProvidersFromAppSettings(ILogger logger)
{
var languagesSection = _config.GetSection($"{LanguageWorkerConstants.LanguageWorkersSectionName}");
foreach (var languageSection in languagesSection.GetChildren())
{
var workerDirectorySection = languageSection.GetSection(LanguageWorkerConstants.WorkerDirectorySectionName);
if (workerDirectorySection.Value != null)
{
_workerProviderDictionary.Remove(languageSection.Key);
AddProvider(Path.Combine(workerDirectorySection.Value, languageSection.Key), logger);
}
}
}
internal void AddProvider(string workerDir, ILogger logger)
{
try
{
Dictionary<string, WorkerDescription> descriptionProfiles = new Dictionary<string, WorkerDescription>();
string workerConfigPath = Path.Combine(workerDir, LanguageWorkerConstants.WorkerConfigFileName);
if (!File.Exists(workerConfigPath))
{
logger.LogTrace($"Did not find worker config file at: {workerConfigPath}");
return;
}
logger.LogTrace($"Found worker config: {workerConfigPath}");
string json = File.ReadAllText(workerConfigPath);
JObject workerConfig = JObject.Parse(json);
WorkerDescription workerDescription = workerConfig.Property(LanguageWorkerConstants.WorkerDescription).Value.ToObject<WorkerDescription>();
workerDescription.WorkerDirectory = workerDir;
var languageSection = _config.GetSection($"{LanguageWorkerConstants.LanguageWorkersSectionName}:{workerDescription.Language}");
workerDescription.Arguments = workerDescription.Arguments ?? new List<string>();
descriptionProfiles = GetWorkerDescriptionProfiles(workerConfig);
if (ScriptSettingsManager.Instance.IsAppServiceEnvironment)
{
//Overwrite default Description with AppServiceEnv profile
workerDescription = GetWorkerDescriptionFromProfiles(LanguageWorkerConstants.WorkerDescriptionAppServiceEnvProfileName, descriptionProfiles, workerDescription);
}
GetDefaultExecutablePathFromAppSettings(workerDescription, languageSection);
AddArgumentsFromAppSettings(workerDescription, languageSection);
if (File.Exists(workerDescription.GetWorkerPath()))
{
logger.LogTrace($"Will load worker provider for language: {workerDescription.Language}");
_workerProviderDictionary[workerDescription.Language] = new GenericWorkerProvider(workerDescription, workerDir);
}
else
{
throw new FileNotFoundException($"Did not find worker for for language: {workerDescription.Language}", workerDescription.GetWorkerPath());
}
}
catch (Exception ex)
{
logger?.LogError(ex, $"Failed to initialize worker provider for: {workerDir}");
}
}
private static Dictionary<string, WorkerDescription> GetWorkerDescriptionProfiles(JObject workerConfig)
{
Dictionary<string, WorkerDescription> descriptionProfiles = new Dictionary<string, WorkerDescription>();
var profiles = workerConfig.Property("profiles")?.Value.ToObject<JObject>();
if (profiles != null)
{
foreach (var profile in profiles)
{
string name = profile.Key;
JToken value = profile.Value;
WorkerDescription description = profile.Value.ToObject<WorkerDescription>();
descriptionProfiles.Add(name, description);
}
}
return descriptionProfiles;
}
private static WorkerDescription GetWorkerDescriptionFromProfiles(string key, Dictionary<string, WorkerDescription> descriptionProfiles, WorkerDescription defaultWorkerDescription)
{
WorkerDescription profileDescription = null;
if (descriptionProfiles.TryGetValue(key, out profileDescription))
{
profileDescription.Arguments = profileDescription.Arguments?.Count > 0 ? profileDescription.Arguments : defaultWorkerDescription.Arguments;
profileDescription.DefaultExecutablePath = string.IsNullOrEmpty(profileDescription.DefaultExecutablePath) ? defaultWorkerDescription.DefaultExecutablePath : profileDescription.DefaultExecutablePath;
profileDescription.DefaultWorkerPath = string.IsNullOrEmpty(profileDescription.DefaultWorkerPath) ? defaultWorkerDescription.DefaultWorkerPath : profileDescription.DefaultWorkerPath;
profileDescription.Extension = string.IsNullOrEmpty(profileDescription.Extension) ? defaultWorkerDescription.Extension : profileDescription.Extension;
profileDescription.Language = string.IsNullOrEmpty(profileDescription.Language) ? defaultWorkerDescription.Language : profileDescription.Language;
profileDescription.WorkerDirectory = string.IsNullOrEmpty(profileDescription.WorkerDirectory) ? defaultWorkerDescription.WorkerDirectory : profileDescription.WorkerDirectory;
return profileDescription;
}
return defaultWorkerDescription;
}
private static void GetDefaultExecutablePathFromAppSettings(WorkerDescription workerDescription, IConfigurationSection languageSection)
{
var defaultExecutablePath = languageSection.GetSection($"{LanguageWorkerConstants.WorkerDescriptionDefaultExecutablePath}");
workerDescription.DefaultExecutablePath = defaultExecutablePath.Value != null ? defaultExecutablePath.Value : workerDescription.DefaultExecutablePath;
}
private static void AddArgumentsFromAppSettings(WorkerDescription workerDescription, IConfigurationSection languageSection)
{
var argumentsSection = languageSection.GetSection($"{LanguageWorkerConstants.WorkerDescriptionArguments}");
if (argumentsSection.Value != null)
{
workerDescription.Arguments.AddRange(Regex.Split(argumentsSection.Value, @"\s+"));
}
}
internal string GetExecutablePathForJava(string defaultExecutablePath)
{
string javaHome = ScriptSettingsManager.Instance.GetSetting("JAVA_HOME");
if (string.IsNullOrEmpty(javaHome))
{
return defaultExecutablePath;
}
else
{
return Path.GetFullPath(Path.Combine(javaHome, "bin", defaultExecutablePath));
}
}
}
}