From bed10050dd24be070e6340d7263ddbf2aaa36e07 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 2 Aug 2024 16:57:56 -0400 Subject: [PATCH] feat: fail execute call if any job fails --- internal/commands/execute_workflow.go | 7 ++++- internal/runner/task.go | 44 ++++++++++++++++----------- internal/runner/task_test.go | 29 ++++++++++++++++++ 3 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 internal/runner/task_test.go diff --git a/internal/commands/execute_workflow.go b/internal/commands/execute_workflow.go index 01cfce1..f933dc7 100644 --- a/internal/commands/execute_workflow.go +++ b/internal/commands/execute_workflow.go @@ -2,6 +2,7 @@ package commands import ( "errors" + "fmt" "log" runner "runner/internal/runner" workflow "runner/internal/workflow" @@ -43,9 +44,13 @@ func ExecuteWorkflow(configurationPath string, workflowFile string) error { taskResult := runnerInstance.Execute(*workflow) + if !taskResult.HasError() { + return nil + } + for _, job := range taskResult.Context.Jobs { log.Printf("Job %s: %s", job.Id, job.Status) } - return nil + return fmt.Errorf("Task %s failed with at least 1 error.", taskResult.Id) } diff --git a/internal/runner/task.go b/internal/runner/task.go index fbfd51a..dc87ac6 100644 --- a/internal/runner/task.go +++ b/internal/runner/task.go @@ -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 { jobContext := NewJobContext() t.Context.Jobs[jobContext.Id] = &jobContext @@ -65,3 +48,30 @@ func (t *Task) GetJobContext(jobId string) *JobContext { 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 +} diff --git a/internal/runner/task_test.go b/internal/runner/task_test.go new file mode 100644 index 0000000..d6c4738 --- /dev/null +++ b/internal/runner/task_test.go @@ -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.") + } +}