Skip to content

Commit 496f692

Browse files
Placeholder for Worker Directory Path (#8473)
Added log for formatting arguments
1 parent 7ba2b73 commit 496f692

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ internal void AddProvider(string workerDir)
130130
if (ShouldAddWorkerConfig(workerDescription.Language))
131131
{
132132
workerDescription.FormatWorkerPathIfNeeded(_systemRuntimeInformation, _environment, _logger);
133+
workerDescription.FormatArgumentsIfNeeded(_logger);
133134
workerDescription.ThrowIfFileNotExists(workerDescription.DefaultWorkerPath, nameof(workerDescription.DefaultWorkerPath));
134135
workerDescription.ExpandEnvironmentVariables();
135136

@@ -246,4 +247,4 @@ internal void ReadLanguageWorkerFile(string workerPath)
246247
}
247248
}
248249
}
249-
}
250+
}

src/WebJobs.Script/Workers/Rpc/RpcWorkerConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static class RpcWorkerConstants
3333
public const string OSPlaceholder = "{os}";
3434
public const string ArchitecturePlaceholder = "{architecture}";
3535
public const string RuntimeVersionPlaceholder = "%" + FunctionWorkerRuntimeVersionSettingName + "%";
36+
public const string WorkerDirectoryPath = "{workerDirectoryPath}";
3637

3738
// Rpc message length
3839
public const int DefaultMaxMessageLengthBytes = int.MaxValue;

src/WebJobs.Script/Workers/Rpc/RpcWorkerDescription.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ internal void FormatWorkerPathIfNeeded(ISystemRuntimeInformation systemRuntimeIn
203203
.Replace(RpcWorkerConstants.RuntimeVersionPlaceholder, DefaultRuntimeVersion);
204204
}
205205

206+
internal void FormatArgumentsIfNeeded(ILogger logger)
207+
{
208+
if (Arguments?.Any() == false)
209+
{
210+
return;
211+
}
212+
213+
for (int i = 0; i < Arguments.Count; i++)
214+
{
215+
if (!string.IsNullOrEmpty(Arguments[i]))
216+
{
217+
Arguments[i] = Arguments[i].Replace(RpcWorkerConstants.WorkerDirectoryPath, WorkerDirectory);
218+
}
219+
}
220+
221+
logger.LogDebug($"Worker description arguments after formatting: {Arguments}");
222+
}
223+
206224
internal bool ShouldFormatWorkerPath(string workerPath)
207225
{
208226
if (string.IsNullOrEmpty(workerPath))

test/WebJobs.Script.Tests/Workers/Rpc/RpcWorkerConfigTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,58 @@ public void LanguageWorker_FormatWorkerPath_DefualtRuntimeVersion_WorkerRuntimeM
615615
Assert.Equal("3.7", workerDescription.DefaultRuntimeVersion);
616616
}
617617

618+
public static IEnumerable<object[]> RpcWorkerDescriptionArgumentsWithPlaceholder()
619+
{
620+
yield return new object[] { "D:/Code/Host/workers/java", new List<string> { RpcWorkerConstants.WorkerDirectoryPath } };
621+
yield return new object[] { "/java", new List<string> { string.Concat("/path/version/", RpcWorkerConstants.WorkerDirectoryPath) } };
622+
yield return new object[] { "/", new List<string> { string.Concat("version/", RpcWorkerConstants.WorkerDirectoryPath) } };
623+
}
624+
625+
[Theory]
626+
[MemberData(nameof(RpcWorkerDescriptionArgumentsWithPlaceholder))]
627+
public void LanguageWorker_FormatArguments_ReplacePlaceholder(string workerDirectory, List<string> arguments)
628+
{
629+
RpcWorkerDescription workerDescription = new RpcWorkerDescription()
630+
{
631+
Arguments = arguments,
632+
DefaultExecutablePath = "python",
633+
WorkerDirectory = workerDirectory,
634+
Language = "python"
635+
};
636+
workerDescription.FormatArgumentsIfNeeded(new TestLogger(testLanguage));
637+
638+
for (int i = 0; i < arguments.Count; i++)
639+
{
640+
Assert.Contains(workerDirectory, workerDescription.Arguments[i]);
641+
}
642+
}
643+
644+
public static IEnumerable<object[]> RpcWorkerDescriptionArgumentsWithoutPlaceholder()
645+
{
646+
yield return new object[] { "D:/Code/Host/workers/java", new List<string> { } };
647+
yield return new object[] { "D:/Code/Host/workers/", new List<string> { string.Empty, null } };
648+
yield return new object[] { "/worker/path", new List<string> { string.Empty, null, "/path/version/" } };
649+
}
650+
651+
[Theory]
652+
[MemberData(nameof(RpcWorkerDescriptionArgumentsWithoutPlaceholder))]
653+
public void LanguageWorker_FormatArguments_DoNotReplacePlaceholder(string workerDirectory, List<string> arguments)
654+
{
655+
RpcWorkerDescription workerDescription = new RpcWorkerDescription()
656+
{
657+
Arguments = arguments,
658+
DefaultExecutablePath = "python",
659+
WorkerDirectory = workerDirectory,
660+
Language = "python"
661+
};
662+
workerDescription.FormatArgumentsIfNeeded(new TestLogger(testLanguage));
663+
664+
for (int i = 0; i < arguments.Count; i++)
665+
{
666+
Assert.DoesNotContain(workerDirectory, workerDescription.Arguments[i]);
667+
}
668+
}
669+
618670
private IEnumerable<RpcWorkerConfig> TestReadWorkerProviderFromConfig(IEnumerable<TestRpcWorkerConfig> configs, ILogger testLogger, TestMetricsLogger testMetricsLogger, string language = null, Dictionary<string, string> keyValuePairs = null, bool appSvcEnv = false)
619671
{
620672
Mock<IEnvironment> mockEnvironment = new Mock<IEnvironment>();

0 commit comments

Comments
 (0)