From d8fb1b9a097fd12d08df83a0e2c57d41bad44021 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 19 Aug 2024 23:44:36 -0400 Subject: [PATCH] fix: ensure that deferred tasks are called with defer --- internal/runner/runner.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index b7c81f9..b3e61cc 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -36,6 +36,8 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker { workflowContext := context.WithValue(context.Background(), "workflow", workflow) + defer r.deferred.RunAllDeferredTasks() + for _, group := range workflow.GetJobsAsGroups() { 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 // a WaitGroup to coordinate concurrent tasks) and updates the tracker with results. 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() + jobTracker.SetStatus("started") runnerImage := jobContext.Value("runnerImageUri").(string) - containerName := fmt.Sprintf("runner-%s", jobTracker.TaskId) - logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn) if pullError := runner.Driver.Pull(runnerImage); pullError != nil { @@ -82,7 +85,7 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker { } jobTracker.SetStatus("success") - runner.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName)) + } go runJob(r, jobContext, jobTracker, &groupWait) @@ -91,8 +94,6 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker { groupWait.Wait() } - r.deferred.RunAllDeferredTasks() - return *rootTracker }