courgette/internal/runner/task.go

77 lines
1.2 KiB
Go

// Task tracking
//
// Task and its associated structs implements a task tracker used to record the
// health and outcomes of individual jobs that the runner executes.
package runner
import (
"github.com/gofrs/uuid"
)
type JobContext struct {
Id string
Status string
Error error
}
type TaskContext struct {
Jobs map[string]*JobContext
}
type Task struct {
Id string
Context TaskContext
}
func NewTask() Task {
return Task{
Id: uuid.Must(uuid.NewV1()).String(),
Context: TaskContext{
Jobs: map[string]*JobContext{},
},
}
}
func (t *Task) AddJob() string {
jobContext := NewJobContext()
t.Context.Jobs[jobContext.Id] = &jobContext
return jobContext.Id
}
func (t *Task) GetJobContext(jobId string) *JobContext {
ctx, exists := t.Context.Jobs[jobId]
if exists {
return ctx
}
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
}