Skip to content

Commit

Permalink
Add IsDisabled property to worker description and skip if the value…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
kshyju committed Jun 19, 2024
1 parent 8d4e9df commit c0219e6
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
- Fixed an issue leading to a race when invocation responses returned prior to HTTP requests being sent in proxied scenarios.
- Language worker channels will not be started during placeholder mode if we are in-process (#10161)
- Ordered invocations are now the default (#10201)
- Skip worker description if none of the profile conditions are met (#9932)
- Fixed incorrect function count in the log message.(#10220)
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public abstract class WorkerDescription
/// </summary>
public abstract bool UseStdErrorStreamForErrorsOnly { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the worker description is disabled.
/// </summary>
public bool? IsDisabled { get; set; }

public abstract void ApplyDefaultsAndValidate(string workerDirectory, ILogger logger);

internal void ThrowIfFileNotExists(string inputFile, string paramName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public RpcWorkerDescription ApplyProfile(RpcWorkerDescription defaultWorkerDescr
updatedDescription.Extensions = UseProfileOrDefault(ProfileDescription.Extensions, defaultWorkerDescription.Extensions) as List<string>;
updatedDescription.Language = UseProfileOrDefault(ProfileDescription.Language, defaultWorkerDescription.Language);
updatedDescription.WorkerDirectory = UseProfileOrDefault(ProfileDescription.WorkerDirectory, defaultWorkerDescription.WorkerDirectory);
updatedDescription.IsDisabled = ProfileDescription.IsDisabled ?? defaultWorkerDescription.IsDisabled ?? false;
return updatedDescription;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ internal void AddProvider(string workerDir)
// Validate workerDescription
workerDescription.ApplyDefaultsAndValidate(Directory.GetCurrentDirectory(), _logger);

if (workerDescription.IsDisabled == true)
{
_logger.LogInformation("Skipping WorkerConfig for stack: {language} since it is disabled.", workerDescription.Language);
return;
}

if (ShouldAddWorkerConfig(workerDescription.Language))
{
workerDescription.FormatWorkerPathIfNeeded(_systemRuntimeInformation, _environment, _logger);
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": {
"language": "foo",
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/1.bat",
"defaultWorkerPath": "1.bat"
}
}
Empty file.
36 changes: 36 additions & 0 deletions test/WebJobs.Script.Tests/TestWorkers/worker2/worker.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"description": {
"language": "bar"
},
"profiles": [
{
"profileName": "SpecificConditionProfile",
"conditions": [
{
"conditionType": "environment",
"conditionName": "NON_EXISTING_ENV_VAR",
"conditionExpression": "(?i)true$"
}
],
"description": {
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/2.bat",
"defaultWorkerPath": "2.bat"
}
},
{
"profileName": "FallbackProfileToDisableWorker",
"conditions": [
{
"conditionType": "environment",
"conditionName": "ENV_VAR_BAR",
"conditionExpression": "(?i)true$"
}
],
"description": {
"defaultExecutablePath": "%FUNCTIONS_WORKER_DIRECTORY%/2.bat",
"defaultWorkerPath": "2.bat",
"isDisabled": true
}
}
]
}
1 change: 1 addition & 0 deletions test/WebJobs.Script.Tests/WebJobs.Script.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<None Update="Microsoft.Azure.WebJobs.Script.WebHost.deps.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="TestFixture\HostOptionsProviderTests\*.json" CopyToOutputDirectory="PreserveNewest" />
<None Update="Workers\Rpc\Resources\**" CopyToOutputDirectory="PreserveNewest" />
<None Update="TestWorkers\**" CopyToOutputDirectory="PreserveNewest" />
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
<None Remove="Resources\FileProvisioning\PowerShell\*" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ public void LanguageWorker_WorkersDir_NotSet()
Assert.Equal(expectedWorkersDir, configFactory.WorkersDirPath);
}

[Fact]
public void WorkerDescription_Skipped_When_Profile_Disables_Worker()
{
// The "TestWorkers" directory has 2 workers: "worker1" and "worker2".
// "worker2" has 2 profiles. The first profile will be skipped since the condition is not met.
// The second profile will be applied since the condition is met. The second profile updates
// the "IsDisabled" property to True and will cause the workerDescription to be skipped.

var testPath = Path.GetDirectoryName(new Uri(typeof(RpcWorkerConfigFactoryTests).Assembly.Location).LocalPath);
var testWorkersDirectory = Path.Combine(testPath, "TestWorkers");
var testEnvVariables = new Dictionary<string, string>
{
{ $"{RpcWorkerConstants.LanguageWorkersSectionName}:{WorkerConstants.WorkersDirectorySectionName}", testWorkersDirectory }
};
var configBuilder = ScriptSettingsManager.CreateDefaultConfigurationBuilder()
.AddInMemoryCollection(testEnvVariables);
var config = configBuilder.Build();
var scriptSettingsManager = new ScriptSettingsManager(config);
var testLogger = new TestLogger("test");
_testEnvironment.SetEnvironmentVariable("ENV_VAR_BAR", "True");
var configFactory = new RpcWorkerConfigFactory(config, testLogger, _testSysRuntimeInfo, _testEnvironment, new TestMetricsLogger(), _testWorkerProfileManager);

var workerConfigs = configFactory.GetConfigs();

Assert.False(testLogger.GetLogMessages().Any(m => m.Exception != null), "There should not be an exception logged while executing GetConfigs method.");
Assert.Equal(1, workerConfigs.Count);
Assert.EndsWith("worker1\\1.bat", workerConfigs[0].Description.DefaultWorkerPath);
}

[Fact]
public void JavaPath_FromEnvVars()
{
Expand Down

0 comments on commit c0219e6

Please sign in to comment.