-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Prefer _migrated config on startup #3853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TingluoHuang
merged 5 commits into
actions:main
from
lokesh755:lokesh755-use-migrated-runner
May 12, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
109507e
Prefer _migrated config on startup
lokesh755 7ffac6a
Retry RunnerAsync on config update
lokesh755 0742c36
restart session only for runner type config refresh
lokesh755 4fbc642
add comment for additional context
lokesh755 6a2b31d
restart session only if the job dispatcher is not busy
lokesh755 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -325,7 +325,7 @@ public async Task<int> ExecuteCommand(CommandSettings command) | |
| } | ||
|
|
||
| // Run the runner interactively or as service | ||
| return await RunAsync(settings, command.RunOnce || settings.Ephemeral); | ||
| return await ExecuteRunnerAsync(settings, command.RunOnce || settings.Ephemeral); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -387,12 +387,12 @@ private void CtrlCHandler(object sender, EventArgs e) | |
| } | ||
| } | ||
|
|
||
| private IMessageListener GetMessageListener(RunnerSettings settings) | ||
| private IMessageListener GetMessageListener(RunnerSettings settings, bool isMigratedSettings = false) | ||
| { | ||
| if (settings.UseV2Flow) | ||
| { | ||
| Trace.Info($"Using BrokerMessageListener"); | ||
| var brokerListener = new BrokerMessageListener(); | ||
| var brokerListener = new BrokerMessageListener(settings, isMigratedSettings); | ||
| brokerListener.Initialize(HostContext); | ||
| return brokerListener; | ||
| } | ||
|
|
@@ -406,15 +406,65 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false) | |
| try | ||
| { | ||
| Trace.Info(nameof(RunAsync)); | ||
| _listener = GetMessageListener(settings); | ||
| CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken); | ||
| if (createSessionResult == CreateSessionResult.SessionConflict) | ||
|
|
||
| // First try using migrated settings if available | ||
| var configManager = HostContext.GetService<IConfigurationManager>(); | ||
| RunnerSettings migratedSettings = null; | ||
|
|
||
| try | ||
| { | ||
| migratedSettings = configManager.LoadMigratedSettings(); | ||
| Trace.Info("Loaded migrated settings from .runner_migrated file"); | ||
| Trace.Info(migratedSettings); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Constants.Runner.ReturnCode.SessionConflict; | ||
| // If migrated settings file doesn't exist or can't be loaded, we'll use the provided settings | ||
| Trace.Info($"Failed to load migrated settings: {ex.Message}"); | ||
TingluoHuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| else if (createSessionResult == CreateSessionResult.Failure) | ||
|
|
||
| bool usedMigratedSettings = false; | ||
|
|
||
| if (migratedSettings != null) | ||
| { | ||
| return Constants.Runner.ReturnCode.TerminatedError; | ||
| // Try to create session with migrated settings first | ||
| Trace.Info("Attempting to create session using migrated settings"); | ||
| _listener = GetMessageListener(migratedSettings, isMigratedSettings: true); | ||
|
|
||
| try | ||
| { | ||
| CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to report telemetry back to service in case of any failure, so we know customers are having issue with migration. |
||
| if (createSessionResult == CreateSessionResult.Success) | ||
| { | ||
| Trace.Info("Successfully created session with migrated settings"); | ||
| settings = migratedSettings; // Use migrated settings for the rest of the process | ||
| usedMigratedSettings = true; | ||
| } | ||
| else | ||
| { | ||
| Trace.Warning($"Failed to create session with migrated settings: {createSessionResult}"); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Error($"Exception when creating session with migrated settings: {ex}"); | ||
| } | ||
| } | ||
|
|
||
| // If migrated settings weren't used or session creation failed, use original settings | ||
| if (!usedMigratedSettings) | ||
| { | ||
| Trace.Info("Falling back to original .runner settings"); | ||
| _listener = GetMessageListener(settings); | ||
| CreateSessionResult createSessionResult = await _listener.CreateSessionAsync(HostContext.RunnerShutdownToken); | ||
| if (createSessionResult == CreateSessionResult.SessionConflict) | ||
| { | ||
| return Constants.Runner.ReturnCode.SessionConflict; | ||
| } | ||
| else if (createSessionResult == CreateSessionResult.Failure) | ||
| { | ||
| return Constants.Runner.ReturnCode.TerminatedError; | ||
| } | ||
| } | ||
|
|
||
| HostContext.WritePerfCounter("SessionCreated"); | ||
|
|
@@ -428,6 +478,8 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false) | |
| // Should we try to cleanup ephemeral runners | ||
| bool runOnceJobCompleted = false; | ||
| bool skipSessionDeletion = false; | ||
| bool restartSession = false; // Flag to indicate session restart | ||
| bool restartSessionPending = false; | ||
| try | ||
| { | ||
| var notification = HostContext.GetService<IJobNotification>(); | ||
|
|
@@ -443,6 +495,15 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false) | |
|
|
||
| while (!HostContext.RunnerShutdownToken.IsCancellationRequested) | ||
| { | ||
| // Check if we need to restart the session and can do so (job dispatcher not busy) | ||
| if (restartSessionPending && !jobDispatcher.Busy) | ||
| { | ||
| Trace.Info("Pending session restart detected and job dispatcher is not busy. Restarting session now."); | ||
| messageQueueLoopTokenSource.Cancel(); | ||
| restartSession = true; | ||
| break; | ||
| } | ||
|
|
||
| TaskAgentMessage message = null; | ||
| bool skipMessageDeletion = false; | ||
| try | ||
|
|
@@ -679,6 +740,17 @@ await configUpdater.UpdateRunnerConfigAsync( | |
| configType: runnerRefreshConfigMessage.ConfigType, | ||
| serviceType: runnerRefreshConfigMessage.ServiceType, | ||
| configRefreshUrl: runnerRefreshConfigMessage.ConfigRefreshUrl); | ||
|
|
||
| // Set flag to schedule session restart if ConfigType is "runner" | ||
| if (string.Equals(runnerRefreshConfigMessage.ConfigType, "runner", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| Trace.Info("Runner configuration was updated. Session restart has been scheduled"); | ||
| restartSessionPending = true; | ||
| } | ||
| else | ||
| { | ||
| Trace.Info($"No session restart needed for config type: {runnerRefreshConfigMessage.ConfigType}"); | ||
| } | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -733,10 +805,16 @@ await configUpdater.UpdateRunnerConfigAsync( | |
|
|
||
| if (settings.Ephemeral && runOnceJobCompleted) | ||
| { | ||
| var configManager = HostContext.GetService<IConfigurationManager>(); | ||
| configManager.DeleteLocalRunnerConfig(); | ||
| } | ||
| } | ||
|
|
||
| // After cleanup, check if we need to restart the session | ||
| if (restartSession) | ||
| { | ||
| Trace.Info("Restarting runner session after config update..."); | ||
| return Constants.Runner.ReturnCode.RunnerConfigurationRefreshed; | ||
| } | ||
| } | ||
| catch (TaskAgentAccessTokenExpiredException) | ||
| { | ||
|
|
@@ -750,6 +828,28 @@ await configUpdater.UpdateRunnerConfigAsync( | |
| return Constants.Runner.ReturnCode.Success; | ||
| } | ||
|
|
||
| private async Task<int> ExecuteRunnerAsync(RunnerSettings settings, bool runOnce) | ||
| { | ||
| int returnCode = Constants.Runner.ReturnCode.Success; | ||
| bool restart = false; | ||
| do | ||
| { | ||
| restart = false; | ||
| returnCode = await RunAsync(settings, runOnce); | ||
|
|
||
| if (returnCode == Constants.Runner.ReturnCode.RunnerConfigurationRefreshed) | ||
| { | ||
| Trace.Info("Runner configuration was refreshed, restarting session..."); | ||
| // Reload settings in case they changed | ||
| var configManager = HostContext.GetService<IConfigurationManager>(); | ||
| settings = configManager.LoadSettings(); | ||
| restart = true; | ||
| } | ||
| } while (restart); | ||
|
|
||
| return returnCode; | ||
| } | ||
|
|
||
| private void HandleAuthMigrationChanged(object sender, AuthMigrationEventArgs e) | ||
| { | ||
| Trace.Verbose("Handle AuthMigrationChanged in Runner"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.