feat: fail execute call if any job fails

This commit is contained in:
Marc 2024-08-02 16:57:56 -04:00
parent 528cdebd1f
commit bed10050dd
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 62 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package commands
import ( import (
"errors" "errors"
"fmt"
"log" "log"
runner "runner/internal/runner" runner "runner/internal/runner"
workflow "runner/internal/workflow" workflow "runner/internal/workflow"
@ -43,9 +44,13 @@ func ExecuteWorkflow(configurationPath string, workflowFile string) error {
taskResult := runnerInstance.Execute(*workflow) taskResult := runnerInstance.Execute(*workflow)
if !taskResult.HasError() {
return nil
}
for _, job := range taskResult.Context.Jobs { for _, job := range taskResult.Context.Jobs {
log.Printf("Job %s: %s", job.Id, job.Status) log.Printf("Job %s: %s", job.Id, job.Status)
} }
return nil return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.Id)
} }

View file

@ -32,23 +32,6 @@ func NewTask() Task {
} }
} }
func NewJobContext() JobContext {
return JobContext{
Id: uuid.Must(uuid.NewV1()).String(),
}
}
func (c *JobContext) SetStatus(newStatus string) *JobContext {
c.Status = newStatus
return c
}
func (c *JobContext) SetError(err error) *JobContext {
c.Error = err
return c
}
func (t *Task) AddJob() string { func (t *Task) AddJob() string {
jobContext := NewJobContext() jobContext := NewJobContext()
t.Context.Jobs[jobContext.Id] = &jobContext t.Context.Jobs[jobContext.Id] = &jobContext
@ -65,3 +48,30 @@ func (t *Task) GetJobContext(jobId string) *JobContext {
return nil return nil
} }
func (t Task) HasError() bool {
for _, job := range t.Context.Jobs {
if job.Error != nil {
return true
}
}
return false
}
func NewJobContext() JobContext {
return JobContext{
Id: uuid.Must(uuid.NewV1()).String(),
}
}
func (c *JobContext) SetStatus(newStatus string) *JobContext {
c.Status = newStatus
return c
}
func (c *JobContext) SetError(err error) *JobContext {
c.Error = err
return c
}

View file

@ -0,0 +1,29 @@
package runner
import (
"errors"
"testing"
)
func TestTaskHasErrorReturnsFalseIfNoUnderlyingJobHaveErrors(t *testing.T) {
task := NewTask()
task.AddJob()
if task.HasError() {
t.Errorf("Expected false, got true.")
}
}
func TestTaskHasErrorReturnsTrueIfAnyJobHasErrors(t *testing.T) {
task := NewTask()
// Two jobs, one of which has an error.
task.AddJob()
jobId := task.AddJob()
task.GetJobContext(jobId).SetError(errors.New("test"))
if !task.HasError() {
t.Errorf("Expected true, got false.")
}
}