forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
327 lines (278 loc) · 8.57 KB
/
worker.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
package work
import (
"fmt"
"math/rand"
"reflect"
"time"
"github.com/gomodule/redigo/redis"
)
const fetchKeysPerJobType = 6
type worker struct {
workerID string
poolID string
namespace string
pool *redis.Pool
jobTypes map[string]*jobType
sleepBackoffs []int64
middleware []*middlewareHandler
contextType reflect.Type
redisFetchScript *redis.Script
sampler prioritySampler
*observer
stopChan chan struct{}
doneStoppingChan chan struct{}
drainChan chan struct{}
doneDrainingChan chan struct{}
}
func newWorker(namespace string, poolID string, pool *redis.Pool, contextType reflect.Type, middleware []*middlewareHandler, jobTypes map[string]*jobType, sleepBackoffs []int64) *worker {
workerID := makeIdentifier()
ob := newObserver(namespace, pool, workerID)
if len(sleepBackoffs) == 0 {
sleepBackoffs = sleepBackoffsInMilliseconds
}
w := &worker{
workerID: workerID,
poolID: poolID,
namespace: namespace,
pool: pool,
contextType: contextType,
sleepBackoffs: sleepBackoffs,
observer: ob,
stopChan: make(chan struct{}),
doneStoppingChan: make(chan struct{}),
drainChan: make(chan struct{}),
doneDrainingChan: make(chan struct{}),
}
w.updateMiddlewareAndJobTypes(middleware, jobTypes)
return w
}
// note: can't be called while the thing is started
func (w *worker) updateMiddlewareAndJobTypes(middleware []*middlewareHandler, jobTypes map[string]*jobType) {
w.middleware = middleware
sampler := prioritySampler{}
for _, jt := range jobTypes {
sampler.add(jt.Priority,
redisKeyJobs(w.namespace, jt.Name),
redisKeyJobsInProgress(w.namespace, w.poolID, jt.Name),
redisKeyJobsPaused(w.namespace, jt.Name),
redisKeyJobsLock(w.namespace, jt.Name),
redisKeyJobsLockInfo(w.namespace, jt.Name),
redisKeyJobsConcurrency(w.namespace, jt.Name))
}
w.sampler = sampler
w.jobTypes = jobTypes
w.redisFetchScript = redis.NewScript(len(jobTypes)*fetchKeysPerJobType, redisLuaFetchJob)
}
func (w *worker) start() {
go w.loop()
go w.observer.start()
}
func (w *worker) stop() {
w.stopChan <- struct{}{}
<-w.doneStoppingChan
w.observer.drain()
w.observer.stop()
}
func (w *worker) drain() {
w.drainChan <- struct{}{}
<-w.doneDrainingChan
w.observer.drain()
}
var sleepBackoffsInMilliseconds = []int64{0, 10, 100, 1000, 5000}
func (w *worker) loop() {
var drained bool
var consequtiveNoJobs int64
// Begin immediately. We'll change the duration on each tick with a timer.Reset()
timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-w.stopChan:
w.doneStoppingChan <- struct{}{}
return
case <-w.drainChan:
drained = true
timer.Reset(0)
case <-timer.C:
job, err := w.fetchJob()
if err != nil {
logError("worker.fetch", err)
timer.Reset(10 * time.Millisecond)
} else if job != nil {
w.processJob(job)
consequtiveNoJobs = 0
timer.Reset(0)
} else {
if drained {
w.doneDrainingChan <- struct{}{}
drained = false
}
consequtiveNoJobs++
idx := consequtiveNoJobs
if idx >= int64(len(w.sleepBackoffs)) {
idx = int64(len(w.sleepBackoffs)) - 1
}
timer.Reset(time.Duration(w.sleepBackoffs[idx]) * time.Millisecond)
}
}
}
}
func (w *worker) fetchJob() (*Job, error) {
// resort queues
// NOTE: we could optimize this to only resort every second, or something.
w.sampler.sample()
numKeys := len(w.sampler.samples) * fetchKeysPerJobType
var scriptArgs = make([]interface{}, 0, numKeys+1)
for _, s := range w.sampler.samples {
scriptArgs = append(scriptArgs, s.redisJobs, s.redisJobsInProg, s.redisJobsPaused, s.redisJobsLock, s.redisJobsLockInfo, s.redisJobsMaxConcurrency) // KEYS[1-6 * N]
}
scriptArgs = append(scriptArgs, w.poolID) // ARGV[1]
conn := w.pool.Get()
defer conn.Close()
values, err := redis.Values(w.redisFetchScript.Do(conn, scriptArgs...))
if err == redis.ErrNil {
return nil, nil
} else if err != nil {
return nil, err
}
if len(values) != 3 {
return nil, fmt.Errorf("need 3 elements back")
}
rawJSON, ok := values[0].([]byte)
if !ok {
return nil, fmt.Errorf("response msg not bytes")
}
dequeuedFrom, ok := values[1].([]byte)
if !ok {
return nil, fmt.Errorf("response queue not bytes")
}
inProgQueue, ok := values[2].([]byte)
if !ok {
return nil, fmt.Errorf("response in prog not bytes")
}
job, err := newJob(rawJSON, dequeuedFrom, inProgQueue)
if err != nil {
return nil, err
}
return job, nil
}
func (w *worker) processJob(job *Job) {
if job.Unique {
updatedJob := w.getAndDeleteUniqueJob(job)
// This is to support the old way of doing it, where we used the job off the queue and just deleted the unique key
// Going forward the job on the queue will always be just a placeholder, and we will be replacing it with the
// updated job extracted here
if updatedJob != nil {
job = updatedJob
}
}
var runErr error
jt := w.jobTypes[job.Name]
if jt == nil {
runErr = fmt.Errorf("stray job: no handler")
logError("process_job.stray", runErr)
} else {
w.observeStarted(job.Name, job.ID, job.Args)
job.observer = w.observer // for Checkin
_, runErr = runJob(job, w.contextType, w.middleware, jt)
w.observeDone(job.Name, job.ID, runErr)
}
fate := terminateOnly
if runErr != nil {
job.failed(runErr)
fate = w.jobFate(jt, job)
}
w.removeJobFromInProgress(job, fate)
}
func (w *worker) getAndDeleteUniqueJob(job *Job) *Job {
var uniqueKey string
var err error
if job.UniqueKey != "" {
uniqueKey = job.UniqueKey
} else { // For jobs put in queue prior to this change. In the future this can be deleted as there will always be a UniqueKey
uniqueKey, err = redisKeyUniqueJob(w.namespace, job.Name, job.Args)
if err != nil {
logError("worker.delete_unique_job.key", err)
return nil
}
}
conn := w.pool.Get()
defer conn.Close()
rawJSON, err := redis.Bytes(conn.Do("GET", uniqueKey))
if err != nil {
logError("worker.delete_unique_job.get", err)
return nil
}
_, err = conn.Do("DEL", uniqueKey)
if err != nil {
logError("worker.delete_unique_job.del", err)
return nil
}
// Previous versions did not support updated arguments and just set key to 1, so in these cases we should do nothing.
// In the future this can be deleted, as we will always be getting arguments from here
if string(rawJSON) == "1" {
return nil
}
// The job pulled off the queue was just a placeholder with no args, so replace it
jobWithArgs, err := newJob(rawJSON, job.dequeuedFrom, job.inProgQueue)
if err != nil {
logError("worker.delete_unique_job.updated_job", err)
return nil
}
return jobWithArgs
}
func (w *worker) removeJobFromInProgress(job *Job, fate terminateOp) {
conn := w.pool.Get()
defer conn.Close()
conn.Send("MULTI")
conn.Send("LREM", job.inProgQueue, 1, job.rawJSON)
conn.Send("DECR", redisKeyJobsLock(w.namespace, job.Name))
conn.Send("HINCRBY", redisKeyJobsLockInfo(w.namespace, job.Name), w.poolID, -1)
fate(conn)
if _, err := conn.Do("EXEC"); err != nil {
logError("worker.remove_job_from_in_progress.lrem", err)
}
}
type terminateOp func(conn redis.Conn)
func terminateOnly(_ redis.Conn) { return }
func terminateAndRetry(w *worker, jt *jobType, job *Job) terminateOp {
rawJSON, err := job.serialize()
if err != nil {
logError("worker.terminate_and_retry.serialize", err)
return terminateOnly
}
return func(conn redis.Conn) {
conn.Send("ZADD", redisKeyRetry(w.namespace), nowEpochSeconds()+jt.calcBackoff(job), rawJSON)
}
}
func terminateAndDead(w *worker, job *Job) terminateOp {
rawJSON, err := job.serialize()
if err != nil {
logError("worker.terminate_and_dead.serialize", err)
return terminateOnly
}
return func(conn redis.Conn) {
// NOTE: sidekiq limits the # of jobs: only keep jobs for 6 months, and only keep a max # of jobs
// The max # of jobs seems really horrible. Seems like operations should be on top of it.
// conn.Send("ZREMRANGEBYSCORE", redisKeyDead(w.namespace), "-inf", now - keepInterval)
// conn.Send("ZREMRANGEBYRANK", redisKeyDead(w.namespace), 0, -maxJobs)
conn.Send("ZADD", redisKeyDead(w.namespace), nowEpochSeconds(), rawJSON)
}
}
func (w *worker) jobFate(jt *jobType, job *Job) terminateOp {
if jt != nil {
failsRemaining := int64(jt.MaxFails) - job.Fails
if failsRemaining > 0 {
return terminateAndRetry(w, jt, job)
}
if jt.SkipDead {
return terminateOnly
}
}
return terminateAndDead(w, job)
}
// Default algorithm returns an fastly increasing backoff counter which grows in an unbounded fashion
func defaultBackoffCalculator(job *Job) int64 {
fails := job.Fails
return (fails * fails * fails * fails) + 15 + (rand.Int63n(30) * (fails + 1))
}