fix: ensure that deferred tasks are called with defer

This commit is contained in:
Marc 2024-08-19 23:44:36 -04:00
parent 0bb9c3f127
commit d8fb1b9a09
Signed by: marc
GPG key ID: 048E042F22B5DC79

View file

@ -36,6 +36,8 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
workflowContext := context.WithValue(context.Background(), "workflow", workflow) workflowContext := context.WithValue(context.Background(), "workflow", workflow)
defer r.deferred.RunAllDeferredTasks()
for _, group := range workflow.GetJobsAsGroups() { for _, group := range workflow.GetJobsAsGroups() {
var groupWait sync.WaitGroup var groupWait sync.WaitGroup
@ -55,12 +57,13 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
// Runs a given job (provided a runner to run it on, its context, a task tracker for progress monitoring and // Runs a given job (provided a runner to run it on, its context, a task tracker for progress monitoring and
// a WaitGroup to coordinate concurrent tasks) and updates the tracker with results. // a WaitGroup to coordinate concurrent tasks) and updates the tracker with results.
runJob := func(runner *Runner, jobContext context.Context, jobTracker *TaskTracker, jobWaitGroup *sync.WaitGroup) { runJob := func(runner *Runner, jobContext context.Context, jobTracker *TaskTracker, jobWaitGroup *sync.WaitGroup) {
containerName := fmt.Sprintf("runner-%s", jobTracker.TaskId)
defer runner.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
defer jobWaitGroup.Done() defer jobWaitGroup.Done()
jobTracker.SetStatus("started") jobTracker.SetStatus("started")
runnerImage := jobContext.Value("runnerImageUri").(string) runnerImage := jobContext.Value("runnerImageUri").(string)
containerName := fmt.Sprintf("runner-%s", jobTracker.TaskId)
logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn) logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn)
if pullError := runner.Driver.Pull(runnerImage); pullError != nil { if pullError := runner.Driver.Pull(runnerImage); pullError != nil {
@ -82,7 +85,7 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
} }
jobTracker.SetStatus("success") jobTracker.SetStatus("success")
runner.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
} }
go runJob(r, jobContext, jobTracker, &groupWait) go runJob(r, jobContext, jobTracker, &groupWait)
@ -91,8 +94,6 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
groupWait.Wait() groupWait.Wait()
} }
r.deferred.RunAllDeferredTasks()
return *rootTracker return *rootTracker
} }