refactor: extract job running routine into own function
This commit is contained in:
parent
d8fb1b9a09
commit
3b70f47676
1 changed files with 37 additions and 35 deletions
|
@ -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)
|
||||||
|
|
||||||
// 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) {
|
|
||||||
|
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)
|
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")
|
||||||
|
@ -86,15 +97,6 @@ func (r *Runner) RunWorkflow(workflow workflow.Workflow) TaskTracker {
|
||||||
|
|
||||||
jobTracker.SetStatus("success")
|
jobTracker.SetStatus("success")
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
go runJob(r, jobContext, jobTracker, &groupWait)
|
|
||||||
}
|
|
||||||
|
|
||||||
groupWait.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
return *rootTracker
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executes a command within the given container.
|
// Executes a command within the given container.
|
||||||
|
|
Loading…
Reference in a new issue