This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
260 lines (234 loc) · 7.58 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
package work
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
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 redisKeyUniqueJob(namespace, jobName string, payload interface{}) (string, error) {
var buf bytes.Buffer
buf.WriteString(redisNamespacePrefix(namespace))
buf.WriteString("unique:")
buf.WriteString(jobName)
buf.WriteRune(':')
if payload != nil {
var err error
// If it's already a byte array, use that. Otherwise encode it
if byteArr, ok := payload.([]byte); ok {
_, err = buf.Write(byteArr)
} else {
err = json.NewEncoder(&buf).Encode(payload)
}
if err != nil {
return "", err
}
}
// JSON encode adds a new line at the end so will not be the same as
// the decoded byte array. Need to strip it.
// https://github.com/golang/go/issues/7767
return strings.TrimSuffix(buf.String(), "\n"), nil
}
func redisKeyLastPeriodicEnqueue(namespace string) string {
return redisNamespacePrefix(namespace) + "last_periodic_enqueue"
}
// 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...
var redisLuaRpoplpushMultiCmd = `
local res
local keylen = #KEYS
for i=1,keylen,2 do
res = redis.call('rpoplpush', KEYS[i], KEYS[i+1])
if res then
return {res, KEYS[i], KEYS[i+1]}
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 existance and set if we push.
// ARGV[1] = job
var redisLuaEnqueueUnique = `
if redis.call('set', KEYS[2], '1', 'NX', 'EX', '86400') then
redis.call('lpush', KEYS[1], ARGV[1])
return 'ok'
end
return 'dup'
`
// KEYS[1] = scheduled job queue
// KEYS[2] = Unique job's key. Test for existance and set if we push.
// ARGV[1] = job
// ARGV[2] = epoch seconds for job to be run at
var redisLuaEnqueueUniqueIn = `
if redis.call('set', KEYS[2], '1', 'NX', 'EX', '86400') then
redis.call('zadd', KEYS[1], ARGV[2], ARGV[1])
return 'ok'
end
return 'dup'
`