bullmq 5.41.8 → 5.41.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/classes/job-scheduler.js +11 -18
- package/dist/cjs/classes/job-scheduler.js.map +1 -1
- package/dist/cjs/classes/scripts.js +28 -2
- package/dist/cjs/classes/scripts.js.map +1 -1
- package/dist/cjs/commands/addJobScheduler-10.lua +9 -36
- package/dist/cjs/commands/includes/addJobFromScheduler.lua +41 -0
- package/dist/cjs/commands/{updateJobScheduler-7.lua → updateJobScheduler-11.lua} +28 -21
- package/dist/cjs/scripts/addJobScheduler-10.js +77 -67
- package/dist/cjs/scripts/addJobScheduler-10.js.map +1 -1
- package/dist/cjs/scripts/index.js +1 -1
- package/dist/cjs/scripts/index.js.map +1 -1
- package/dist/cjs/scripts/updateJobScheduler-11.js +246 -0
- package/dist/cjs/scripts/updateJobScheduler-11.js.map +1 -0
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/classes/job-scheduler.js +11 -18
- package/dist/esm/classes/job-scheduler.js.map +1 -1
- package/dist/esm/classes/scripts.d.ts +1 -1
- package/dist/esm/classes/scripts.js +28 -2
- package/dist/esm/classes/scripts.js.map +1 -1
- package/dist/esm/commands/addJobScheduler-10.lua +9 -36
- package/dist/esm/commands/includes/addJobFromScheduler.lua +41 -0
- package/dist/esm/commands/{updateJobScheduler-7.lua → updateJobScheduler-11.lua} +28 -21
- package/dist/esm/scripts/addJobScheduler-10.js +77 -67
- package/dist/esm/scripts/addJobScheduler-10.js.map +1 -1
- package/dist/esm/scripts/index.d.ts +1 -1
- package/dist/esm/scripts/index.js +1 -1
- package/dist/esm/scripts/index.js.map +1 -1
- package/dist/esm/scripts/updateJobScheduler-11.js +243 -0
- package/dist/esm/scripts/updateJobScheduler-11.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +1 -1
- package/dist/cjs/scripts/updateJobScheduler-7.js +0 -144
- package/dist/cjs/scripts/updateJobScheduler-7.js.map +0 -1
- package/dist/esm/scripts/updateJobScheduler-7.js +0 -141
- package/dist/esm/scripts/updateJobScheduler-7.js.map +0 -1
- /package/dist/esm/scripts/{updateJobScheduler-7.d.ts → updateJobScheduler-11.d.ts} +0 -0
@@ -34,12 +34,19 @@ const content = `--[[
|
|
34
34
|
local rcall = redis.call
|
35
35
|
local repeatKey = KEYS[1]
|
36
36
|
local delayedKey = KEYS[2]
|
37
|
+
local waitKey = KEYS[3]
|
38
|
+
local pausedKey = KEYS[4]
|
39
|
+
local metaKey = KEYS[5]
|
37
40
|
local prioritizedKey = KEYS[6]
|
38
41
|
local nextMillis = ARGV[1]
|
39
42
|
local jobSchedulerId = ARGV[3]
|
40
43
|
local templateOpts = cmsgpack.unpack(ARGV[5])
|
41
44
|
local prefixKey = ARGV[8]
|
42
45
|
-- Includes
|
46
|
+
--[[
|
47
|
+
Add delay marker if needed.
|
48
|
+
]]
|
49
|
+
-- Includes
|
43
50
|
--[[
|
44
51
|
Adds a delayed job to the queue by doing the following:
|
45
52
|
- Creates a new job key with the job data.
|
@@ -129,6 +136,71 @@ local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, pr
|
|
129
136
|
rcall("ZADD", prioritizedKey, score, jobId)
|
130
137
|
addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
131
138
|
end
|
139
|
+
--[[
|
140
|
+
Function to check for the meta.paused key to decide if we are paused or not
|
141
|
+
(since an empty list and !EXISTS are not really the same).
|
142
|
+
]]
|
143
|
+
local function isQueuePaused(queueMetaKey)
|
144
|
+
return rcall("HEXISTS", queueMetaKey, "paused") == 1
|
145
|
+
end
|
146
|
+
--[[
|
147
|
+
Function to store a job
|
148
|
+
]]
|
149
|
+
local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
|
150
|
+
parentKey, parentData, repeatJobKey)
|
151
|
+
local jsonOpts = cjson.encode(opts)
|
152
|
+
local delay = opts['delay'] or 0
|
153
|
+
local priority = opts['priority'] or 0
|
154
|
+
local debounceId = opts['de'] and opts['de']['id']
|
155
|
+
local optionalValues = {}
|
156
|
+
if parentKey ~= nil then
|
157
|
+
table.insert(optionalValues, "parentKey")
|
158
|
+
table.insert(optionalValues, parentKey)
|
159
|
+
table.insert(optionalValues, "parent")
|
160
|
+
table.insert(optionalValues, parentData)
|
161
|
+
end
|
162
|
+
if repeatJobKey ~= nil then
|
163
|
+
table.insert(optionalValues, "rjk")
|
164
|
+
table.insert(optionalValues, repeatJobKey)
|
165
|
+
end
|
166
|
+
if debounceId then
|
167
|
+
table.insert(optionalValues, "deid")
|
168
|
+
table.insert(optionalValues, debounceId)
|
169
|
+
end
|
170
|
+
rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
|
171
|
+
"timestamp", timestamp, "delay", delay, "priority", priority,
|
172
|
+
unpack(optionalValues))
|
173
|
+
rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
|
174
|
+
return delay, priority
|
175
|
+
end
|
176
|
+
local function addJobFromScheduler(jobKey, jobId, rawOpts, waitKey, pausedKey, metaKey, prioritizedKey,
|
177
|
+
priorityCounter, delayedKey, markerKey, eventsKey, name, maxEvents, timestamp, data, jobSchedulerId)
|
178
|
+
local opts = cmsgpack.unpack(rawOpts)
|
179
|
+
local delay, priority = storeJob(eventsKey, jobKey, jobId, name, data,
|
180
|
+
opts, timestamp, nil, nil, jobSchedulerId)
|
181
|
+
if delay ~= 0 then
|
182
|
+
addDelayedJob(jobId, delayedKey, eventsKey, timestamp, maxEvents, markerKey, delay)
|
183
|
+
else
|
184
|
+
local isPaused = isQueuePaused(metaKey)
|
185
|
+
-- Standard or priority add
|
186
|
+
if priority == 0 then
|
187
|
+
if isPaused then
|
188
|
+
-- LIFO or FIFO
|
189
|
+
local pushCmd = opts['lifo'] and 'RPUSH' or 'LPUSH'
|
190
|
+
rcall(pushCmd, pausedKey, jobId)
|
191
|
+
else
|
192
|
+
-- LIFO or FIFO
|
193
|
+
local pushCmd = opts['lifo'] and 'RPUSH' or 'LPUSH'
|
194
|
+
rcall(pushCmd, waitKey, jobId)
|
195
|
+
end
|
196
|
+
else
|
197
|
+
-- Priority add
|
198
|
+
addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounter, isPaused)
|
199
|
+
end
|
200
|
+
-- Emit waiting event
|
201
|
+
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting", "jobId", jobId)
|
202
|
+
end
|
203
|
+
end
|
132
204
|
--[[
|
133
205
|
Function to get max events value or set by default 10000.
|
134
206
|
]]
|
@@ -140,13 +212,6 @@ local function getOrSetMaxEvents(metaKey)
|
|
140
212
|
end
|
141
213
|
return maxEvents
|
142
214
|
end
|
143
|
-
--[[
|
144
|
-
Function to check for the meta.paused key to decide if we are paused or not
|
145
|
-
(since an empty list and !EXISTS are not really the same).
|
146
|
-
]]
|
147
|
-
local function isQueuePaused(queueMetaKey)
|
148
|
-
return rcall("HEXISTS", queueMetaKey, "paused") == 1
|
149
|
-
end
|
150
215
|
--[[
|
151
216
|
Function to remove job.
|
152
217
|
]]
|
@@ -292,36 +357,6 @@ local function removeJob(jobId, hard, baseKey, shouldRemoveDeduplicationKey)
|
|
292
357
|
end
|
293
358
|
removeJobKeys(jobKey)
|
294
359
|
end
|
295
|
-
--[[
|
296
|
-
Function to store a job
|
297
|
-
]]
|
298
|
-
local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
|
299
|
-
parentKey, parentData, repeatJobKey)
|
300
|
-
local jsonOpts = cjson.encode(opts)
|
301
|
-
local delay = opts['delay'] or 0
|
302
|
-
local priority = opts['priority'] or 0
|
303
|
-
local debounceId = opts['de'] and opts['de']['id']
|
304
|
-
local optionalValues = {}
|
305
|
-
if parentKey ~= nil then
|
306
|
-
table.insert(optionalValues, "parentKey")
|
307
|
-
table.insert(optionalValues, parentKey)
|
308
|
-
table.insert(optionalValues, "parent")
|
309
|
-
table.insert(optionalValues, parentData)
|
310
|
-
end
|
311
|
-
if repeatJobKey ~= nil then
|
312
|
-
table.insert(optionalValues, "rjk")
|
313
|
-
table.insert(optionalValues, repeatJobKey)
|
314
|
-
end
|
315
|
-
if debounceId then
|
316
|
-
table.insert(optionalValues, "deid")
|
317
|
-
table.insert(optionalValues, debounceId)
|
318
|
-
end
|
319
|
-
rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
|
320
|
-
"timestamp", timestamp, "delay", delay, "priority", priority,
|
321
|
-
unpack(optionalValues))
|
322
|
-
rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
|
323
|
-
return delay, priority
|
324
|
-
end
|
325
360
|
--[[
|
326
361
|
Function to store a job scheduler
|
327
362
|
]]
|
@@ -376,12 +411,12 @@ if prevMillis ~= false then
|
|
376
411
|
removeJob(currentJobId, true, prefixKey, true --[[remove debounce key]] )
|
377
412
|
rcall("ZREM", prioritizedKey, currentJobId)
|
378
413
|
else
|
379
|
-
if isQueuePaused(
|
380
|
-
if rcall("LREM",
|
414
|
+
if isQueuePaused(metaKey) then
|
415
|
+
if rcall("LREM", pausedKey, 1, currentJobId) > 0 then
|
381
416
|
removeJob(currentJobId, true, prefixKey, true --[[remove debounce key]] )
|
382
417
|
end
|
383
418
|
else
|
384
|
-
if rcall("LREM",
|
419
|
+
if rcall("LREM", waitKey, 1, currentJobId) > 0 then
|
385
420
|
removeJob(currentJobId, true, prefixKey, true --[[remove debounce key]] )
|
386
421
|
end
|
387
422
|
end
|
@@ -392,35 +427,10 @@ local schedulerOpts = cmsgpack.unpack(ARGV[2])
|
|
392
427
|
storeJobScheduler(jobSchedulerId, schedulerKey, repeatKey, nextMillis, schedulerOpts, ARGV[4], templateOpts)
|
393
428
|
if rcall("EXISTS", nextDelayedJobKey) ~= 1 then
|
394
429
|
local eventsKey = KEYS[9]
|
395
|
-
local metaKey = KEYS[5]
|
396
430
|
local maxEvents = getOrSetMaxEvents(metaKey)
|
397
431
|
rcall("INCR", KEYS[8])
|
398
|
-
|
399
|
-
|
400
|
-
delayedOpts, ARGV[7], nil, nil, jobSchedulerId)
|
401
|
-
if delay ~= 0 then
|
402
|
-
addDelayedJob(nextDelayedJobId, delayedKey, eventsKey,
|
403
|
-
ARGV[7], maxEvents, KEYS[7], delay)
|
404
|
-
else
|
405
|
-
local isPaused = isQueuePaused(KEYS[5])
|
406
|
-
-- Standard or priority add
|
407
|
-
if priority == 0 then
|
408
|
-
if isPaused then
|
409
|
-
-- LIFO or FIFO
|
410
|
-
local pushCmd = delayedOpts['lifo'] and 'RPUSH' or 'LPUSH'
|
411
|
-
rcall(pushCmd, KEYS[4], nextDelayedJobId)
|
412
|
-
else
|
413
|
-
-- LIFO or FIFO
|
414
|
-
local pushCmd = delayedOpts['lifo'] and 'RPUSH' or 'LPUSH'
|
415
|
-
rcall(pushCmd, KEYS[3], nextDelayedJobId)
|
416
|
-
end
|
417
|
-
else
|
418
|
-
-- Priority add
|
419
|
-
addJobWithPriority(KEYS[7], KEYS[6], priority, nextDelayedJobId, KEYS[10], isPaused)
|
420
|
-
end
|
421
|
-
-- Emit waiting event
|
422
|
-
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting", "jobId", nextDelayedJobId)
|
423
|
-
end
|
432
|
+
addJobFromScheduler(nextDelayedJobKey, nextDelayedJobId, ARGV[6], waitKey, pausedKey, metaKey, prioritizedKey,
|
433
|
+
KEYS[10], delayedKey, KEYS[7], eventsKey, schedulerOpts['name'], maxEvents, ARGV[7], ARGV[4], jobSchedulerId)
|
424
434
|
if ARGV[9] ~= "" then
|
425
435
|
rcall("HSET", ARGV[9], "nrjid", nextDelayedJobId)
|
426
436
|
end
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"addJobScheduler-10.js","sourceRoot":"","sources":["../../../src/scripts/addJobScheduler-10.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG
|
1
|
+
{"version":3,"file":"addJobScheduler-10.js","sourceRoot":"","sources":["../../../src/scripts/addJobScheduler-10.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmbf,CAAC;AACW,QAAA,eAAe,GAAG;IAC7B,IAAI,EAAE,iBAAiB;IACvB,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}
|
@@ -44,7 +44,7 @@ tslib_1.__exportStar(require("./reprocessJob-8"), exports);
|
|
44
44
|
tslib_1.__exportStar(require("./retryJob-11"), exports);
|
45
45
|
tslib_1.__exportStar(require("./saveStacktrace-1"), exports);
|
46
46
|
tslib_1.__exportStar(require("./updateData-1"), exports);
|
47
|
-
tslib_1.__exportStar(require("./updateJobScheduler-
|
47
|
+
tslib_1.__exportStar(require("./updateJobScheduler-11"), exports);
|
48
48
|
tslib_1.__exportStar(require("./updateProgress-3"), exports);
|
49
49
|
tslib_1.__exportStar(require("./updateRepeatableJobMillis-1"), exports);
|
50
50
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/scripts/index.ts"],"names":[],"mappings":";;;AAAA,4DAAkC;AAClC,+DAAqC;AACrC,qDAA2B;AAC3B,2DAAiC;AACjC,gEAAsC;AACtC,+DAAqC;AACrC,6DAAmC;AACnC,0DAAgC;AAChC,6DAAmC;AACnC,6DAAmC;AACnC,oDAA0B;AAC1B,yDAA+B;AAC/B,0DAAgC;AAChC,wDAA8B;AAC9B,mEAAyC;AACzC,8DAAoC;AACpC,wDAA8B;AAC9B,8DAAoC;AACpC,uDAA6B;AAC7B,yDAA+B;AAC/B,yDAA+B;AAC/B,0DAAgC;AAChC,sDAA4B;AAC5B,sEAA4C;AAC5C,6DAAmC;AACnC,oEAA0C;AAC1C,4DAAkC;AAClC,4DAAkC;AAClC,8DAAoC;AACpC,oEAA0C;AAC1C,yDAA+B;AAC/B,uDAA6B;AAC7B,oDAA0B;AAC1B,sDAA4B;AAC5B,0DAAgC;AAChC,oEAA0C;AAC1C,wDAA8B;AAC9B,iEAAuC;AACvC,+DAAqC;AACrC,2DAAiC;AACjC,wDAA8B;AAC9B,6DAAmC;AACnC,yDAA+B;AAC/B,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/scripts/index.ts"],"names":[],"mappings":";;;AAAA,4DAAkC;AAClC,+DAAqC;AACrC,qDAA2B;AAC3B,2DAAiC;AACjC,gEAAsC;AACtC,+DAAqC;AACrC,6DAAmC;AACnC,0DAAgC;AAChC,6DAAmC;AACnC,6DAAmC;AACnC,oDAA0B;AAC1B,yDAA+B;AAC/B,0DAAgC;AAChC,wDAA8B;AAC9B,mEAAyC;AACzC,8DAAoC;AACpC,wDAA8B;AAC9B,8DAAoC;AACpC,uDAA6B;AAC7B,yDAA+B;AAC/B,yDAA+B;AAC/B,0DAAgC;AAChC,sDAA4B;AAC5B,sEAA4C;AAC5C,6DAAmC;AACnC,oEAA0C;AAC1C,4DAAkC;AAClC,4DAAkC;AAClC,8DAAoC;AACpC,oEAA0C;AAC1C,yDAA+B;AAC/B,uDAA6B;AAC7B,oDAA0B;AAC1B,sDAA4B;AAC5B,0DAAgC;AAChC,oEAA0C;AAC1C,wDAA8B;AAC9B,iEAAuC;AACvC,+DAAqC;AACrC,2DAAiC;AACjC,wDAA8B;AAC9B,6DAAmC;AACnC,yDAA+B;AAC/B,kEAAwC;AACxC,6DAAmC;AACnC,wEAA8C"}
|
@@ -0,0 +1,246 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.updateJobScheduler = void 0;
|
4
|
+
const content = `--[[
|
5
|
+
Updates a job scheduler and adds next delayed job
|
6
|
+
Input:
|
7
|
+
KEYS[1] 'repeat' key
|
8
|
+
KEYS[2] 'delayed'
|
9
|
+
KEYS[3] 'wait' key
|
10
|
+
KEYS[4] 'paused' key
|
11
|
+
KEYS[5] 'meta'
|
12
|
+
KEYS[6] 'prioritized' key
|
13
|
+
KEYS[7] 'marker',
|
14
|
+
KEYS[8] 'id'
|
15
|
+
KEYS[9] events stream key
|
16
|
+
KEYS[10] 'pc' priority counter
|
17
|
+
KEYS[11] producer key
|
18
|
+
ARGV[1] next milliseconds
|
19
|
+
ARGV[2] jobs scheduler id
|
20
|
+
ARGV[3] Json stringified delayed data
|
21
|
+
ARGV[4] msgpacked delayed opts
|
22
|
+
ARGV[5] timestamp
|
23
|
+
ARGV[6] prefix key
|
24
|
+
ARGV[7] producer id
|
25
|
+
Output:
|
26
|
+
next delayed job id - OK
|
27
|
+
]]
|
28
|
+
local rcall = redis.call
|
29
|
+
local repeatKey = KEYS[1]
|
30
|
+
local delayedKey = KEYS[2]
|
31
|
+
local waitKey = KEYS[3]
|
32
|
+
local pausedKey = KEYS[4]
|
33
|
+
local metaKey = KEYS[5]
|
34
|
+
local prioritizedKey = KEYS[6]
|
35
|
+
local nextMillis = ARGV[1]
|
36
|
+
local jobSchedulerId = ARGV[2]
|
37
|
+
local timestamp = ARGV[5]
|
38
|
+
local prefixKey = ARGV[6]
|
39
|
+
local producerId = ARGV[7]
|
40
|
+
-- Includes
|
41
|
+
--[[
|
42
|
+
Add delay marker if needed.
|
43
|
+
]]
|
44
|
+
-- Includes
|
45
|
+
--[[
|
46
|
+
Adds a delayed job to the queue by doing the following:
|
47
|
+
- Creates a new job key with the job data.
|
48
|
+
- adds to delayed zset.
|
49
|
+
- Emits a global event 'delayed' if the job is delayed.
|
50
|
+
]]
|
51
|
+
-- Includes
|
52
|
+
--[[
|
53
|
+
Add delay marker if needed.
|
54
|
+
]]
|
55
|
+
-- Includes
|
56
|
+
--[[
|
57
|
+
Function to return the next delayed job timestamp.
|
58
|
+
]]
|
59
|
+
local function getNextDelayedTimestamp(delayedKey)
|
60
|
+
local result = rcall("ZRANGE", delayedKey, 0, 0, "WITHSCORES")
|
61
|
+
if #result then
|
62
|
+
local nextTimestamp = tonumber(result[2])
|
63
|
+
if nextTimestamp ~= nil then
|
64
|
+
return nextTimestamp / 0x1000
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
local function addDelayMarkerIfNeeded(markerKey, delayedKey)
|
69
|
+
local nextTimestamp = getNextDelayedTimestamp(delayedKey)
|
70
|
+
if nextTimestamp ~= nil then
|
71
|
+
-- Replace the score of the marker with the newest known
|
72
|
+
-- next timestamp.
|
73
|
+
rcall("ZADD", markerKey, nextTimestamp, "1")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
--[[
|
77
|
+
Bake in the job id first 12 bits into the timestamp
|
78
|
+
to guarantee correct execution order of delayed jobs
|
79
|
+
(up to 4096 jobs per given timestamp or 4096 jobs apart per timestamp)
|
80
|
+
WARNING: Jobs that are so far apart that they wrap around will cause FIFO to fail
|
81
|
+
]]
|
82
|
+
local function getDelayedScore(delayedKey, timestamp, delay)
|
83
|
+
local delayedTimestamp = (delay > 0 and (tonumber(timestamp) + delay)) or tonumber(timestamp)
|
84
|
+
local minScore = delayedTimestamp * 0x1000
|
85
|
+
local maxScore = (delayedTimestamp + 1 ) * 0x1000 - 1
|
86
|
+
local result = rcall("ZREVRANGEBYSCORE", delayedKey, maxScore,
|
87
|
+
minScore, "WITHSCORES","LIMIT", 0, 1)
|
88
|
+
if #result then
|
89
|
+
local currentMaxScore = tonumber(result[2])
|
90
|
+
if currentMaxScore ~= nil then
|
91
|
+
if currentMaxScore >= maxScore then
|
92
|
+
return maxScore, delayedTimestamp
|
93
|
+
else
|
94
|
+
return currentMaxScore + 1, delayedTimestamp
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
return minScore, delayedTimestamp
|
99
|
+
end
|
100
|
+
local function addDelayedJob(jobId, delayedKey, eventsKey, timestamp,
|
101
|
+
maxEvents, markerKey, delay)
|
102
|
+
local score, delayedTimestamp = getDelayedScore(delayedKey, timestamp, tonumber(delay))
|
103
|
+
rcall("ZADD", delayedKey, score, jobId)
|
104
|
+
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "delayed",
|
105
|
+
"jobId", jobId, "delay", delayedTimestamp)
|
106
|
+
-- mark that a delayed job is available
|
107
|
+
addDelayMarkerIfNeeded(markerKey, delayedKey)
|
108
|
+
end
|
109
|
+
--[[
|
110
|
+
Function to add job considering priority.
|
111
|
+
]]
|
112
|
+
-- Includes
|
113
|
+
--[[
|
114
|
+
Add marker if needed when a job is available.
|
115
|
+
]]
|
116
|
+
local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
117
|
+
if not isPausedOrMaxed then
|
118
|
+
rcall("ZADD", markerKey, 0, "0")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
--[[
|
122
|
+
Function to get priority score.
|
123
|
+
]]
|
124
|
+
local function getPriorityScore(priority, priorityCounterKey)
|
125
|
+
local prioCounter = rcall("INCR", priorityCounterKey)
|
126
|
+
return priority * 0x100000000 + prioCounter % 0x100000000
|
127
|
+
end
|
128
|
+
local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey,
|
129
|
+
isPausedOrMaxed)
|
130
|
+
local score = getPriorityScore(priority, priorityCounterKey)
|
131
|
+
rcall("ZADD", prioritizedKey, score, jobId)
|
132
|
+
addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
|
133
|
+
end
|
134
|
+
--[[
|
135
|
+
Function to check for the meta.paused key to decide if we are paused or not
|
136
|
+
(since an empty list and !EXISTS are not really the same).
|
137
|
+
]]
|
138
|
+
local function isQueuePaused(queueMetaKey)
|
139
|
+
return rcall("HEXISTS", queueMetaKey, "paused") == 1
|
140
|
+
end
|
141
|
+
--[[
|
142
|
+
Function to store a job
|
143
|
+
]]
|
144
|
+
local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
|
145
|
+
parentKey, parentData, repeatJobKey)
|
146
|
+
local jsonOpts = cjson.encode(opts)
|
147
|
+
local delay = opts['delay'] or 0
|
148
|
+
local priority = opts['priority'] or 0
|
149
|
+
local debounceId = opts['de'] and opts['de']['id']
|
150
|
+
local optionalValues = {}
|
151
|
+
if parentKey ~= nil then
|
152
|
+
table.insert(optionalValues, "parentKey")
|
153
|
+
table.insert(optionalValues, parentKey)
|
154
|
+
table.insert(optionalValues, "parent")
|
155
|
+
table.insert(optionalValues, parentData)
|
156
|
+
end
|
157
|
+
if repeatJobKey ~= nil then
|
158
|
+
table.insert(optionalValues, "rjk")
|
159
|
+
table.insert(optionalValues, repeatJobKey)
|
160
|
+
end
|
161
|
+
if debounceId then
|
162
|
+
table.insert(optionalValues, "deid")
|
163
|
+
table.insert(optionalValues, debounceId)
|
164
|
+
end
|
165
|
+
rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
|
166
|
+
"timestamp", timestamp, "delay", delay, "priority", priority,
|
167
|
+
unpack(optionalValues))
|
168
|
+
rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
|
169
|
+
return delay, priority
|
170
|
+
end
|
171
|
+
local function addJobFromScheduler(jobKey, jobId, rawOpts, waitKey, pausedKey, metaKey, prioritizedKey,
|
172
|
+
priorityCounter, delayedKey, markerKey, eventsKey, name, maxEvents, timestamp, data, jobSchedulerId)
|
173
|
+
local opts = cmsgpack.unpack(rawOpts)
|
174
|
+
local delay, priority = storeJob(eventsKey, jobKey, jobId, name, data,
|
175
|
+
opts, timestamp, nil, nil, jobSchedulerId)
|
176
|
+
if delay ~= 0 then
|
177
|
+
addDelayedJob(jobId, delayedKey, eventsKey, timestamp, maxEvents, markerKey, delay)
|
178
|
+
else
|
179
|
+
local isPaused = isQueuePaused(metaKey)
|
180
|
+
-- Standard or priority add
|
181
|
+
if priority == 0 then
|
182
|
+
if isPaused then
|
183
|
+
-- LIFO or FIFO
|
184
|
+
local pushCmd = opts['lifo'] and 'RPUSH' or 'LPUSH'
|
185
|
+
rcall(pushCmd, pausedKey, jobId)
|
186
|
+
else
|
187
|
+
-- LIFO or FIFO
|
188
|
+
local pushCmd = opts['lifo'] and 'RPUSH' or 'LPUSH'
|
189
|
+
rcall(pushCmd, waitKey, jobId)
|
190
|
+
end
|
191
|
+
else
|
192
|
+
-- Priority add
|
193
|
+
addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounter, isPaused)
|
194
|
+
end
|
195
|
+
-- Emit waiting event
|
196
|
+
rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting", "jobId", jobId)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
--[[
|
200
|
+
Function to get max events value or set by default 10000.
|
201
|
+
]]
|
202
|
+
local function getOrSetMaxEvents(metaKey)
|
203
|
+
local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents")
|
204
|
+
if not maxEvents then
|
205
|
+
maxEvents = 10000
|
206
|
+
rcall("HSET", metaKey, "opts.maxLenEvents", maxEvents)
|
207
|
+
end
|
208
|
+
return maxEvents
|
209
|
+
end
|
210
|
+
local schedulerKey = repeatKey .. ":" .. jobSchedulerId
|
211
|
+
local nextDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. nextMillis
|
212
|
+
local nextDelayedJobKey = schedulerKey .. ":" .. nextMillis
|
213
|
+
-- Validate that scheduler exists.
|
214
|
+
local prevMillis = rcall("ZSCORE", repeatKey, jobSchedulerId)
|
215
|
+
if prevMillis ~= false then
|
216
|
+
local currentDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. prevMillis
|
217
|
+
if producerId == currentDelayedJobId and rcall("EXISTS", nextDelayedJobKey) ~= 1 then
|
218
|
+
local schedulerAttributes = rcall("HMGET", schedulerKey, "name", "data")
|
219
|
+
rcall("ZADD", repeatKey, nextMillis, jobSchedulerId)
|
220
|
+
rcall("HINCRBY", schedulerKey, "ic", 1)
|
221
|
+
local eventsKey = KEYS[9]
|
222
|
+
local maxEvents = getOrSetMaxEvents(metaKey)
|
223
|
+
rcall("INCR", KEYS[8])
|
224
|
+
-- TODO: remove this workaround in next breaking change,
|
225
|
+
-- all job-schedulers must save job data
|
226
|
+
local templateData = schedulerAttributes[2] or ARGV[3]
|
227
|
+
if templateData and templateData ~= '{}' then
|
228
|
+
rcall("HSET", schedulerKey, "data", templateData)
|
229
|
+
end
|
230
|
+
addJobFromScheduler(nextDelayedJobKey, nextDelayedJobId, ARGV[4], waitKey, pausedKey, metaKey, prioritizedKey,
|
231
|
+
KEYS[10], delayedKey, KEYS[7], eventsKey, schedulerAttributes[1], maxEvents, ARGV[5],
|
232
|
+
templateData or '{}', jobSchedulerId)
|
233
|
+
-- TODO: remove this workaround in next breaking change
|
234
|
+
if KEYS[11] ~= "" then
|
235
|
+
rcall("HSET", KEYS[11], "nrjid", nextDelayedJobId)
|
236
|
+
end
|
237
|
+
return nextDelayedJobId .. "" -- convert to string
|
238
|
+
end
|
239
|
+
end
|
240
|
+
`;
|
241
|
+
exports.updateJobScheduler = {
|
242
|
+
name: 'updateJobScheduler',
|
243
|
+
content,
|
244
|
+
keys: 11,
|
245
|
+
};
|
246
|
+
//# sourceMappingURL=updateJobScheduler-11.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"updateJobScheduler-11.js","sourceRoot":"","sources":["../../../src/scripts/updateJobScheduler-11.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4Of,CAAC;AACW,QAAA,kBAAkB,GAAG;IAChC,IAAI,EAAE,oBAAoB;IAC1B,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}
|