forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 11
/
redis.go
375 lines (333 loc) · 11.1 KB
/
redis.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package work
import (
"bytes"
"encoding/json"
"fmt"
)
func redisNamespacePrefix(namespace string) string {
l := len(namespace)
if (l > 0) && (namespace[l-1] != ':') {
namespace = namespace + ":"
}
return namespace
}
func redisKeyKnownJobs(namespace string) string {
return redisNamespacePrefix(namespace) + "known_jobs"
}
// returns "<namespace>:jobs:"
// so that we can just append the job name and be good to go
func redisKeyJobsPrefix(namespace string) string {
return redisNamespacePrefix(namespace) + "jobs:"
}
func redisKeyJobs(namespace, jobName string) string {
return redisKeyJobsPrefix(namespace) + jobName
}
func redisKeyJobsInProgress(namespace, poolID, jobName string) string {
return fmt.Sprintf("%s:%s:inprogress", redisKeyJobs(namespace, jobName), poolID)
}
func redisKeyRetry(namespace string) string {
return redisNamespacePrefix(namespace) + "retry"
}
func redisKeyDead(namespace string) string {
return redisNamespacePrefix(namespace) + "dead"
}
func redisKeyScheduled(namespace string) string {
return redisNamespacePrefix(namespace) + "scheduled"
}
func redisKeyWorkerObservation(namespace, workerID string) string {
return redisNamespacePrefix(namespace) + "worker:" + workerID
}
func redisKeyWorkerPools(namespace string) string {
return redisNamespacePrefix(namespace) + "worker_pools"
}
func redisKeyHeartbeat(namespace, workerPoolID string) string {
return redisNamespacePrefix(namespace) + "worker_pools:" + workerPoolID
}
func redisKeyJobsPaused(namespace, jobName string) string {
return redisKeyJobs(namespace, jobName) + ":paused"
}
func redisKeyJobsLock(namespace, jobName string) string {
return redisKeyJobs(namespace, jobName) + ":lock"
}
func redisKeyJobsLockInfo(namespace, jobName string) string {
return redisKeyJobs(namespace, jobName) + ":lock_info"
}
func redisKeyJobsConcurrency(namespace, jobName string) string {
return redisKeyJobs(namespace, jobName) + ":max_concurrency"
}
func redisKeyUniqueJob(namespace, jobName string, args map[string]interface{}) (string, error) {
var buf bytes.Buffer
buf.WriteString(redisNamespacePrefix(namespace))
buf.WriteString("unique:")
buf.WriteString(jobName)
buf.WriteRune(':')
if args != nil {
err := json.NewEncoder(&buf).Encode(args)
if err != nil {
return "", err
}
}
return buf.String(), nil
}
func redisKeyLastPeriodicEnqueue(namespace string) string {
return redisNamespacePrefix(namespace) + "last_periodic_enqueue"
}
// Used to fetch the next job to run
//
// KEYS[1] = the 1st job queue we want to try, eg, "work:jobs:emails"
// KEYS[2] = the 1st job queue's in prog queue, eg, "work:jobs:emails:97c84119d13cb54119a38743:inprogress"
// KEYS[3] = the 2nd job queue...
// KEYS[4] = the 2nd job queue's in prog queue...
// ...
// KEYS[N] = the last job queue...
// KEYS[N+1] = the last job queue's in prog queue...
// ARGV[1] = job queue's workerPoolID
var redisLuaFetchJob = fmt.Sprintf(`
local function acquireLock(lockKey, lockInfoKey, workerPoolID)
redis.call('incr', lockKey)
redis.call('hincrby', lockInfoKey, workerPoolID, 1)
end
local function haveJobs(jobQueue)
return redis.call('llen', jobQueue) > 0
end
local function isPaused(pauseKey)
return redis.call('get', pauseKey)
end
local function canRun(lockKey, maxConcurrency)
local activeJobs = tonumber(redis.call('get', lockKey))
if (not maxConcurrency or maxConcurrency == 0) or (not activeJobs or activeJobs < maxConcurrency) then
-- default case: maxConcurrency not defined or set to 0 means no cap on concurrent jobs OR
-- maxConcurrency set, but lock does not yet exist OR
-- maxConcurrency set, lock is set, but not yet at max concurrency
return true
else
-- we are at max capacity for running jobs
return false
end
end
local res, jobQueue, inProgQueue, pauseKey, lockKey, maxConcurrency, workerPoolID, concurrencyKey, lockInfoKey
local keylen = #KEYS
workerPoolID = ARGV[1]
for i=1,keylen,%d do
jobQueue = KEYS[i]
inProgQueue = KEYS[i+1]
pauseKey = KEYS[i+2]
lockKey = KEYS[i+3]
lockInfoKey = KEYS[i+4]
concurrencyKey = KEYS[i+5]
maxConcurrency = tonumber(redis.call('get', concurrencyKey))
if haveJobs(jobQueue) and not isPaused(pauseKey) and canRun(lockKey, maxConcurrency) then
acquireLock(lockKey, lockInfoKey, workerPoolID)
res = redis.call('rpoplpush', jobQueue, inProgQueue)
return {res, jobQueue, inProgQueue}
end
end
return nil`, fetchKeysPerJobType)
// Used by the reaper to re-enqueue jobs that were in progress
//
// KEYS[1] = the 1st job's in progress queue
// KEYS[2] = the 1st job's job queue
// KEYS[3] = the 2nd job's in progress queue
// KEYS[4] = the 2nd job's job queue
// ...
// KEYS[N] = the last job's in progress queue
// KEYS[N+1] = the last job's job queue
// ARGV[1] = workerPoolID for job queue
var redisLuaReenqueueJob = fmt.Sprintf(`
local function releaseLock(lockKey, lockInfoKey, workerPoolID)
redis.call('decr', lockKey)
redis.call('hincrby', lockInfoKey, workerPoolID, -1)
end
local keylen = #KEYS
local res, jobQueue, inProgQueue, workerPoolID, lockKey, lockInfoKey
workerPoolID = ARGV[1]
for i=1,keylen,%d do
inProgQueue = KEYS[i]
jobQueue = KEYS[i+1]
lockKey = KEYS[i+2]
lockInfoKey = KEYS[i+3]
res = redis.call('rpoplpush', inProgQueue, jobQueue)
if res then
releaseLock(lockKey, lockInfoKey, workerPoolID)
return {res, inProgQueue, jobQueue}
end
end
return nil`, requeueKeysPerJob)
// Used by the reaper to clean up stale locks
//
// KEYS[1] = the 1st job's lock
// KEYS[2] = the 1st job's lock info hash
// KEYS[3] = the 2nd job's lock
// KEYS[4] = the 2nd job's lock info hash
// ...
// KEYS[N] = the last job's lock
// KEYS[N+1] = the last job's lock info haash
// ARGV[1] = the dead worker pool id
var redisLuaReapStaleLocks = `
local keylen = #KEYS
local lock, lockInfo, deadLockCount
local deadPoolID = ARGV[1]
for i=1,keylen,2 do
lock = KEYS[i]
lockInfo = KEYS[i+1]
deadLockCount = tonumber(redis.call('hget', lockInfo, deadPoolID))
if deadLockCount then
redis.call('decrby', lock, deadLockCount)
redis.call('hdel', lockInfo, deadPoolID)
if tonumber(redis.call('get', lock)) < 0 then
redis.call('set', lock, 0)
end
end
end
return nil
`
// KEYS[1] = zset of jobs (retry or scheduled), eg work:retry
// KEYS[2] = zset of dead, eg work:dead. If we don't know the jobName of a job, we'll put it in dead.
// KEYS[3...] = known job queues, eg ["work:jobs:create_watch", "work:jobs:send_email", ...]
// ARGV[1] = jobs prefix, eg, "work:jobs:". We'll take that and append the job name from the JSON object in order to queue up a job
// ARGV[2] = current time in epoch seconds
var redisLuaZremLpushCmd = `
local res, j, queue
res = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[2], 'LIMIT', 0, 1)
if #res > 0 then
j = cjson.decode(res[1])
redis.call('zrem', KEYS[1], res[1])
queue = ARGV[1] .. j['name']
for _,v in pairs(KEYS) do
if v == queue then
j['t'] = tonumber(ARGV[2])
redis.call('lpush', queue, cjson.encode(j))
return 'ok'
end
end
j['err'] = 'unknown job when requeueing'
j['failed_at'] = tonumber(ARGV[2])
redis.call('zadd', KEYS[2], ARGV[2], cjson.encode(j))
return 'dead' -- put on dead queue
end
return nil
`
// KEYS[1] = zset of (dead|scheduled|retry), eg, work:dead
// ARGV[1] = died at. The z rank of the job.
// ARGV[2] = job ID to requeue
// Returns:
// - number of jobs deleted (typically 1 or 0)
// - job bytes (last job only)
var redisLuaDeleteSingleCmd = `
local jobs, i, j, deletedCount, jobBytes
jobs = redis.call('zrangebyscore', KEYS[1], ARGV[1], ARGV[1])
local jobCount = #jobs
jobBytes = ''
deletedCount = 0
for i=1,jobCount do
j = cjson.decode(jobs[i])
if j['id'] == ARGV[2] then
redis.call('zrem', KEYS[1], jobs[i])
deletedCount = deletedCount + 1
jobBytes = jobs[i]
end
end
return {deletedCount, jobBytes}
`
// KEYS[1] = zset of dead jobs, eg, work:dead
// KEYS[2...] = known job queues, eg ["work:jobs:create_watch", "work:jobs:send_email", ...]
// ARGV[1] = jobs prefix, eg, "work:jobs:". We'll take that and append the job name from the JSON object in order to queue up a job
// ARGV[2] = current time in epoch seconds
// ARGV[3] = died at. The z rank of the job.
// ARGV[4] = job ID to requeue
// Returns: number of jobs requeued (typically 1 or 0)
var redisLuaRequeueSingleDeadCmd = `
local jobs, i, j, queue, found, requeuedCount
jobs = redis.call('zrangebyscore', KEYS[1], ARGV[3], ARGV[3])
local jobCount = #jobs
requeuedCount = 0
for i=1,jobCount do
j = cjson.decode(jobs[i])
if j['id'] == ARGV[4] then
redis.call('zrem', KEYS[1], jobs[i])
queue = ARGV[1] .. j['name']
found = false
for _,v in pairs(KEYS) do
if v == queue then
j['t'] = tonumber(ARGV[2])
j['fails'] = nil
j['failed_at'] = nil
j['err'] = nil
redis.call('lpush', queue, cjson.encode(j))
requeuedCount = requeuedCount + 1
found = true
break
end
end
if not found then
j['err'] = 'unknown job when requeueing'
j['failed_at'] = tonumber(ARGV[2])
redis.call('zadd', KEYS[1], ARGV[2] + 5, cjson.encode(j))
end
end
end
return requeuedCount
`
// KEYS[1] = zset of dead jobs, eg work:dead
// KEYS[2...] = known job queues, eg ["work:jobs:create_watch", "work:jobs:send_email", ...]
// ARGV[1] = jobs prefix, eg, "work:jobs:". We'll take that and append the job name from the JSON object in order to queue up a job
// ARGV[2] = current time in epoch seconds
// ARGV[3] = max number of jobs to requeue
// Returns: number of jobs requeued
var redisLuaRequeueAllDeadCmd = `
local jobs, i, j, queue, found, requeuedCount
jobs = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[2], 'LIMIT', 0, ARGV[3])
local jobCount = #jobs
requeuedCount = 0
for i=1,jobCount do
j = cjson.decode(jobs[i])
redis.call('zrem', KEYS[1], jobs[i])
queue = ARGV[1] .. j['name']
found = false
for _,v in pairs(KEYS) do
if v == queue then
j['t'] = tonumber(ARGV[2])
j['fails'] = nil
j['failed_at'] = nil
j['err'] = nil
redis.call('lpush', queue, cjson.encode(j))
requeuedCount = requeuedCount + 1
found = true
break
end
end
if not found then
j['err'] = 'unknown job when requeueing'
j['failed_at'] = tonumber(ARGV[2])
redis.call('zadd', KEYS[1], ARGV[2] + 5, cjson.encode(j))
end
end
return requeuedCount
`
// KEYS[1] = job queue to push onto
// KEYS[2] = Unique job's key. Test for existence and set if we push.
// ARGV[1] = job
// ARGV[2] = updated job or just a 1 if arguments don't update
var redisLuaEnqueueUnique = `
if redis.call('set', KEYS[2], ARGV[2], 'NX', 'EX', '86400') then
redis.call('lpush', KEYS[1], ARGV[1])
return 'ok'
else
redis.call('set', KEYS[2], ARGV[2], 'EX', '86400')
end
return 'dup'
`
// KEYS[1] = scheduled job queue
// KEYS[2] = Unique job's key. Test for existence and set if we push.
// ARGV[1] = job
// ARGV[2] = updated job or just a 1 if arguments don't update
// ARGV[3] = epoch seconds for job to be run at
var redisLuaEnqueueUniqueIn = `
if redis.call('set', KEYS[2], ARGV[2], 'NX', 'EX', '86400') then
redis.call('zadd', KEYS[1], ARGV[3], ARGV[1])
return 'ok'
else
redis.call('set', KEYS[2], ARGV[2], 'EX', '86400')
end
return 'dup'
`