-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Background steps execution engine #4476
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
lokesh755
wants to merge
6
commits into
main
Choose a base branch
from
lokesh755-background-steps-engine
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.
+1,201
−11
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c6e2142
Adds the core background step execution engine to the runner
lokesh755 a25a0dc
Merge branch 'main' into lokesh755-background-steps-engine
lokesh755 e301339
add test
lokesh755 9a8ca18
cleanup
lokesh755 6ceef1f
resolve pr comments
lokesh755 d410bf2
refactor CompleteWaitedSteps
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,329 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using GitHub.DistributedTask.WebApi; | ||
| using GitHub.Runner.Common; | ||
| using GitHub.Runner.Common.Util; | ||
| using GitHub.Runner.Sdk; | ||
| using Pipelines = GitHub.DistributedTask.Pipelines; | ||
|
|
||
| namespace GitHub.Runner.Worker | ||
| { | ||
| [ServiceLocator(Default = typeof(BackgroundStepCoordinator))] | ||
| public interface IBackgroundStepCoordinator : IRunnerService | ||
| { | ||
| void InitializeCoordinator(int maxConcurrent); | ||
| void StartBackgroundStep(IStep step, CancellationToken jobCancellationToken); | ||
| Task WaitForUnwaitedStepsAsync(IExecutionContext jobContext); | ||
| Task RunControlFlowAsync(IExecutionContext stepContext, object data); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Coordinates background step execution, waiting, cancellation, and deferred state. | ||
| /// Extracted from StepsRunner so the main step loop stays clean. | ||
| /// </summary> | ||
| public sealed class BackgroundStepCoordinator : RunnerService, IBackgroundStepCoordinator | ||
| { | ||
| private const int DefaultMaxBackgroundSteps = 10; | ||
| private readonly Dictionary<string, (IStep Step, Task Task, CancellationTokenSource Cts)> _backgroundSteps = new(); | ||
| private readonly HashSet<string> _waitedStepIds = new(); | ||
| private SemaphoreSlim _backgroundSlotSemaphore = new SemaphoreSlim(DefaultMaxBackgroundSteps); | ||
|
|
||
| /// <summary> | ||
| /// Reset per-job state. Call at the start of each job. | ||
| /// </summary> | ||
| public void InitializeCoordinator(int maxConcurrent) | ||
| { | ||
| _backgroundSteps.Clear(); | ||
| _waitedStepIds.Clear(); | ||
| var max = maxConcurrent > 0 ? maxConcurrent : DefaultMaxBackgroundSteps; | ||
| _backgroundSlotSemaphore = new SemaphoreSlim(max); | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Starting background steps | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| /// <summary> | ||
| /// Prepare and launch a background step. Does not block the caller. | ||
| /// </summary> | ||
| public void StartBackgroundStep(IStep step, CancellationToken jobCancellationToken) | ||
| { | ||
| var stepId = step.ExecutionContext?.ContextName ?? step.DisplayName; | ||
|
|
||
| // Isolate GitHubContext so concurrent steps don't overwrite each other's GITHUB_OUTPUT paths | ||
| if (step.ExecutionContext.ExpressionValues.TryGetValue("github", out var ghCtx) && ghCtx is GitHubContext sharedGitHub) | ||
| { | ||
| step.ExecutionContext.ExpressionValues["github"] = sharedGitHub.ShallowCopy(); | ||
| } | ||
|
|
||
| var bgCts = CancellationTokenSource.CreateLinkedTokenSource(jobCancellationToken); | ||
|
|
||
| // Evaluate timeout on the main thread (needs expression context) | ||
| var timeoutMinutes = 0; | ||
| try | ||
| { | ||
| var templateEvaluator = step.ExecutionContext.ToPipelineTemplateEvaluator(); | ||
| timeoutMinutes = templateEvaluator.EvaluateStepTimeout(step.Timeout, step.ExecutionContext.ExpressionValues, step.ExecutionContext.ExpressionFunctions); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Info($"Error determining timeout for background step '{stepId}': {ex.Message}"); | ||
| } | ||
|
|
||
| var task = ExecuteBackgroundStepCoreAsync(step, bgCts, stepId, timeoutMinutes); | ||
| _backgroundSteps[stepId] = (step, task, bgCts); | ||
| Trace.Info($"Background step '{stepId}' queued (slot will be acquired asynchronously)."); | ||
| } | ||
|
|
||
| private async Task ExecuteBackgroundStepCoreAsync( | ||
|
lokesh755 marked this conversation as resolved.
|
||
| IStep step, CancellationTokenSource bgCts, | ||
| string stepId, int timeoutMinutes) | ||
| { | ||
| Trace.Info($"Background step '{stepId}' waiting for slot."); | ||
| await _backgroundSlotSemaphore.WaitAsync(bgCts.Token); | ||
| Trace.Info($"Background step '{stepId}' acquired slot."); | ||
|
|
||
| step.ExecutionContext.Start(); | ||
|
|
||
| if (timeoutMinutes > 0) | ||
| { | ||
| step.ExecutionContext.SetTimeout(TimeSpan.FromMinutes(timeoutMinutes)); | ||
| } | ||
|
|
||
| using var cancelReg = bgCts.Token.Register(() => | ||
| { | ||
| Trace.Info($"Background step '{stepId}': cancellation signalled, sending CancelToken to process."); | ||
| step.ExecutionContext.CancelToken(); | ||
| }); | ||
|
|
||
| TaskResult? result = null; | ||
| try | ||
| { | ||
| await step.RunAsync(); | ||
| result = step.ExecutionContext.Result ?? TaskResult.Succeeded; | ||
| } | ||
| catch (OperationCanceledException) when (bgCts.Token.IsCancellationRequested) | ||
| { | ||
| result = TaskResult.Canceled; | ||
| } | ||
| catch (OperationCanceledException) when (step.ExecutionContext.CancellationToken.IsCancellationRequested) | ||
| { | ||
| Trace.Info($"Background step '{stepId}' timed out after {timeoutMinutes} minutes."); | ||
| step.ExecutionContext.Error($"The background step '{step.DisplayName}' has timed out after {timeoutMinutes} minutes."); | ||
| result = TaskResult.Failed; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Trace.Info($"Background step '{stepId}' failed: {ex.Message}"); | ||
| step.ExecutionContext.Error(ex); | ||
| result = TaskResult.Failed; | ||
| } | ||
| finally | ||
| { | ||
| _backgroundSlotSemaphore.Release(); | ||
|
|
||
| if (step.ExecutionContext.CommandResult != null) | ||
| { | ||
| result = TaskResultUtil.MergeTaskResults(result, step.ExecutionContext.CommandResult.Value); | ||
| } | ||
|
|
||
| step.ExecutionContext.Result = result; | ||
| step.ExecutionContext.ApplyContinueOnError(step.ContinueOnError); | ||
|
|
||
| step.ExecutionContext.Complete(step.ExecutionContext.Result); | ||
| Trace.Info($"Background step '{stepId}' completed with result: {step.ExecutionContext.Result}"); | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Wait | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| private async Task<TaskResult> WaitForStepsAsync(string[] stepIds, CancellationToken cancellationToken) | ||
| { | ||
| var ids = stepIds ?? Array.Empty<string>(); | ||
| await WaitForStepTasksAsync(ids, cancellationToken); | ||
| return CompleteWaitedSteps(ids); | ||
| } | ||
|
|
||
| private async Task<TaskResult> WaitForAllAsync(CancellationToken cancellationToken) | ||
| { | ||
| var remaining = _backgroundSteps.Keys.Where(id => !_waitedStepIds.Contains(id)).ToList(); | ||
| await WaitForStepTasksAsync(remaining, cancellationToken); | ||
| return CompleteWaitedSteps(remaining); | ||
| } | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Cancel | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| private async Task CancelStepsAsync(string[] cancelStepIds) | ||
| { | ||
| if (cancelStepIds == null || cancelStepIds.Length == 0) return; | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
|
|
||
| var idsToCancel = cancelStepIds | ||
| .Where(id => _backgroundSteps.ContainsKey(id) && !_backgroundSteps[id].Task.IsCompleted) | ||
| .ToArray(); | ||
|
|
||
| if (idsToCancel.Length > 0) | ||
| { | ||
| Trace.Info($"Cancelling {idsToCancel.Length} background step(s): {string.Join(", ", idsToCancel)}"); | ||
| await CancelWithGracePeriodAsync(idsToCancel); | ||
| } | ||
|
|
||
| foreach (var id in cancelStepIds) | ||
| { | ||
| FlushDeferredState(id); | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Safety net | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| public async Task WaitForUnwaitedStepsAsync(IExecutionContext jobContext) | ||
| { | ||
| var unwaitedIds = _backgroundSteps.Keys.Where(id => !_waitedStepIds.Contains(id)).ToList(); | ||
| if (unwaitedIds.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Trace.Info($"Safety net: {unwaitedIds.Count} unwaited background step(s) at post-job boundary: {string.Join(", ", unwaitedIds)}"); | ||
| await WaitForAllAsync(jobContext.CancellationToken); | ||
|
|
||
| // Propagate any background step failures to the job result | ||
| foreach (var (_, (step, _, _)) in _backgroundSteps) | ||
| { | ||
| if (step.ExecutionContext.Result == TaskResult.Failed) | ||
| { | ||
| Trace.Info($"Propagating failure from background step '{step.ExecutionContext.ContextName}' to job result."); | ||
| jobContext.Result = TaskResultUtil.MergeTaskResults(jobContext.Result, TaskResult.Failed); | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
| jobContext.JobContext.Status = jobContext.Result?.ToActionResult(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Control-flow step dispatch | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| /// <summary> | ||
| /// Execute a control-flow step (wait, wait-all, cancel) and propagate results. | ||
| /// </summary> | ||
| public async Task RunControlFlowAsync(IExecutionContext stepContext, object data) | ||
| { | ||
| var controlFlow = data as ControlFlowStepData; | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
| switch (controlFlow.Type) | ||
| { | ||
| case Pipelines.BackgroundControlTypes.Wait: | ||
| stepContext.Result = await WaitForStepsAsync(controlFlow.StepIds, stepContext.CancellationToken); | ||
| break; | ||
|
|
||
| case Pipelines.BackgroundControlTypes.WaitAll: | ||
| stepContext.Result = await WaitForAllAsync(stepContext.CancellationToken); | ||
| break; | ||
|
|
||
| case Pipelines.BackgroundControlTypes.Cancel: | ||
| await CancelStepsAsync(controlFlow.StepIds); | ||
| stepContext.Result = TaskResult.Succeeded; | ||
| break; | ||
|
|
||
| default: | ||
| throw new ArgumentException($"Unknown background step control type '{controlFlow.Type}'."); | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Private helpers | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| private async Task WaitForStepTasksAsync(IEnumerable<string> stepIds, CancellationToken cancellationToken) | ||
| { | ||
| var ids = stepIds.ToList(); | ||
| var tasks = new List<Task>(); | ||
|
|
||
| foreach (var stepId in ids) | ||
| { | ||
| if (_backgroundSteps.TryGetValue(stepId, out var entry) && !entry.Task.IsCompleted) | ||
| { | ||
| tasks.Add(entry.Task); | ||
| } | ||
| else if (!_backgroundSteps.ContainsKey(stepId)) | ||
| { | ||
| Trace.Info($"Wait references unknown background step: {stepId}"); | ||
| } | ||
| } | ||
|
|
||
| if (tasks.Count > 0) | ||
| { | ||
| Trace.Info($"Waiting for {tasks.Count} background step(s)..."); | ||
| try | ||
| { | ||
| await Task.WhenAll(tasks).WaitAsync(cancellationToken); | ||
| } | ||
| catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) | ||
| { | ||
| Trace.Info("Wait interrupted by job cancellation — cancelling background steps."); | ||
| await CancelWithGracePeriodAsync(ids); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task CancelWithGracePeriodAsync(IEnumerable<string> stepIds, double graceSeconds = 7.5) | ||
| { | ||
| var tasks = new List<Task>(); | ||
| foreach (var stepId in stepIds) | ||
| { | ||
| if (_backgroundSteps.TryGetValue(stepId, out var entry) && !entry.Task.IsCompleted) | ||
| { | ||
| entry.Step.ExecutionContext.CancelToken(); | ||
| entry.Cts.Cancel(); | ||
| tasks.Add(entry.Task); | ||
| } | ||
| } | ||
|
|
||
| if (tasks.Count > 0) | ||
| { | ||
| try | ||
| { | ||
| await Task.WhenAll(tasks).WaitAsync(TimeSpan.FromSeconds(graceSeconds)); | ||
| } | ||
| catch (TimeoutException) | ||
| { | ||
| Trace.Info($"Some background steps did not terminate within {graceSeconds}s grace period."); | ||
|
lokesh755 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| private void FlushDeferredState(string stepId) | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
| { | ||
| if (_backgroundSteps.TryGetValue(stepId, out var entry)) | ||
| { | ||
| entry.Step.ExecutionContext.FlushDeferredOutputs(); | ||
| entry.Step.ExecutionContext.FlushDeferredEnvironment(); | ||
| entry.Step.ExecutionContext.FlushDeferredOutcomeConclusion(); | ||
| Trace.Info($"Flushed deferred state for background step '{stepId}'."); | ||
| } | ||
| } | ||
|
|
||
| private TaskResult CompleteWaitedSteps(IEnumerable<string> stepIds) | ||
| { | ||
| var result = TaskResult.Succeeded; | ||
| foreach (var id in stepIds) | ||
| { | ||
| FlushDeferredState(id); | ||
| _waitedStepIds.Add(id); | ||
| if (_backgroundSteps.TryGetValue(id, out var entry) && entry.Step.ExecutionContext.Result == TaskResult.Failed) | ||
|
lokesh755 marked this conversation as resolved.
Outdated
|
||
| { | ||
| result = TaskResult.Failed; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System; | ||
|
|
||
| namespace GitHub.Runner.Worker | ||
| { | ||
| /// <summary> | ||
| /// Pure data for control-flow steps (wait, wait-all, cancel). | ||
| /// Type uses Pipelines.BackgroundControlTypes string constants. | ||
| /// </summary> | ||
| public sealed class ControlFlowStepData | ||
|
lokesh755 marked this conversation as resolved.
Outdated
lokesh755 marked this conversation as resolved.
Outdated
|
||
| { | ||
| public string Type { get; set; } | ||
| public Guid StepId { get; set; } | ||
| public string StepName { get; set; } | ||
|
|
||
| // Target step IDs (for wait: steps to wait for; for cancel: steps to cancel) | ||
| public string[] StepIds { get; set; } | ||
|
|
||
| // Parallel group ID for grouping steps in the UI | ||
| public string ParallelGroupId { get; set; } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.