Skip to content

Commit c0219e6

Browse files
authored
Add IsDisabled property to worker description and skip if the value is True (#10231)
* Adding support to skip a worker description when none of the profile conditions are met. * Adding release notes. * Changes to switch to "IsDisabled" property on worker description. * missed a file
1 parent 8d4e9df commit c0219e6

File tree

10 files changed

+86
-0
lines changed

10 files changed

+86
-0
lines changed

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
- Fixed an issue leading to a race when invocation responses returned prior to HTTP requests being sent in proxied scenarios.
1616
- Language worker channels will not be started during placeholder mode if we are in-process (#10161)
1717
- Ordered invocations are now the default (#10201)
18+
- Skip worker description if none of the profile conditions are met (#9932)
1819
- Fixed incorrect function count in the log message.(#10220)

src/WebJobs.Script/Workers/ProcessManagement/WorkerDescription.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public abstract class WorkerDescription
4545
/// </summary>
4646
public abstract bool UseStdErrorStreamForErrorsOnly { get; set; }
4747

48+
/// <summary>
49+
/// Gets or sets a value indicating whether the worker description is disabled.
50+
/// </summary>
51+
public bool? IsDisabled { get; set; }
52+
4853
public abstract void ApplyDefaultsAndValidate(string workerDirectory, ILogger logger);
4954

5055
internal void ThrowIfFileNotExists(string inputFile, string paramName)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public RpcWorkerDescription ApplyProfile(RpcWorkerDescription defaultWorkerDescr
8181
updatedDescription.Extensions = UseProfileOrDefault(ProfileDescription.Extensions, defaultWorkerDescription.Extensions) as List<string>;
8282
updatedDescription.Language = UseProfileOrDefault(ProfileDescription.Language, defaultWorkerDescription.Language);
8383
updatedDescription.WorkerDirectory = UseProfileOrDefault(ProfileDescription.WorkerDirectory, defaultWorkerDescription.WorkerDirectory);
84+
updatedDescription.IsDisabled = ProfileDescription.IsDisabled ?? defaultWorkerDescription.IsDisabled ?? false;
8485
return updatedDescription;
8586
}
8687

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ internal void AddProvider(string workerDir)
151151
// Validate workerDescription
152152
workerDescription.ApplyDefaultsAndValidate(Directory.GetCurrentDirectory(), _logger);
153153

154+
if (workerDescription.IsDisabled == true)
155+
{
156+
_logger.LogInformation("Skipping WorkerConfig for stack: {language} since it is disabled.", workerDescription.Language);
157+
return;
158+
}
159+
154160
if (ShouldAddWorkerConfig(workerDescription.Language))
155161
{
156162
workerDescription.FormatWorkerPathIfNeeded(_systemRuntimeInformation, _environment, _logger);

test/WebJobs.Script.Tests/TestWorkers/worker1/1.bat

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"description": {
3+
"language": "foo",
4+
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/1.bat",
5+
"defaultWorkerPath": "1.bat"
6+
}
7+
}

test/WebJobs.Script.Tests/TestWorkers/worker2/2.bat

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"description": {
3+
"language": "bar"
4+
},
5+
"profiles": [
6+
{
7+
"profileName": "SpecificConditionProfile",
8+
"conditions": [
9+
{
10+
"conditionType": "environment",
11+
"conditionName": "NON_EXISTING_ENV_VAR",
12+
"conditionExpression": "(?i)true$"
13+
}
14+
],
15+
"description": {
16+
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/2.bat",
17+
"defaultWorkerPath": "2.bat"
18+
}
19+
},
20+
{
21+
"profileName": "FallbackProfileToDisableWorker",
22+
"conditions": [
23+
{
24+
"conditionType": "environment",
25+
"conditionName": "ENV_VAR_BAR",
26+
"conditionExpression": "(?i)true$"
27+
}
28+
],
29+
"description": {
30+
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/2.bat",
31+
"defaultWorkerPath": "2.bat",
32+
"isDisabled": true
33+
}
34+
}
35+
]
36+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<None Update="Microsoft.Azure.WebJobs.Script.WebHost.deps.json" CopyToOutputDirectory="PreserveNewest" />
7979
<None Update="TestFixture\HostOptionsProviderTests\*.json" CopyToOutputDirectory="PreserveNewest" />
8080
<None Update="Workers\Rpc\Resources\**" CopyToOutputDirectory="PreserveNewest" />
81+
<None Update="TestWorkers\**" CopyToOutputDirectory="PreserveNewest" />
8182
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
8283
<None Remove="Resources\FileProvisioning\PowerShell\*" />
8384
</ItemGroup>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ public void LanguageWorker_WorkersDir_NotSet()
9292
Assert.Equal(expectedWorkersDir, configFactory.WorkersDirPath);
9393
}
9494

95+
[Fact]
96+
public void WorkerDescription_Skipped_When_Profile_Disables_Worker()
97+
{
98+
// The "TestWorkers" directory has 2 workers: "worker1" and "worker2".
99+
// "worker2" has 2 profiles. The first profile will be skipped since the condition is not met.
100+
// The second profile will be applied since the condition is met. The second profile updates
101+
// the "IsDisabled" property to True and will cause the workerDescription to be skipped.
102+
103+
var testPath = Path.GetDirectoryName(new Uri(typeof(RpcWorkerConfigFactoryTests).Assembly.Location).LocalPath);
104+
var testWorkersDirectory = Path.Combine(testPath, "TestWorkers");
105+
var testEnvVariables = new Dictionary<string, string>
106+
{
107+
{ $"{RpcWorkerConstants.LanguageWorkersSectionName}:{WorkerConstants.WorkersDirectorySectionName}", testWorkersDirectory }
108+
};
109+
var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder()
110+
.AddInMemoryCollection(testEnvVariables);
111+
var config = configBuilder.Build();
112+
var scriptSettingsManager = new ScriptSettingsManager(config);
113+
var testLogger = new TestLogger("test");
114+
_testEnvironment.SetEnvironmentVariable("ENV_VAR_BAR", "True");
115+
var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger(), _testWorkerProfileManager);
116+
117+
var workerConfigs = configFactory.GetConfigs();
118+
119+
Assert.False(testLogger.GetLogMessages().Any(m => m.Exception != null), "There should not be an exception logged while executing GetConfigs method.");
120+
Assert.Equal(1, workerConfigs.Count);
121+
Assert.EndsWith("worker1\\1.bat", workerConfigs[0].Description.DefaultWorkerPath);
122+
}
123+
95124
[Fact]
96125
public void JavaPath_FromEnvVars()
97126
{

0 commit comments

Comments
 (0)