-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprocessor.go
351 lines (291 loc) · 8.55 KB
/
processor.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) 2022, R.I. Pienaar and the Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package asyncjobs
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type processor struct {
queue *Queue
mux *Mux
c *Client
concurrency int
limiter chan struct{}
retryPolicy RetryPolicyProvider
log Logger
mu *sync.Mutex
}
// ItemKind indicates the kind of job a work queue entry represents
type ItemKind int
var (
// TaskItem is a task as defined by Task
TaskItem ItemKind = 0
)
// ProcessItem is an individual item stored in the work queue
type ProcessItem struct {
Kind ItemKind `json:"kind"`
JobID string `json:"job"`
storageMeta any
}
func newProcessItem(kind ItemKind, id string) ([]byte, error) {
return json.Marshal(&ProcessItem{Kind: kind, JobID: id})
}
func newProcessor(c *Client) (*processor, error) {
p := &processor{
c: c,
queue: c.opts.queue,
concurrency: c.opts.concurrency,
limiter: make(chan struct{}, c.opts.concurrency),
retryPolicy: c.opts.retryPolicy,
log: c.log,
mu: &sync.Mutex{},
}
for i := 0; i < cap(p.limiter); i++ {
p.limiter <- struct{}{}
}
return p, nil
}
func (p *processor) loadDependencies(task *Task) (bool, bool, error) {
ready := true
for _, parent := range task.Dependencies {
pt, err := p.c.LoadTaskByID(parent)
if errors.Is(err, ErrTaskNotFound) {
return false, true, fmt.Errorf("%w: %s", ErrTaskNotFound, parent)
} else if err != nil {
return false, false, fmt.Errorf("%s: %s: %v", ErrTaskLoadFailed, parent, err)
}
switch pt.State {
case TaskStateCompleted:
case TaskStateExpired, TaskStateTerminated, TaskStateQueueError, TaskStateUnknown:
return false, true, nil
default:
ready = false
continue
}
if task.LoadDependencies {
if len(task.DependencyResults) == 0 {
task.DependencyResults = make(map[string]*TaskResult)
}
task.DependencyResults[pt.ID] = pt.Result
}
}
return ready, false, nil
}
func (p *processor) processDependencies(ctx context.Context, item *ProcessItem, task *Task) (bool, error) {
ready, failed, err := p.loadDependencies(task)
if err != nil {
if failed {
p.log.Warnf("Could not process dependencies for task %s, terminating task: %v", task.ID, err)
taskDependenciesFailedCounter.WithLabelValues().Inc()
p.c.storage.TerminateItem(ctx, item)
p.c.handleTaskError(ctx, task, fmt.Errorf("%w: %v", ErrTaskDependenciesFailed, err))
} else {
p.log.Warnf("Could not process dependencies for task %s, will retry: %v", task.ID, err)
err = p.c.storage.NakBlockedItem(ctx, item)
if err != nil {
p.log.Warnf("NaK blocked item failed: %v", err)
}
}
return false, err
}
if failed {
p.c.storage.TerminateItem(ctx, item)
p.c.handleTaskError(ctx, task, fmt.Errorf("%w: %v", ErrTaskDependenciesFailed, err))
return false, ErrTaskDependenciesFailed
}
if !ready {
err = p.c.storage.NakBlockedItem(ctx, item)
if err != nil {
p.log.Warnf("NaK of blocked item failed: %v", err)
}
return false, nil
}
return true, nil
}
func (p *processor) processMessage(ctx context.Context, item *ProcessItem) error {
task, err := p.c.LoadTaskByID(item.JobID)
if err != nil {
workQueueEntryForUnknownTaskErrorCounter.WithLabelValues(p.queue.Name).Inc()
if errors.Is(err, ErrTaskNotFound) {
p.log.Warnf("Could not find task data for %s, discarding work item", item.JobID)
p.c.storage.TerminateItem(ctx, item)
p.limiter <- struct{}{} // todo handle this in a better place
return nil
}
return fmt.Errorf("%s: %s", ErrTaskLoadFailed, err)
}
switch task.State {
case TaskStateActive:
if task.LastTriedAt == nil || time.Since(*task.LastTriedAt) < p.queue.MaxRunTime {
return ErrTaskAlreadyActive
}
case TaskStateUnreachable:
p.c.storage.AckItem(ctx, item)
return ErrTaskDependenciesFailed
case TaskStateCompleted, TaskStateExpired:
p.c.storage.AckItem(ctx, item)
return fmt.Errorf("%w %q", ErrTaskAlreadyInState, task.State)
}
if task.IsPastDeadline() {
workQueueEntryPastDeadlineCounter.WithLabelValues(p.queue.Name).Inc()
err = p.c.handleTaskExpired(ctx, task)
if err != nil {
p.log.Warnf("Could not expire task %s: %v", task.ID, err)
}
return ErrTaskPastDeadline
}
if task.MaxTries > 0 && task.Tries >= task.MaxTries {
workQueueEntryPastMaxTriesCounter.WithLabelValues(p.queue.Name).Inc()
err = p.c.handleTaskExpired(ctx, task)
if err != nil {
p.log.Warnf("Could not expire task %s: %v", task.ID, err)
}
return ErrTaskExceedsMaxTries
}
if task.State == TaskStateBlocked && task.HasDependencies() {
should, err := p.processDependencies(ctx, item, task)
if err != nil {
if errors.Is(err, ErrTaskDependenciesFailed) {
return err
}
}
if !should {
p.limiter <- struct{}{} // todo handle this in a better place
return nil
}
}
err = p.c.setTaskActive(ctx, task)
if err != nil {
return fmt.Errorf("%w %s: %v", ErrTaskUpdateFailed, task.State, err)
}
go p.handle(ctx, task, item, p.queue.MaxRunTime)
return nil
}
func (p *processor) pollItem(ctx context.Context) (*ProcessItem, error) {
ctr := 0
for {
if ctx.Err() != nil {
return nil, ctx.Err()
}
workQueuePollCounter.WithLabelValues(p.queue.Name).Inc()
timeout, cancel := context.WithTimeout(ctx, time.Minute)
item, err := p.c.storage.PollQueue(timeout, p.queue)
cancel()
switch {
case err == context.Canceled:
p.log.Debugf("Context canceled, terminating polling")
return nil, err
case err == context.DeadlineExceeded:
p.log.Debugf("Context timeout, retrying poll")
ctr = 0
continue
case err != nil:
p.log.Debugf("Unexpected polling error: %v", err)
workQueuePollErrorCounter.WithLabelValues(p.queue.Name).Inc()
if RetrySleep(ctx, retryLinearTenSeconds, ctr) == context.Canceled {
return nil, ctx.Err()
}
ctr++
continue
case item == nil:
p.log.Debugf("Had a nil item, retrying")
// 404 etc
continue
}
return item, nil
}
}
func (p *processor) processMessages(ctx context.Context, mux *Mux) error {
if mux == nil {
return ErrNoMux
}
p.mux = mux
for {
select {
case <-p.limiter:
item, err := p.pollItem(ctx)
if err != nil {
if err == context.DeadlineExceeded {
p.log.Infof("Processor exiting on context %s", err)
return nil
}
if err == context.Canceled {
return nil
}
p.log.Errorf("Unexpected polling error: %v", err)
// pollItem already logged and slept
p.limiter <- struct{}{}
}
if item == nil {
continue
}
p.log.Debugf("Received an Item with ID %s", item.JobID)
err = p.processMessage(ctx, item)
if err != nil {
p.log.Warnf("Processing job %s failed: %v", item.JobID, err)
p.limiter <- struct{}{}
continue
}
case <-ctx.Done():
p.log.Infof("Processor exiting on context %s", ctx.Err())
return nil
}
}
}
func (p *processor) handle(ctx context.Context, t *Task, item *ProcessItem, to time.Duration) {
defer func() {
handlersBusyGauge.WithLabelValues().Dec()
p.limiter <- struct{}{}
}()
if p.mux == nil {
return
}
obs := prometheus.NewTimer(handlerRunTimeSummary.WithLabelValues(t.Queue, t.Type))
defer obs.ObserveDuration()
handlersBusyGauge.WithLabelValues().Inc()
timeout, cancel := context.WithTimeout(ctx, to)
defer cancel()
t.Tries++
payload, err := p.mux.Handler(t)(timeout, p.log, t)
if err != nil {
if errors.Is(err, ErrTerminateTask) {
handlersErroredCounter.WithLabelValues(t.Queue, t.Type).Inc()
p.log.Errorf("Handling task %s failed, terminating retries: %s", t.ID, err)
err = p.c.handleTaskTerminated(ctx, t, err)
if err != nil {
p.log.Warnf("Updating task after failed processing failed: %v", err)
}
err = p.c.storage.TerminateItem(ctx, item)
if err != nil {
p.log.Warnf("Term after failed processing failed: %v", err)
}
} else {
handlersErroredCounter.WithLabelValues(t.Queue, t.Type).Inc()
p.log.Errorf("Handling task %s failed: %s", t.ID, err)
err = p.c.handleTaskError(ctx, t, err)
if err != nil {
p.log.Warnf("Updating task after failed processing failed: %v", err)
}
err = p.c.storage.NakItem(ctx, item)
if err != nil {
p.log.Warnf("NaK after failed processing failed: %v", err)
}
}
return
}
err = p.c.setTaskSuccess(ctx, t, payload)
if err != nil {
p.log.Warnf("Updating task after processing failed: %v", err)
}
// we try ack the thing anyway, hail mary to avoid a retry even if setTaskSuccess failed
err = p.c.storage.AckItem(ctx, item)
if err != nil {
p.log.Errorf("Acknowledging work item failed: %v", err)
}
}