-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Implement termination grace period support for the runner #3830
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
Open
TingluoHuang
wants to merge
1
commit into
main
Choose a base branch
from
users/tihuang/termgrace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ public sealed class Runner : RunnerService, IRunner | |
private readonly object _authMigrationTelemetryLock = new(); | ||
private IRunnerServer _runnerServer; | ||
private CancellationTokenSource _authMigrationTelemetryTokenSource = new(); | ||
private bool _runnerExiting = false; | ||
private bool _hasTerminationGracePeriod = false; | ||
|
||
// <summary> | ||
// Helps avoid excessive calls to Run Service when encountering non-retriable errors from /acquirejob. | ||
|
@@ -309,6 +311,12 @@ public async Task<int> ExecuteCommand(CommandSettings command) | |
_term.WriteLine("https://docs.github.com/en/actions/hosting-your-own-runners/autoscaling-with-self-hosted-runners#using-ephemeral-runners-for-autoscaling", ConsoleColor.Yellow); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(Constants.Variables.Agent.ActionsTerminationGracePeriodSeconds))) | ||
{ | ||
_hasTerminationGracePeriod = true; | ||
Trace.Verbose($"Runner has termination grace period set"); | ||
} | ||
|
||
var cred = store.GetCredentials(); | ||
if (cred != null && | ||
cred.Scheme == Constants.Configuration.OAuth && | ||
|
@@ -339,16 +347,18 @@ public async Task<int> ExecuteCommand(CommandSettings command) | |
|
||
private void Runner_Unloading(object sender, EventArgs e) | ||
{ | ||
_runnerExiting = true; | ||
if ((!_inConfigStage) && (!HostContext.RunnerShutdownToken.IsCancellationRequested)) | ||
{ | ||
HostContext.ShutdownRunner(ShutdownReason.UserCancelled); | ||
HostContext.ShutdownRunner(ShutdownReason.UserCancelled, GetShutdownDelay()); | ||
_completedCommand.WaitOne(Constants.Runner.ExitOnUnloadTimeout); | ||
} | ||
} | ||
|
||
private void CtrlCHandler(object sender, EventArgs e) | ||
{ | ||
_term.WriteLine("Exiting..."); | ||
_runnerExiting = true; | ||
if (_inConfigStage) | ||
{ | ||
HostContext.Dispose(); | ||
|
@@ -371,15 +381,27 @@ private void CtrlCHandler(object sender, EventArgs e) | |
reason = ShutdownReason.UserCancelled; | ||
} | ||
|
||
HostContext.ShutdownRunner(reason); | ||
HostContext.ShutdownRunner(reason, GetShutdownDelay()); | ||
} | ||
else | ||
{ | ||
HostContext.ShutdownRunner(ShutdownReason.UserCancelled); | ||
HostContext.ShutdownRunner(ShutdownReason.UserCancelled, GetShutdownDelay()); | ||
} | ||
} | ||
} | ||
|
||
private void HandleJobStatusEvent(object sender, JobStatusEventArgs e) | ||
{ | ||
if (_hasTerminationGracePeriod && | ||
e != null && | ||
e.Status != TaskAgentStatus.Busy && | ||
_runnerExiting) | ||
{ | ||
Trace.Info("Runner is no longer busy, shutting down."); | ||
HostContext.ShutdownRunner(ShutdownReason.UserCancelled); | ||
TingluoHuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
private IMessageListener GetMessageListener(RunnerSettings settings) | ||
{ | ||
if (settings.UseV2Flow) | ||
|
@@ -430,9 +452,13 @@ private async Task<int> RunAsync(RunnerSettings settings, bool runOnce = false) | |
bool autoUpdateInProgress = false; | ||
Task<bool> selfUpdateTask = null; | ||
bool runOnceJobReceived = false; | ||
jobDispatcher = HostContext.CreateService<IJobDispatcher>(); | ||
jobDispatcher = HostContext.GetService<IJobDispatcher>(); | ||
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. Change to |
||
|
||
jobDispatcher.JobStatus += _listener.OnJobStatus; | ||
if (_hasTerminationGracePeriod) | ||
{ | ||
jobDispatcher.JobStatus += HandleJobStatusEvent; | ||
} | ||
|
||
while (!HostContext.RunnerShutdownToken.IsCancellationRequested) | ||
{ | ||
|
@@ -703,6 +729,10 @@ await configUpdater.UpdateRunnerConfigAsync( | |
{ | ||
if (jobDispatcher != null) | ||
{ | ||
if (_hasTerminationGracePeriod) | ||
{ | ||
jobDispatcher.JobStatus -= HandleJobStatusEvent; | ||
} | ||
jobDispatcher.JobStatus -= _listener.OnJobStatus; | ||
await jobDispatcher.ShutdownAsync(); | ||
} | ||
|
@@ -810,6 +840,34 @@ private async Task ReportAuthMigrationTelemetryAsync(CancellationToken token) | |
} | ||
} | ||
|
||
private TimeSpan GetShutdownDelay() | ||
{ | ||
TimeSpan delay = TimeSpan.Zero; | ||
if (_hasTerminationGracePeriod) | ||
{ | ||
var jobDispatcher = HostContext.GetService<IJobDispatcher>(); | ||
if (jobDispatcher.Busy) | ||
{ | ||
Trace.Info("Runner is busy, checking for grace period."); | ||
var delayEnv = Environment.GetEnvironmentVariable(Constants.Variables.Agent.ActionsTerminationGracePeriodSeconds); | ||
if (!string.IsNullOrEmpty(delayEnv) && | ||
int.TryParse(delayEnv, out int delaySeconds) && | ||
delaySeconds > 0 && | ||
delaySeconds < 60 * 60) // 1 hour | ||
{ | ||
Trace.Info($"Waiting for {delaySeconds} seconds before shutting down."); | ||
delay = TimeSpan.FromSeconds(delaySeconds); | ||
} | ||
} | ||
else | ||
{ | ||
Trace.Verbose("Runner is not busy, no grace period."); | ||
} | ||
} | ||
|
||
return delay; | ||
} | ||
|
||
private void PrintUsage(CommandSettings command) | ||
{ | ||
string separator; | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i use
_hasTerminationGracePeriod
as the feature flag, so unless the new ENV is set, no new code will get executed.