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(workflowContext, "currentJob", job)
jobContext = context.WithValue(jobContext, "runnerImageUri", runnerImage) jobContext = context.WithValue(jobContext, "runnerImageUri", runnerImage)
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 // 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) {
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) 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() defer jobWaitGroup.Done()
jobTracker.SetStatus("started") 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) 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) jobTracker.SetError(pullError)
if !job.ContinueOnError { 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) jobTracker.SetError(runError)
if !job.ContinueOnError { if !job.ContinueOnError {
jobTracker.SetStatus("failed") jobTracker.SetStatus("failed")
@ -88,15 +99,6 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
} }
go runJob(r, jobContext, jobTracker, &groupWait)
}
groupWait.Wait()
}
return *rootTracker
}
// Executes a command within the given container. // Executes a command within the given container.
// //
// If the command raises an error while in the container or fails to run // If the command raises an error while in the container or fails to run