refactor: extract job running routine into own function

This commit is contained in:
Marc 2024-08-20 00:18:14 -04:00
parent d8fb1b9a09
commit 3b70f47676
Signed by: marc
GPG key ID: 048E042F22B5DC79

View file

@ -54,11 +54,22 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
jobContext := context.WithValue(workflowContext, "currentJob", job)
jobContext = context.WithValue(jobContext, "runnerImageUri", runnerImage)
// 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) {
go r.runJob(jobContext, jobTracker, &groupWait)
}
groupWait.Wait()
}
return *rootTracker
}
// 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.
func (r Runner) runJob(jobContext context.Context, jobTracker *TaskTracker, jobWaitGroup *sync.WaitGroup) {
job := jobContext.Value("currentJob").(workflow.Job)
containerName := fmt.Sprintf("runner-%s", jobTracker.TaskId)
defer runner.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
defer r.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
defer jobWaitGroup.Done()
jobTracker.SetStatus("started")
@ -66,7 +77,7 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn)
if pullError := runner.Driver.Pull(runnerImage); pullError != nil {
if pullError := r.Driver.Pull(runnerImage); pullError != nil {
jobTracker.SetError(pullError)
if !job.ContinueOnError {
@ -76,7 +87,7 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
}
if runError := runner.RunJobInContainer(runnerImage, containerName, jobContext); runError != nil {
if runError := r.RunJobInContainer(runnerImage, containerName, jobContext); runError != nil {
jobTracker.SetError(runError)
if !job.ContinueOnError {
jobTracker.SetStatus("failed")
@ -86,15 +97,6 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
jobTracker.SetStatus("success")
}
go runJob(r, jobContext, jobTracker, &groupWait)
}
groupWait.Wait()
}
return *rootTracker
}
// Executes a command within the given container.