Skip to content

Commit 49738c7

Browse files
Update active profile when multiple runtimes use worker description profiles (#9084)
* Handle multiple worker runtime profiles * Adding unit tests * Review comments
1 parent 7a93a92 commit 49738c7

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ internal class WorkerProfileManager : IWorkerProfileManager
1818
private readonly IEnvironment _environment;
1919
private readonly IEnumerable<IWorkerProfileConditionProvider> _conditionProviders;
2020

21-
private string _activeProfile = string.Empty;
21+
private Dictionary<string, string> _activeProfiles;
2222
private Dictionary<string, List<WorkerDescriptionProfile>> _profiles;
2323

2424
public WorkerProfileManager(ILogger<WorkerProfileManager> logger, IEnvironment environment)
2525
{
2626
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
2727
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2828

29+
_activeProfiles = new Dictionary<string, string>();
2930
_profiles = new Dictionary<string, List<WorkerDescriptionProfile>>();
3031
_conditionProviders = new List<IWorkerProfileConditionProvider>
3132
{
@@ -63,7 +64,7 @@ public void LoadWorkerDescriptionFromProfiles(RpcWorkerDescription defaultWorker
6364
if (GetEvaluatedProfile(defaultWorkerDescription.Language, out WorkerDescriptionProfile profile))
6465
{
6566
_logger?.LogInformation($"Worker initialized with profile - {profile.Name}, Profile ID {profile.ProfileId} from worker config.");
66-
_activeProfile = profile.ProfileId;
67+
_activeProfiles[defaultWorkerDescription.Language] = profile.ProfileId;
6768
workerDescription = profile.ApplyProfile(defaultWorkerDescription);
6869
}
6970
else
@@ -97,7 +98,13 @@ public bool IsCorrectProfileLoaded(string workerRuntime)
9798
{
9899
profileId = profile.ProfileId;
99100
}
100-
return _activeProfile.Equals(profileId);
101+
102+
string activeProfile;
103+
if (!_activeProfiles.TryGetValue(workerRuntime, out activeProfile))
104+
{
105+
activeProfile = string.Empty;
106+
}
107+
return activeProfile.Equals(profileId);
101108
}
102109
}
103110
}

test/WebJobs.Script.Tests/Workers/Profiles/WorkerProfileManagerTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,81 @@ public void IsCorrectProfileLoaded_ConditionChange_ReturnsFalse()
116116
Assert.False(profileManager.IsCorrectProfileLoaded("java"));
117117
}
118118

119+
[Fact]
120+
public void IsCorrectProfileLoaded_DifferentRuntimeNoProfile_ReturnsTrue()
121+
{
122+
var argumentList = new string[] { "-TestArg=1" };
123+
var description = RpcWorkerConfigTestUtilities.GetTestDefaultWorkerDescription("java", argumentList);
124+
var profiles = WorkerDescriptionProfileData("java", description);
125+
126+
_testEnvironment.SetEnvironmentVariable("APPLICATIONINSIGHTS_ENABLE_AGENT", "true");
127+
128+
WorkerProfileManager profileManager = new(_testLogger, _testEnvironment);
129+
profileManager.SetWorkerDescriptionProfiles(profiles, "java");
130+
profileManager.LoadWorkerDescriptionFromProfiles(description, out var workerDescription);
131+
132+
// Same profile should load as we didn't change any condition outcomes
133+
Assert.True(profileManager.IsCorrectProfileLoaded("java"));
134+
135+
// Different runtime without profiles
136+
Assert.True(profileManager.IsCorrectProfileLoaded("dotnet"));
137+
}
138+
139+
[Fact]
140+
public void IsCorrectProfileLoaded_DifferentRuntimeWithProfile_ReturnsTrue()
141+
{
142+
var argumentList = new string[] { "-TestArg=1" };
143+
var description = RpcWorkerConfigTestUtilities.GetTestDefaultWorkerDescription("java", argumentList);
144+
var profiles = WorkerDescriptionProfileData("java", description);
145+
146+
_testEnvironment.SetEnvironmentVariable("APPLICATIONINSIGHTS_ENABLE_AGENT", "true");
147+
148+
WorkerProfileManager profileManager = new(_testLogger, _testEnvironment);
149+
profileManager.SetWorkerDescriptionProfiles(profiles, "java");
150+
profileManager.LoadWorkerDescriptionFromProfiles(description, out var javaWorkerDescription);
151+
152+
var dotnetArgumentList = new string[] { "-TestArg=2" };
153+
var dotnetDescription = RpcWorkerConfigTestUtilities.GetTestDefaultWorkerDescription("dotnet", dotnetArgumentList);
154+
var dotnetProfiles = WorkerDescriptionProfileData("dotnet", dotnetDescription);
155+
156+
profileManager.SetWorkerDescriptionProfiles(dotnetProfiles, "dotnet");
157+
profileManager.LoadWorkerDescriptionFromProfiles(dotnetDescription, out var dotnetWorkerDescription);
158+
159+
// Same profile should load as we didn't change any condition outcomes
160+
Assert.True(profileManager.IsCorrectProfileLoaded("java"));
161+
162+
// Different runtime also loads same profile as we didn't change any condition outcomes
163+
Assert.True(profileManager.IsCorrectProfileLoaded("dotnet"));
164+
}
165+
166+
[Fact]
167+
public void IsCorrectProfileLoaded_DifferentRuntimeWithProfile_ReturnsFalse()
168+
{
169+
var argumentList = new string[] { "-TestArg=1" };
170+
var description = RpcWorkerConfigTestUtilities.GetTestDefaultWorkerDescription("java", argumentList);
171+
var profiles = WorkerDescriptionProfileData("java", description);
172+
173+
_testEnvironment.SetEnvironmentVariable("APPLICATIONINSIGHTS_ENABLE_AGENT", "true");
174+
175+
WorkerProfileManager profileManager = new(_testLogger, _testEnvironment);
176+
profileManager.SetWorkerDescriptionProfiles(profiles, "java");
177+
profileManager.LoadWorkerDescriptionFromProfiles(description, out var javaWorkerDescription);
178+
179+
var dotnetArgumentList = new string[] { "-TestArg=2" };
180+
var dotnetDescription = RpcWorkerConfigTestUtilities.GetTestDefaultWorkerDescription("dotnet", dotnetArgumentList);
181+
var dotnetProfiles = WorkerDescriptionProfileData("dotnet", dotnetDescription);
182+
183+
profileManager.SetWorkerDescriptionProfiles(dotnetProfiles, "dotnet");
184+
profileManager.LoadWorkerDescriptionFromProfiles(dotnetDescription, out var dotnetWorkerDescription);
185+
186+
// Same profile should load as we didn't change any condition outcomes
187+
Assert.True(profileManager.IsCorrectProfileLoaded("java"));
188+
189+
// Changing the condition so a different profile evaluates to true
190+
_testEnvironment.SetEnvironmentVariable("APPLICATIONINSIGHTS_ENABLE_AGENT", "false");
191+
Assert.False(profileManager.IsCorrectProfileLoaded("dotnet"));
192+
}
193+
119194
public static List<WorkerDescriptionProfile> WorkerDescriptionProfileData(string language, RpcWorkerDescription description)
120195
{
121196
var conditionLogger = new TestLogger("ConditionLogger");

0 commit comments

Comments
 (0)