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,41 +54,7 @@ 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)
// Runs a given job (provided a runner to run it on, its context, a task tracker for progress monitoring and go r.runJob(jobContext, jobTracker, &groupWait)
// 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)
logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn)
if pullError := runner.Driver.Pull(runnerImage); pullError != nil {
jobTracker.SetError(pullError)
if !job.ContinueOnError {
jobTracker.SetStatus("failed")
return
}
}
if runError := runner.RunJobInContainer(runnerImage, containerName, jobContext); runError != nil {
jobTracker.SetError(runError)
if !job.ContinueOnError {
jobTracker.SetStatus("failed")
return
}
}
jobTracker.SetStatus("success")
}
go runJob(r, jobContext, jobTracker, &groupWait)
} }
groupWait.Wait() groupWait.Wait()
@ -97,6 +63,42 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
return *rootTracker 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 r.deferred.RunDeferredTasksInScope(fmt.Sprintf("job-%s", containerName))
defer jobWaitGroup.Done()
jobTracker.SetStatus("started")
runnerImage := jobContext.Value("runnerImageUri").(string)
logger.Info("Using image %s (label: %s)", runnerImage, job.RunsOn)
if pullError := r.Driver.Pull(runnerImage); pullError != nil {
jobTracker.SetError(pullError)
if !job.ContinueOnError {
jobTracker.SetStatus("failed")
return
}
}
if runError := r.RunJobInContainer(runnerImage, containerName, jobContext); runError != nil {
jobTracker.SetError(runError)
if !job.ContinueOnError {
jobTracker.SetStatus("failed")
return
}
}
jobTracker.SetStatus("success")
}
// 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