feat: fail execute call if any job fails
This commit is contained in:
parent
528cdebd1f
commit
bed10050dd
3 changed files with 62 additions and 18 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
29
internal/runner/task_test.go
Normal file
29
internal/runner/task_test.go
Normal 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.")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue