-
Notifications
You must be signed in to change notification settings - Fork 31
/
job.go
221 lines (211 loc) · 6.22 KB
/
job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package handlers
import (
"context"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/datastore"
"github.com/runabol/tork/internal/eval"
"github.com/runabol/tork/internal/uuid"
"github.com/runabol/tork/middleware/job"
"github.com/runabol/tork/middleware/task"
"github.com/runabol/tork/mq"
)
type jobHandler struct {
ds datastore.Datastore
broker mq.Broker
onPending task.HandlerFunc
onCancel job.HandlerFunc
}
func NewJobHandler(ds datastore.Datastore, b mq.Broker, mw ...task.MiddlewareFunc) job.HandlerFunc {
h := &jobHandler{
ds: ds,
broker: b,
onPending: task.ApplyMiddleware(NewPendingHandler(ds, b), mw),
onCancel: NewCancelHandler(ds, b),
}
return h.handle
}
func (h *jobHandler) handle(ctx context.Context, et job.EventType, j *tork.Job) error {
switch j.State {
case tork.JobStatePending:
return h.startJob(ctx, j)
case tork.JobStateCancelled:
return h.onCancel(ctx, et, j)
case tork.JobStateRestart:
return h.restartJob(ctx, j)
case tork.JobStateCompleted:
return h.completeJob(ctx, j)
case tork.JobStateFailed:
return h.failJob(ctx, j)
case tork.JobStateRunning:
return h.markJobAsRunning(ctx, j)
default:
return errors.Errorf("invalid job state: %s", j.State)
}
}
func (h *jobHandler) startJob(ctx context.Context, j *tork.Job) error {
log.Debug().Msgf("starting job %s", j.ID)
now := time.Now().UTC()
t := j.Tasks[0]
t.ID = uuid.NewUUID()
t.JobID = j.ID
t.State = tork.TaskStatePending
t.Position = 1
t.CreatedAt = &now
if err := eval.EvaluateTask(t, j.Context.AsMap()); err != nil {
t.Error = err.Error()
t.State = tork.TaskStateFailed
t.FailedAt = &now
}
if err := h.ds.CreateTask(ctx, t); err != nil {
return err
}
if err := h.ds.UpdateJob(ctx, j.ID, func(u *tork.Job) error {
n := time.Now().UTC()
u.State = tork.JobStateScheduled
u.StartedAt = &n
u.Position = 1
return nil
}); err != nil {
return err
}
if t.State == tork.TaskStateFailed {
n := time.Now().UTC()
j.FailedAt = &n
j.State = tork.JobStateFailed
return h.handle(ctx, job.StateChange, j)
}
return h.onPending(ctx, task.StateChange, t)
}
func (h *jobHandler) completeJob(ctx context.Context, j *tork.Job) error {
// mark the job as completed
if err := h.ds.UpdateJob(ctx, j.ID, func(u *tork.Job) error {
if u.State != tork.JobStateRunning && u.State != tork.JobStateScheduled {
return errors.Errorf("job %s is %s and can not be completed", u.ID, u.State)
}
now := time.Now().UTC()
// evaluate the job's output
result, jobErr := eval.EvaluateTemplate(j.Output, j.Context.AsMap())
if jobErr != nil {
log.Error().Err(jobErr).Msgf("error evaluating job %s output", j.ID)
j.State = tork.JobStateFailed
u.State = tork.JobStateFailed
u.FailedAt = &now
u.Error = jobErr.Error()
j.Error = jobErr.Error()
} else {
j.State = tork.JobStateCompleted
u.State = tork.JobStateCompleted
u.CompletedAt = &now
u.Result = result
j.Result = result
}
return nil
}); err != nil {
return errors.Wrapf(err, "error updating job in datastore")
}
// if this is a sub-job -- complete/fail the parent task
if j.ParentID != "" {
parent, err := h.ds.GetTaskByID(ctx, j.ParentID)
if err != nil {
return errors.Wrapf(err, "could not find parent task for subtask: %s", j.ParentID)
}
now := time.Now().UTC()
if j.State == tork.JobStateFailed {
parent.State = tork.TaskStateFailed
parent.FailedAt = &now
parent.Error = j.Error
} else {
parent.State = tork.TaskStateCompleted
parent.CompletedAt = &now
parent.Result = j.Result
}
return h.broker.PublishTask(ctx, mq.QUEUE_COMPLETED, parent)
}
// publish job completd/failed event
if j.State == tork.JobStateFailed {
return h.broker.PublishEvent(ctx, mq.TOPIC_JOB_FAILED, j)
} else {
return h.broker.PublishEvent(ctx, mq.TOPIC_JOB_COMPLETED, j)
}
}
func (h *jobHandler) markJobAsRunning(ctx context.Context, j *tork.Job) error {
return h.ds.UpdateJob(ctx, j.ID, func(u *tork.Job) error {
if u.State != tork.JobStateScheduled {
return nil
}
u.State = tork.JobStateRunning
u.FailedAt = nil
return nil
})
}
func (h *jobHandler) restartJob(ctx context.Context, j *tork.Job) error {
// mark the job as running
if err := h.ds.UpdateJob(ctx, j.ID, func(u *tork.Job) error {
if u.State != tork.JobStateFailed && u.State != tork.JobStateCancelled {
return errors.Errorf("job %s is in %s state and can't be restarted", j.ID, j.State)
}
u.State = tork.JobStateRunning
u.FailedAt = nil
return nil
}); err != nil {
return err
}
// retry the current top level task
now := time.Now().UTC()
t := j.Tasks[j.Position-1]
t.ID = uuid.NewUUID()
t.JobID = j.ID
t.State = tork.TaskStatePending
t.Position = j.Position
t.CreatedAt = &now
if err := eval.EvaluateTask(t, j.Context.AsMap()); err != nil {
t.Error = err.Error()
t.State = tork.TaskStateFailed
t.FailedAt = &now
}
if err := h.ds.CreateTask(ctx, t); err != nil {
return err
}
return h.broker.PublishTask(ctx, mq.QUEUE_PENDING, t)
}
func (h *jobHandler) failJob(ctx context.Context, j *tork.Job) error {
// mark the job as FAILED
if err := h.ds.UpdateJob(ctx, j.ID, func(u *tork.Job) error {
// we only want to make the job as FAILED
// if it's actually running as opposed to
// possibly being CANCELLED
if u.State == tork.JobStateRunning || u.State == tork.JobStateScheduled {
u.State = tork.JobStateFailed
u.FailedAt = j.FailedAt
}
return nil
}); err != nil {
return errors.Wrapf(err, "error marking the job as failed in the datastore")
}
// if this is a sub-job -- FAIL the parent task
if j.ParentID != "" {
parent, err := h.ds.GetTaskByID(ctx, j.ParentID)
if err != nil {
return errors.Wrapf(err, "could not find parent task for subtask: %s", j.ParentID)
}
parent.State = tork.TaskStateFailed
parent.FailedAt = j.FailedAt
parent.Error = j.Error
return h.broker.PublishTask(ctx, mq.QUEUE_ERROR, parent)
}
// cancel all currently running tasks
if err := cancelActiveTasks(ctx, h.ds, h.broker, j.ID); err != nil {
return err
}
j, err := h.ds.GetJobByID(ctx, j.ID)
if err != nil {
return errors.Wrapf(err, "unknown job: %s", j.ID)
}
if j.State == tork.JobStateFailed {
return h.broker.PublishEvent(ctx, mq.TOPIC_JOB_FAILED, j)
}
return nil
}