-
Notifications
You must be signed in to change notification settings - Fork 31
/
completed.go
224 lines (212 loc) · 6.52 KB
/
completed.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
222
223
224
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 completedHandler struct {
ds datastore.Datastore
broker mq.Broker
onJob job.HandlerFunc
}
func NewCompletedHandler(ds datastore.Datastore, b mq.Broker, mw ...job.MiddlewareFunc) task.HandlerFunc {
h := &completedHandler{
ds: ds,
broker: b,
onJob: job.ApplyMiddleware(NewJobHandler(ds, b), mw),
}
return h.handle
}
func (h *completedHandler) handle(ctx context.Context, et task.EventType, t *tork.Task) error {
return h.completeTask(ctx, t)
}
func (h *completedHandler) completeTask(ctx context.Context, t *tork.Task) error {
if t.ParentID != "" {
return h.completeSubTask(ctx, t)
}
return h.completeTopLevelTask(ctx, t)
}
func (h *completedHandler) completeSubTask(ctx context.Context, t *tork.Task) error {
parent, err := h.ds.GetTaskByID(ctx, t.ParentID)
if err != nil {
return errors.Wrapf(err, "error getting parent composite task: %s", t.ParentID)
}
if parent.Parallel != nil {
return h.completeParallelTask(ctx, t)
}
return h.completeEachTask(ctx, t)
}
func (h *completedHandler) completeEachTask(ctx context.Context, t *tork.Task) error {
var isLast bool
err := h.ds.WithTx(ctx, func(tx datastore.Datastore) error {
// update actual task
if err := tx.UpdateTask(ctx, t.ID, func(u *tork.Task) error {
if u.State != tork.TaskStateRunning && u.State != tork.TaskStateScheduled {
return errors.Errorf("can't complete task %s because it's %s", t.ID, u.State)
}
u.State = tork.TaskStateCompleted
u.CompletedAt = t.CompletedAt
u.Result = t.Result
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
// update parent task
if err := tx.UpdateTask(ctx, t.ParentID, func(u *tork.Task) error {
u.Each.Completions = u.Each.Completions + 1
isLast = u.Each.Completions >= u.Each.Size
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
// update job context
if t.Result != "" && t.Var != "" {
if err := tx.UpdateJob(ctx, t.JobID, func(u *tork.Job) error {
if u.Context.Tasks == nil {
u.Context.Tasks = make(map[string]string)
}
u.Context.Tasks[t.Var] = t.Result
return nil
}); err != nil {
return errors.Wrapf(err, "error updating job in datastore")
}
}
return nil
})
if err != nil {
return errors.Wrapf(err, "error complating each task: %s", t.ID)
}
// complete the parent task
if isLast {
parent, err := h.ds.GetTaskByID(ctx, t.ParentID)
if err != nil {
return errors.New("error fetching the parent task")
}
now := time.Now().UTC()
parent.State = tork.TaskStateCompleted
parent.CompletedAt = &now
return h.completeTask(ctx, parent)
}
return nil
}
func (h *completedHandler) completeParallelTask(ctx context.Context, t *tork.Task) error {
// complete actual task
var isLast bool
err := h.ds.WithTx(ctx, func(tx datastore.Datastore) error {
if err := tx.UpdateTask(ctx, t.ID, func(u *tork.Task) error {
if u.State != tork.TaskStateRunning && u.State != tork.TaskStateScheduled {
return errors.Errorf("can't complete task %s because it's %s", t.ID, u.State)
}
u.State = tork.TaskStateCompleted
u.CompletedAt = t.CompletedAt
u.Result = t.Result
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
// update parent task
if err := tx.UpdateTask(ctx, t.ParentID, func(u *tork.Task) error {
u.Parallel.Completions = u.Parallel.Completions + 1
isLast = u.Parallel.Completions >= len(u.Parallel.Tasks)
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
// update job context
if t.Result != "" && t.Var != "" {
if err := tx.UpdateJob(ctx, t.JobID, func(u *tork.Job) error {
if u.Context.Tasks == nil {
u.Context.Tasks = make(map[string]string)
}
u.Context.Tasks[t.Var] = t.Result
return nil
}); err != nil {
return errors.Wrapf(err, "error updating job in datastore")
}
}
return nil
})
if err != nil {
return errors.Wrapf(err, "error completing task %s", t.ID)
}
// complete the parent task
if isLast {
parent, err := h.ds.GetTaskByID(ctx, t.ParentID)
if err != nil {
return errors.New("error fetching the parent task")
}
now := time.Now().UTC()
parent.State = tork.TaskStateCompleted
parent.CompletedAt = &now
return h.completeTask(ctx, parent)
}
return nil
}
func (c *completedHandler) completeTopLevelTask(ctx context.Context, t *tork.Task) error {
log.Debug().Str("task-id", t.ID).Msg("received task completion")
err := c.ds.WithTx(ctx, func(tx datastore.Datastore) error {
// update task in DB
if err := tx.UpdateTask(ctx, t.ID, func(u *tork.Task) error {
if u.State != tork.TaskStateRunning && u.State != tork.TaskStateScheduled {
return errors.Errorf("can't complete task %s because it's %s", t.ID, u.State)
}
u.State = tork.TaskStateCompleted
u.CompletedAt = t.CompletedAt
u.Result = t.Result
return nil
}); err != nil {
return errors.Wrapf(err, "error updating task in datastore")
}
// update job in DB
if err := tx.UpdateJob(ctx, t.JobID, func(u *tork.Job) error {
u.Position = u.Position + 1
if t.Result != "" && t.Var != "" {
if u.Context.Tasks == nil {
u.Context.Tasks = make(map[string]string)
}
u.Context.Tasks[t.Var] = t.Result
}
return nil
}); err != nil {
return errors.Wrapf(err, "error updating job in datastore")
}
return nil
})
if err != nil {
return errors.Wrapf(err, "error completing task: %s", t.ID)
}
// fire the next task
j, err := c.ds.GetJobByID(ctx, t.JobID)
if err != nil {
return errors.Wrapf(err, "error getting job from datatstore")
}
if j.Position <= len(j.Tasks) {
now := time.Now().UTC()
next := j.Tasks[j.Position-1]
next.ID = uuid.NewUUID()
next.JobID = j.ID
next.State = tork.TaskStatePending
next.Position = j.Position
next.CreatedAt = &now
if err := eval.EvaluateTask(next, j.Context.AsMap()); err != nil {
next.Error = err.Error()
next.State = tork.TaskStateFailed
next.FailedAt = &now
}
if err := c.ds.CreateTask(ctx, next); err != nil {
return err
}
return c.broker.PublishTask(ctx, mq.QUEUE_PENDING, next)
} else {
j.State = tork.JobStateCompleted
return c.onJob(ctx, job.StateChange, j)
}
}