bullmq 5.1.3 → 5.1.5

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.
Files changed (103) hide show
  1. package/dist/cjs/classes/scripts.js +1 -0
  2. package/dist/cjs/classes/scripts.js.map +1 -1
  3. package/dist/cjs/commands/addStandardJob-7.lua +4 -8
  4. package/dist/cjs/commands/changeDelay-3.lua +1 -0
  5. package/dist/cjs/commands/changePriority-6.lua +0 -1
  6. package/dist/cjs/commands/includes/addBaseMarkerIfNeeded.lua +9 -0
  7. package/dist/cjs/commands/includes/addDelayMarkerIfNeeded.lua +3 -1
  8. package/dist/cjs/commands/includes/addJobInTargetList.lua +11 -0
  9. package/dist/cjs/commands/includes/addJobWithPriority.lua +5 -3
  10. package/dist/cjs/commands/includes/moveParentToWaitIfNeeded.lua +6 -4
  11. package/dist/cjs/commands/includes/prepareJobForProcessing.lua +3 -39
  12. package/dist/cjs/commands/includes/promoteDelayedJobs.lua +2 -4
  13. package/dist/cjs/commands/moveJobsToWait-7.lua +2 -3
  14. package/dist/cjs/commands/moveStalledJobsToWait-9.lua +150 -0
  15. package/dist/cjs/commands/moveToActive-11.lua +6 -5
  16. package/dist/cjs/commands/moveToFinished-14.lua +13 -11
  17. package/dist/cjs/commands/pause-7.lua +1 -0
  18. package/dist/cjs/commands/promote-8.lua +2 -2
  19. package/dist/cjs/commands/retryJob-10.lua +1 -0
  20. package/dist/cjs/scripts/addDelayedJob-6.js +21 -7
  21. package/dist/cjs/scripts/addDelayedJob-6.js.map +1 -1
  22. package/dist/cjs/scripts/addParentJob-4.js +25 -11
  23. package/dist/cjs/scripts/addParentJob-4.js.map +1 -1
  24. package/dist/cjs/scripts/addPrioritizedJob-7.js +21 -7
  25. package/dist/cjs/scripts/addPrioritizedJob-7.js.map +1 -1
  26. package/dist/cjs/scripts/addStandardJob-7.js +46 -36
  27. package/dist/cjs/scripts/addStandardJob-7.js.map +1 -1
  28. package/dist/cjs/scripts/changeDelay-3.js +1 -0
  29. package/dist/cjs/scripts/changeDelay-3.js.map +1 -1
  30. package/dist/cjs/scripts/changePriority-6.js +10 -14
  31. package/dist/cjs/scripts/changePriority-6.js.map +1 -1
  32. package/dist/cjs/scripts/index.js +1 -1
  33. package/dist/cjs/scripts/moveJobsToWait-7.js +9 -3
  34. package/dist/cjs/scripts/moveJobsToWait-7.js.map +1 -1
  35. package/dist/cjs/scripts/{moveStalledJobsToWait-8.js → moveStalledJobsToWait-9.js} +109 -106
  36. package/dist/cjs/scripts/{moveStalledJobsToWait-8.js.map → moveStalledJobsToWait-9.js.map} +1 -1
  37. package/dist/cjs/scripts/moveToActive-11.js +28 -50
  38. package/dist/cjs/scripts/moveToActive-11.js.map +1 -1
  39. package/dist/cjs/scripts/moveToDelayed-7.js +1 -1
  40. package/dist/cjs/scripts/moveToFinished-14.js +47 -70
  41. package/dist/cjs/scripts/moveToFinished-14.js.map +1 -1
  42. package/dist/cjs/scripts/pause-7.js +2 -1
  43. package/dist/cjs/scripts/pause-7.js.map +1 -1
  44. package/dist/cjs/scripts/promote-8.js +19 -5
  45. package/dist/cjs/scripts/promote-8.js.map +1 -1
  46. package/dist/cjs/scripts/retryJob-10.js +20 -7
  47. package/dist/cjs/scripts/retryJob-10.js.map +1 -1
  48. package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
  49. package/dist/esm/classes/scripts.js +1 -0
  50. package/dist/esm/classes/scripts.js.map +1 -1
  51. package/dist/esm/commands/addStandardJob-7.lua +4 -8
  52. package/dist/esm/commands/changeDelay-3.lua +1 -0
  53. package/dist/esm/commands/changePriority-6.lua +0 -1
  54. package/dist/esm/commands/includes/addBaseMarkerIfNeeded.lua +9 -0
  55. package/dist/esm/commands/includes/addDelayMarkerIfNeeded.lua +3 -1
  56. package/dist/esm/commands/includes/addJobInTargetList.lua +11 -0
  57. package/dist/esm/commands/includes/addJobWithPriority.lua +5 -3
  58. package/dist/esm/commands/includes/moveParentToWaitIfNeeded.lua +6 -4
  59. package/dist/esm/commands/includes/prepareJobForProcessing.lua +3 -39
  60. package/dist/esm/commands/includes/promoteDelayedJobs.lua +2 -4
  61. package/dist/esm/commands/moveJobsToWait-7.lua +2 -3
  62. package/dist/esm/commands/moveStalledJobsToWait-9.lua +150 -0
  63. package/dist/esm/commands/moveToActive-11.lua +6 -5
  64. package/dist/esm/commands/moveToFinished-14.lua +13 -11
  65. package/dist/esm/commands/pause-7.lua +1 -0
  66. package/dist/esm/commands/promote-8.lua +2 -2
  67. package/dist/esm/commands/retryJob-10.lua +1 -0
  68. package/dist/esm/scripts/addDelayedJob-6.js +21 -7
  69. package/dist/esm/scripts/addDelayedJob-6.js.map +1 -1
  70. package/dist/esm/scripts/addParentJob-4.js +25 -11
  71. package/dist/esm/scripts/addParentJob-4.js.map +1 -1
  72. package/dist/esm/scripts/addPrioritizedJob-7.js +21 -7
  73. package/dist/esm/scripts/addPrioritizedJob-7.js.map +1 -1
  74. package/dist/esm/scripts/addStandardJob-7.js +46 -36
  75. package/dist/esm/scripts/addStandardJob-7.js.map +1 -1
  76. package/dist/esm/scripts/changeDelay-3.js +1 -0
  77. package/dist/esm/scripts/changeDelay-3.js.map +1 -1
  78. package/dist/esm/scripts/changePriority-6.js +10 -14
  79. package/dist/esm/scripts/changePriority-6.js.map +1 -1
  80. package/dist/esm/scripts/index.d.ts +1 -1
  81. package/dist/esm/scripts/index.js +1 -1
  82. package/dist/esm/scripts/moveJobsToWait-7.js +9 -3
  83. package/dist/esm/scripts/moveJobsToWait-7.js.map +1 -1
  84. package/dist/esm/scripts/{moveStalledJobsToWait-8.js → moveStalledJobsToWait-9.js} +109 -106
  85. package/dist/esm/scripts/{moveStalledJobsToWait-8.js.map → moveStalledJobsToWait-9.js.map} +1 -1
  86. package/dist/esm/scripts/moveToActive-11.js +28 -50
  87. package/dist/esm/scripts/moveToActive-11.js.map +1 -1
  88. package/dist/esm/scripts/moveToDelayed-7.js +1 -1
  89. package/dist/esm/scripts/moveToFinished-14.js +47 -70
  90. package/dist/esm/scripts/moveToFinished-14.js.map +1 -1
  91. package/dist/esm/scripts/pause-7.js +2 -1
  92. package/dist/esm/scripts/pause-7.js.map +1 -1
  93. package/dist/esm/scripts/promote-8.js +19 -5
  94. package/dist/esm/scripts/promote-8.js.map +1 -1
  95. package/dist/esm/scripts/retryJob-10.js +20 -7
  96. package/dist/esm/scripts/retryJob-10.js.map +1 -1
  97. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  98. package/package.json +1 -1
  99. package/dist/cjs/commands/includes/checkStalledJobs.lua +0 -139
  100. package/dist/cjs/commands/moveStalledJobsToWait-8.lua +0 -24
  101. package/dist/esm/commands/includes/checkStalledJobs.lua +0 -139
  102. package/dist/esm/commands/moveStalledJobsToWait-8.lua +0 -24
  103. /package/dist/esm/scripts/{moveStalledJobsToWait-8.d.ts → moveStalledJobsToWait-9.d.ts} +0 -0
@@ -11,34 +11,33 @@ const content = `--[[
11
11
  KEYS[5] 'stalled-check', (KEY)
12
12
  KEYS[6] 'meta', (KEY)
13
13
  KEYS[7] 'paused', (LIST)
14
- KEYS[8] 'event stream' (STREAM)
14
+ KEYS[8] 'marker'
15
+ KEYS[9] 'event stream' (STREAM)
15
16
  ARGV[1] Max stalled job count
16
17
  ARGV[2] queue.toKey('')
17
18
  ARGV[3] timestamp
18
19
  ARGV[4] max check time
19
20
  Events:
20
21
  'stalled' with stalled job id.
21
- ]] -- Includes
22
- --[[
23
- Move stalled jobs to wait.
24
- Input:
25
- stalledKey 'stalled' (SET)
26
- waitKey 'wait', (LIST)
27
- activeKey 'active', (LIST)
28
- failedKey 'failed', (ZSET)
29
- stalledCheckKey 'stalled-check', (KEY)
30
- metaKey 'meta', (KEY)
31
- pausedKey 'paused', (LIST)
32
- eventStreamKey 'event stream' (STREAM)
33
- maxStalledJobCount Max stalled job count
34
- queueKeyPrefix queue.toKey('')
35
- timestamp timestamp
36
- maxCheckTime max check time
37
- Events:
38
- 'stalled' with stalled job id.
39
22
  ]]
40
23
  local rcall = redis.call
41
24
  -- Includes
25
+ --[[
26
+ Function to add job in target list and add marker if needed.
27
+ ]]
28
+ -- Includes
29
+ --[[
30
+ Add marker if needed when a job is available.
31
+ ]]
32
+ local function addBaseMarkerIfNeeded(markerKey, isPaused)
33
+ if not isPaused then
34
+ rcall("ZADD", markerKey, 0, "0")
35
+ end
36
+ end
37
+ local function addJobInTargetList(targetKey, markerKey, pushCmd, isPaused, jobId)
38
+ rcall(pushCmd, targetKey, jobId)
39
+ addBaseMarkerIfNeeded(markerKey, isPaused)
40
+ end
42
41
  --[[
43
42
  Function to loop in batches.
44
43
  Just a bit of warning, some commands as ZREM
@@ -200,103 +199,107 @@ local function trimEvents(metaKey, eventStreamKey)
200
199
  rcall("XTRIM", eventStreamKey, "MAXLEN", "~", 10000)
201
200
  end
202
201
  end
203
- -- Check if we need to check for stalled jobs now.
204
- local function checkStalledJobs(stalledKey, waitKey, activeKey, failedKey,
205
- stalledCheckKey, metaKey, pausedKey,
206
- eventStreamKey, maxStalledJobCount,
207
- queueKeyPrefix, timestamp, maxCheckTime)
208
- if rcall("EXISTS", stalledCheckKey) == 1 then return {{}, {}} end
209
- rcall("SET", stalledCheckKey, timestamp, "PX", maxCheckTime)
210
- -- Trim events before emiting them to avoid trimming events emitted in this script
211
- trimEvents(metaKey, eventStreamKey)
212
- -- Move all stalled jobs to wait
213
- local stalling = rcall('SMEMBERS', stalledKey)
214
- local stalled = {}
215
- local failed = {}
216
- if (#stalling > 0) then
217
- rcall('DEL', stalledKey)
218
- local MAX_STALLED_JOB_COUNT = tonumber(maxStalledJobCount)
219
- -- Remove from active list
220
- for i, jobId in ipairs(stalling) do
221
- -- Markers in waitlist DEPRECATED in v5: Remove in v6.
222
- if string.sub(jobId, 1, 2) == "0:" then
223
- -- If the jobId is a delay marker ID we just remove it.
224
- rcall("LREM", activeKey, 1, jobId)
225
- else
226
- local jobKey = queueKeyPrefix .. jobId
227
- -- Check that the lock is also missing, then we can handle this job as really stalled.
228
- if (rcall("EXISTS", jobKey .. ":lock") == 0) then
229
- -- Remove from the active queue.
230
- local removed = rcall("LREM", activeKey, 1, jobId)
231
- if (removed > 0) then
232
- -- If this job has been stalled too many times, such as if it crashes the worker, then fail it.
233
- local stalledCount =
234
- rcall("HINCRBY", jobKey, "stalledCounter", 1)
235
- if (stalledCount > MAX_STALLED_JOB_COUNT) then
236
- local rawOpts = rcall("HGET", jobKey, "opts")
237
- local opts = cjson.decode(rawOpts)
238
- local removeOnFailType = type(opts["removeOnFail"])
239
- rcall("ZADD", failedKey, timestamp, jobId)
240
- local failedReason =
241
- "job stalled more than allowable limit"
242
- rcall("HMSET", jobKey, "failedReason", failedReason,
243
- "finishedOn", timestamp)
244
- rcall("XADD", eventStreamKey, "*", "event",
245
- "failed", "jobId", jobId, 'prev', 'active',
246
- 'failedReason', failedReason)
247
- if removeOnFailType == "number" then
248
- removeJobsByMaxCount(opts["removeOnFail"],
249
- failedKey, queueKeyPrefix)
250
- elseif removeOnFailType == "boolean" then
251
- if opts["removeOnFail"] then
252
- removeJob(jobId, false, queueKeyPrefix)
253
- rcall("ZREM", failedKey, jobId)
254
- end
255
- elseif removeOnFailType ~= "nil" then
256
- local maxAge = opts["removeOnFail"]["age"]
257
- local maxCount = opts["removeOnFail"]["count"]
258
- if maxAge ~= nil then
259
- removeJobsByMaxAge(timestamp, maxAge,
260
- failedKey, queueKeyPrefix)
261
- end
262
- if maxCount ~= nil and maxCount > 0 then
263
- removeJobsByMaxCount(maxCount, failedKey,
264
- queueKeyPrefix)
265
- end
202
+ local stalledKey = KEYS[1]
203
+ local waitKey = KEYS[2]
204
+ local activeKey = KEYS[3]
205
+ local failedKey = KEYS[4]
206
+ local stalledCheckKey = KEYS[5]
207
+ local metaKey = KEYS[6]
208
+ local pausedKey = KEYS[7]
209
+ local markerKey = KEYS[8]
210
+ local eventStreamKey = KEYS[9]
211
+ local maxStalledJobCount = ARGV[1]
212
+ local queueKeyPrefix = ARGV[2]
213
+ local timestamp = ARGV[3]
214
+ local maxCheckTime = ARGV[4]
215
+ if rcall("EXISTS", stalledCheckKey) == 1 then return {{}, {}} end
216
+ rcall("SET", stalledCheckKey, timestamp, "PX", maxCheckTime)
217
+ -- Trim events before emiting them to avoid trimming events emitted in this script
218
+ trimEvents(metaKey, eventStreamKey)
219
+ -- Move all stalled jobs to wait
220
+ local stalling = rcall('SMEMBERS', stalledKey)
221
+ local stalled = {}
222
+ local failed = {}
223
+ if (#stalling > 0) then
224
+ rcall('DEL', stalledKey)
225
+ local MAX_STALLED_JOB_COUNT = tonumber(maxStalledJobCount)
226
+ -- Remove from active list
227
+ for i, jobId in ipairs(stalling) do
228
+ -- Markers in waitlist DEPRECATED in v5: Remove in v6.
229
+ if string.sub(jobId, 1, 2) == "0:" then
230
+ -- If the jobId is a delay marker ID we just remove it.
231
+ rcall("LREM", activeKey, 1, jobId)
232
+ else
233
+ local jobKey = queueKeyPrefix .. jobId
234
+ -- Check that the lock is also missing, then we can handle this job as really stalled.
235
+ if (rcall("EXISTS", jobKey .. ":lock") == 0) then
236
+ -- Remove from the active queue.
237
+ local removed = rcall("LREM", activeKey, 1, jobId)
238
+ if (removed > 0) then
239
+ -- If this job has been stalled too many times, such as if it crashes the worker, then fail it.
240
+ local stalledCount =
241
+ rcall("HINCRBY", jobKey, "stalledCounter", 1)
242
+ if (stalledCount > MAX_STALLED_JOB_COUNT) then
243
+ local rawOpts = rcall("HGET", jobKey, "opts")
244
+ local opts = cjson.decode(rawOpts)
245
+ local removeOnFailType = type(opts["removeOnFail"])
246
+ rcall("ZADD", failedKey, timestamp, jobId)
247
+ local failedReason =
248
+ "job stalled more than allowable limit"
249
+ rcall("HMSET", jobKey, "failedReason", failedReason,
250
+ "finishedOn", timestamp)
251
+ rcall("XADD", eventStreamKey, "*", "event",
252
+ "failed", "jobId", jobId, 'prev', 'active',
253
+ 'failedReason', failedReason)
254
+ if removeOnFailType == "number" then
255
+ removeJobsByMaxCount(opts["removeOnFail"],
256
+ failedKey, queueKeyPrefix)
257
+ elseif removeOnFailType == "boolean" then
258
+ if opts["removeOnFail"] then
259
+ removeJob(jobId, false, queueKeyPrefix)
260
+ rcall("ZREM", failedKey, jobId)
261
+ end
262
+ elseif removeOnFailType ~= "nil" then
263
+ local maxAge = opts["removeOnFail"]["age"]
264
+ local maxCount = opts["removeOnFail"]["count"]
265
+ if maxAge ~= nil then
266
+ removeJobsByMaxAge(timestamp, maxAge,
267
+ failedKey, queueKeyPrefix)
268
+ end
269
+ if maxCount ~= nil and maxCount > 0 then
270
+ removeJobsByMaxCount(maxCount, failedKey,
271
+ queueKeyPrefix)
266
272
  end
267
- table.insert(failed, jobId)
268
- else
269
- local target =
270
- getTargetQueueList(metaKey, waitKey, pausedKey)
271
- -- Move the job back to the wait queue, to immediately be picked up by a waiting worker.
272
- rcall("RPUSH", target, jobId)
273
- rcall("XADD", eventStreamKey, "*", "event",
274
- "waiting", "jobId", jobId, 'prev', 'active')
275
- -- Emit the stalled event
276
- rcall("XADD", eventStreamKey, "*", "event",
277
- "stalled", "jobId", jobId)
278
- table.insert(stalled, jobId)
279
273
  end
274
+ table.insert(failed, jobId)
275
+ else
276
+ local target, isPaused=
277
+ getTargetQueueList(metaKey, waitKey, pausedKey)
278
+ -- Move the job back to the wait queue, to immediately be picked up by a waiting worker.
279
+ addJobInTargetList(target, markerKey, "RPUSH", isPaused, jobId)
280
+ rcall("XADD", eventStreamKey, "*", "event",
281
+ "waiting", "jobId", jobId, 'prev', 'active')
282
+ -- Emit the stalled event
283
+ rcall("XADD", eventStreamKey, "*", "event",
284
+ "stalled", "jobId", jobId)
285
+ table.insert(stalled, jobId)
280
286
  end
281
287
  end
282
288
  end
283
289
  end
284
290
  end
285
- -- Mark potentially stalled jobs
286
- local active = rcall('LRANGE', activeKey, 0, -1)
287
- if (#active > 0) then
288
- for from, to in batches(#active, 7000) do
289
- rcall('SADD', stalledKey, unpack(active, from, to))
290
- end
291
+ end
292
+ -- Mark potentially stalled jobs
293
+ local active = rcall('LRANGE', activeKey, 0, -1)
294
+ if (#active > 0) then
295
+ for from, to in batches(#active, 7000) do
296
+ rcall('SADD', stalledKey, unpack(active, from, to))
291
297
  end
292
- return {failed, stalled}
293
298
  end
294
- return checkStalledJobs(KEYS[1], KEYS[2], KEYS[3], KEYS[4], KEYS[5], KEYS[6],
295
- KEYS[7], KEYS[8], ARGV[1], ARGV[2], ARGV[3], ARGV[4])
296
- `;
299
+ return {failed, stalled}`;
297
300
  exports.moveStalledJobsToWait = {
298
301
  name: 'moveStalledJobsToWait',
299
302
  content,
300
- keys: 8,
303
+ keys: 9,
301
304
  };
302
- //# sourceMappingURL=moveStalledJobsToWait-8.js.map
305
+ //# sourceMappingURL=moveStalledJobsToWait-9.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"moveStalledJobsToWait-8.js","sourceRoot":"","sources":["../../../src/scripts/moveStalledJobsToWait-8.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoSf,CAAC;AACW,QAAA,qBAAqB,GAAG;IACnC,IAAI,EAAE,uBAAuB;IAC7B,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
1
+ {"version":3,"file":"moveStalledJobsToWait-9.js","sourceRoot":"","sources":["../../../src/scripts/moveStalledJobsToWait-9.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yBAuSS,CAAC;AACb,QAAA,qBAAqB,GAAG;IACnC,IAAI,EAAE,uBAAuB;IAC7B,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
@@ -33,6 +33,7 @@ const content = `--[[
33
33
  local rcall = redis.call
34
34
  local waitKey = KEYS[1]
35
35
  local activeKey = KEYS[2]
36
+ local eventStreamKey = KEYS[4]
36
37
  local rateLimiterKey = KEYS[6]
37
38
  local delayedKey = KEYS[7]
38
39
  local opts = cmsgpack.unpack(ARGV[3])
@@ -91,50 +92,15 @@ end
91
92
  --[[
92
93
  Function to move job from wait state to active.
93
94
  Input:
94
- keys[1] wait key
95
- keys[2] active key
96
- keys[3] prioritized key
97
- keys[4] stream events key
98
- keys[5] stalled key
99
- -- Rate limiting
100
- keys[6] rate limiter key
101
- keys[7] delayed key
102
- keys[8] paused key
103
- keys[9] meta key
104
- keys[10] pc priority counter
105
95
  opts - token - lock token
106
96
  opts - lockDuration
107
97
  opts - limiter
108
98
  ]]
109
- -- Includes
110
- --[[
111
- Function to push back job considering priority in front of same prioritized jobs.
112
- ]]
113
- local function pushBackJobWithPriority(prioritizedKey, priority, jobId)
114
- -- in order to put it at front of same prioritized jobs
115
- -- we consider prioritized counter as 0
116
- local score = priority * 0x100000000
117
- rcall("ZADD", prioritizedKey, score, jobId)
118
- end
119
- local function prepareJobForProcessing(keys, keyPrefix, targetKey, jobId, processedOn,
120
- maxJobs, expireTime, opts)
99
+ local function prepareJobForProcessing(keyPrefix, rateLimiterKey, eventStreamKey,
100
+ jobId, processedOn, maxJobs, opts)
121
101
  local jobKey = keyPrefix .. jobId
122
102
  -- Check if we need to perform rate limiting.
123
103
  if maxJobs then
124
- local rateLimiterKey = keys[6];
125
- -- check if we exceeded rate limit, we need to remove the job and return expireTime
126
- if expireTime > 0 then
127
- -- remove from active queue and add back to the wait list
128
- rcall("LREM", keys[2], 1, jobId)
129
- local priority = tonumber(rcall("HGET", jobKey, "priority")) or 0
130
- if priority == 0 then
131
- rcall("RPUSH", targetKey, jobId)
132
- else
133
- pushBackJobWithPriority(keys[3], priority, jobId)
134
- end
135
- -- Return when we can process more jobs
136
- return {0, 0, expireTime, 0}
137
- end
138
104
  local jobCounter = tonumber(rcall("INCR", rateLimiterKey))
139
105
  if jobCounter == 1 then
140
106
  local limiterDuration = opts['limiter'] and opts['limiter']['duration']
@@ -147,7 +113,7 @@ local function prepareJobForProcessing(keys, keyPrefix, targetKey, jobId, proces
147
113
  if opts['token'] ~= "0" then
148
114
  rcall("SET", lockKey, opts['token'], "PX", opts['lockDuration'])
149
115
  end
150
- rcall("XADD", keys[4], "*", "event", "active", "jobId", jobId, "prev", "waiting")
116
+ rcall("XADD", eventStreamKey, "*", "event", "active", "jobId", jobId, "prev", "waiting")
151
117
  rcall("HSET", jobKey, "processedOn", processedOn)
152
118
  rcall("HINCRBY", jobKey, "ats", 1)
153
119
  return {rcall("HGETALL", jobKey), jobId, 0, 0} -- get job data
@@ -159,16 +125,31 @@ end
159
125
  'waiting'
160
126
  ]]
161
127
  -- Includes
128
+ --[[
129
+ Function to add job in target list and add marker if needed.
130
+ ]]
131
+ -- Includes
132
+ --[[
133
+ Add marker if needed when a job is available.
134
+ ]]
135
+ local function addBaseMarkerIfNeeded(markerKey, isPaused)
136
+ if not isPaused then
137
+ rcall("ZADD", markerKey, 0, "0")
138
+ end
139
+ end
140
+ local function addJobInTargetList(targetKey, markerKey, pushCmd, isPaused, jobId)
141
+ rcall(pushCmd, targetKey, jobId)
142
+ addBaseMarkerIfNeeded(markerKey, isPaused)
143
+ end
162
144
  --[[
163
145
  Function to add job considering priority.
164
146
  ]]
147
+ -- Includes
165
148
  local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey, isPaused)
166
149
  local prioCounter = rcall("INCR", priorityCounterKey)
167
150
  local score = priority * 0x100000000 + bit.band(prioCounter, 0xffffffffffff)
168
151
  rcall("ZADD", prioritizedKey, score, jobId)
169
- if not isPaused then
170
- rcall("ZADD", markerKey, 0, "0")
171
- end
152
+ addBaseMarkerIfNeeded(markerKey, isPaused)
172
153
  end
173
154
  -- Try to get as much as 1000 jobs at once
174
155
  local function promoteDelayedJobs(delayedKey, markerKey, targetKey, prioritizedKey,
@@ -182,10 +163,7 @@ local function promoteDelayedJobs(delayedKey, markerKey, targetKey, prioritizedK
182
163
  tonumber(rcall("HGET", jobKey, "priority")) or 0
183
164
  if priority == 0 then
184
165
  -- LIFO or FIFO
185
- rcall("LPUSH", targetKey, jobId)
186
- if not isPaused then
187
- rcall("ZADD", markerKey, 0, "0")
188
- end
166
+ addJobInTargetList(targetKey, markerKey, "LPUSH", isPaused, jobId)
189
167
  else
190
168
  addJobWithPriority(markerKey, prioritizedKey, priority,
191
169
  jobId, priorityCounterKey, isPaused)
@@ -200,7 +178,7 @@ end
200
178
  local target, paused = getTargetQueueList(KEYS[9], waitKey, KEYS[8])
201
179
  -- Check if there are delayed jobs that we can move to wait.
202
180
  local markerKey = KEYS[11]
203
- promoteDelayedJobs(delayedKey, markerKey, target, KEYS[3], KEYS[4], ARGV[1],
181
+ promoteDelayedJobs(delayedKey, markerKey, target, KEYS[3], eventStreamKey, ARGV[1],
204
182
  ARGV[2], KEYS[10], paused)
205
183
  local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
206
184
  local expireTime = getRateLimitTTL(maxJobs, rateLimiterKey)
@@ -216,13 +194,13 @@ if jobId and string.sub(jobId, 1, 2) == "0:" then
216
194
  jobId = rcall("RPOPLPUSH", waitKey, activeKey)
217
195
  end
218
196
  if jobId then
219
- return prepareJobForProcessing(KEYS, ARGV[1], target, jobId, ARGV[2],
220
- maxJobs, expireTime, opts)
197
+ return prepareJobForProcessing(ARGV[1], rateLimiterKey, eventStreamKey, jobId, ARGV[2],
198
+ maxJobs, opts)
221
199
  else
222
200
  jobId = moveJobFromPriorityToActive(KEYS[3], activeKey, KEYS[10])
223
201
  if jobId then
224
- return prepareJobForProcessing(KEYS, ARGV[1], target, jobId, ARGV[2],
225
- maxJobs, expireTime, opts)
202
+ return prepareJobForProcessing(ARGV[1], rateLimiterKey, eventStreamKey, jobId, ARGV[2],
203
+ maxJobs, opts)
226
204
  end
227
205
  end
228
206
  -- Return the timestamp for the next delayed job if any.
@@ -1 +1 @@
1
- {"version":3,"file":"moveToActive-11.js","sourceRoot":"","sources":["../../../src/scripts/moveToActive-11.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoOf,CAAC;AACW,QAAA,YAAY,GAAG;IAC1B,IAAI,EAAE,cAAc;IACpB,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}
1
+ {"version":3,"file":"moveToActive-11.js","sourceRoot":"","sources":["../../../src/scripts/moveToActive-11.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8Mf,CAAC;AACW,QAAA,YAAY,GAAG;IAC1B,IAAI,EAAE,cAAc;IACpB,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}
@@ -29,7 +29,7 @@ local rcall = redis.call
29
29
  -- Includes
30
30
  --[[
31
31
  Add delay marker if needed.
32
- ]]
32
+ ]]
33
33
  -- Includes
34
34
  --[[
35
35
  Function to return the next delayed job timestamp.
@@ -116,6 +116,17 @@ local function getNextDelayedTimestamp(delayedKey)
116
116
  return nextTimestamp
117
117
  end
118
118
  end
119
+ --[[
120
+ Function to check for the meta.paused key to decide if we are paused or not
121
+ (since an empty list and !EXISTS are not really the same).
122
+ ]]
123
+ local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
124
+ if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
125
+ return waitKey, false
126
+ else
127
+ return pausedKey, true
128
+ end
129
+ end
119
130
  --[[
120
131
  Function to move job from prioritized state to active.
121
132
  ]]
@@ -131,50 +142,15 @@ end
131
142
  --[[
132
143
  Function to move job from wait state to active.
133
144
  Input:
134
- keys[1] wait key
135
- keys[2] active key
136
- keys[3] prioritized key
137
- keys[4] stream events key
138
- keys[5] stalled key
139
- -- Rate limiting
140
- keys[6] rate limiter key
141
- keys[7] delayed key
142
- keys[8] paused key
143
- keys[9] meta key
144
- keys[10] pc priority counter
145
145
  opts - token - lock token
146
146
  opts - lockDuration
147
147
  opts - limiter
148
148
  ]]
149
- -- Includes
150
- --[[
151
- Function to push back job considering priority in front of same prioritized jobs.
152
- ]]
153
- local function pushBackJobWithPriority(prioritizedKey, priority, jobId)
154
- -- in order to put it at front of same prioritized jobs
155
- -- we consider prioritized counter as 0
156
- local score = priority * 0x100000000
157
- rcall("ZADD", prioritizedKey, score, jobId)
158
- end
159
- local function prepareJobForProcessing(keys, keyPrefix, targetKey, jobId, processedOn,
160
- maxJobs, expireTime, opts)
149
+ local function prepareJobForProcessing(keyPrefix, rateLimiterKey, eventStreamKey,
150
+ jobId, processedOn, maxJobs, opts)
161
151
  local jobKey = keyPrefix .. jobId
162
152
  -- Check if we need to perform rate limiting.
163
153
  if maxJobs then
164
- local rateLimiterKey = keys[6];
165
- -- check if we exceeded rate limit, we need to remove the job and return expireTime
166
- if expireTime > 0 then
167
- -- remove from active queue and add back to the wait list
168
- rcall("LREM", keys[2], 1, jobId)
169
- local priority = tonumber(rcall("HGET", jobKey, "priority")) or 0
170
- if priority == 0 then
171
- rcall("RPUSH", targetKey, jobId)
172
- else
173
- pushBackJobWithPriority(keys[3], priority, jobId)
174
- end
175
- -- Return when we can process more jobs
176
- return {0, 0, expireTime, 0}
177
- end
178
154
  local jobCounter = tonumber(rcall("INCR", rateLimiterKey))
179
155
  if jobCounter == 1 then
180
156
  local limiterDuration = opts['limiter'] and opts['limiter']['duration']
@@ -187,7 +163,7 @@ local function prepareJobForProcessing(keys, keyPrefix, targetKey, jobId, proces
187
163
  if opts['token'] ~= "0" then
188
164
  rcall("SET", lockKey, opts['token'], "PX", opts['lockDuration'])
189
165
  end
190
- rcall("XADD", keys[4], "*", "event", "active", "jobId", jobId, "prev", "waiting")
166
+ rcall("XADD", eventStreamKey, "*", "event", "active", "jobId", jobId, "prev", "waiting")
191
167
  rcall("HSET", jobKey, "processedOn", processedOn)
192
168
  rcall("HINCRBY", jobKey, "ats", 1)
193
169
  return {rcall("HGETALL", jobKey), jobId, 0, 0} -- get job data
@@ -202,7 +178,7 @@ end
202
178
  -- Includes
203
179
  --[[
204
180
  Add delay marker if needed.
205
- ]]
181
+ ]]
206
182
  -- Includes
207
183
  local function addDelayMarkerIfNeeded(markerKey, delayedKey)
208
184
  local nextTimestamp = getNextDelayedTimestamp(delayedKey)
@@ -213,33 +189,37 @@ local function addDelayMarkerIfNeeded(markerKey, delayedKey)
213
189
  end
214
190
  end
215
191
  --[[
216
- Function to check for the meta.paused key to decide if we are paused or not
217
- (since an empty list and !EXISTS are not really the same).
192
+ Function to add job in target list and add marker if needed.
218
193
  ]]
219
- local function isQueuePaused(queueMetaKey)
220
- return rcall("HEXISTS", queueMetaKey, "paused") == 1
194
+ -- Includes
195
+ --[[
196
+ Add marker if needed when a job is available.
197
+ ]]
198
+ local function addBaseMarkerIfNeeded(markerKey, isPaused)
199
+ if not isPaused then
200
+ rcall("ZADD", markerKey, 0, "0")
201
+ end
202
+ end
203
+ local function addJobInTargetList(targetKey, markerKey, pushCmd, isPaused, jobId)
204
+ rcall(pushCmd, targetKey, jobId)
205
+ addBaseMarkerIfNeeded(markerKey, isPaused)
221
206
  end
222
207
  --[[
223
208
  Function to add job considering priority.
224
209
  ]]
210
+ -- Includes
225
211
  local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey, isPaused)
226
212
  local prioCounter = rcall("INCR", priorityCounterKey)
227
213
  local score = priority * 0x100000000 + bit.band(prioCounter, 0xffffffffffff)
228
214
  rcall("ZADD", prioritizedKey, score, jobId)
229
- if not isPaused then
230
- rcall("ZADD", markerKey, 0, "0")
231
- end
215
+ addBaseMarkerIfNeeded(markerKey, isPaused)
232
216
  end
233
217
  --[[
234
218
  Function to check for the meta.paused key to decide if we are paused or not
235
219
  (since an empty list and !EXISTS are not really the same).
236
220
  ]]
237
- local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
238
- if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
239
- return waitKey, false
240
- else
241
- return pausedKey, true
242
- end
221
+ local function isQueuePaused(queueMetaKey)
222
+ return rcall("HEXISTS", queueMetaKey, "paused") == 1
243
223
  end
244
224
  local function moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey,
245
225
  parentKey, parentId, timestamp)
@@ -264,11 +244,10 @@ local function moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey,
264
244
  addDelayMarkerIfNeeded(parentMarkerKey, parentDelayedKey)
265
245
  else
266
246
  if priority == 0 then
267
- local parentTarget, _paused =
247
+ local parentTarget, isParentPaused =
268
248
  getTargetQueueList(parentMetaKey, parentWaitKey,
269
249
  parentPausedKey)
270
- rcall("RPUSH", parentTarget, parentId)
271
- rcall("ZADD", parentMarkerKey, 0, "0")
250
+ addJobInTargetList(parentTarget, parentMarkerKey, "RPUSH", isParentPaused, parentId)
272
251
  else
273
252
  local isPaused = isQueuePaused(parentMetaKey)
274
253
  addJobWithPriority(parentMarkerKey,
@@ -328,10 +307,7 @@ local function promoteDelayedJobs(delayedKey, markerKey, targetKey, prioritizedK
328
307
  tonumber(rcall("HGET", jobKey, "priority")) or 0
329
308
  if priority == 0 then
330
309
  -- LIFO or FIFO
331
- rcall("LPUSH", targetKey, jobId)
332
- if not isPaused then
333
- rcall("ZADD", markerKey, 0, "0")
334
- end
310
+ addJobInTargetList(targetKey, markerKey, "LPUSH", isPaused, jobId)
335
311
  else
336
312
  addJobWithPriority(markerKey, prioritizedKey, priority,
337
313
  jobId, priorityCounterKey, isPaused)
@@ -543,9 +519,10 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
543
519
  -- Remove from active list (if not active we shall return error)
544
520
  local numRemovedElements = rcall("LREM", KEYS[2], -1, jobId)
545
521
  if (numRemovedElements < 1) then return -3 end
522
+ local eventStreamKey = KEYS[4]
546
523
  local metaKey = KEYS[9]
547
524
  -- Trim events before emiting them to avoid trimming events emitted in this script
548
- trimEvents(metaKey, KEYS[4])
525
+ trimEvents(metaKey, eventStreamKey)
549
526
  -- If job has a parent we need to
550
527
  -- 1) remove this job id from parents dependencies
551
528
  -- 2) move the job Id to parent "processed" set
@@ -599,11 +576,11 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
599
576
  removeParentDependencyKey(jobIdKey, false, parentKey)
600
577
  end
601
578
  end
602
- rcall("XADD", KEYS[4], "*", "event", ARGV[5], "jobId", jobId, ARGV[3],
579
+ rcall("XADD", eventStreamKey, "*", "event", ARGV[5], "jobId", jobId, ARGV[3],
603
580
  ARGV[4])
604
581
  if ARGV[5] == "failed" then
605
582
  if tonumber(attemptsMade) >= tonumber(attempts) then
606
- rcall("XADD", KEYS[4], "*", "event", "retries-exhausted", "jobId",
583
+ rcall("XADD", eventStreamKey, "*", "event", "retries-exhausted", "jobId",
607
584
  jobId, "attemptsMade", attemptsMade)
608
585
  end
609
586
  end
@@ -616,7 +593,7 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
616
593
  if (ARGV[6] == "1") then
617
594
  local target, paused = getTargetQueueList(metaKey, KEYS[1], KEYS[8])
618
595
  -- Check if there are delayed jobs that can be promoted
619
- promoteDelayedJobs(KEYS[7], KEYS[14], target, KEYS[3], KEYS[4], ARGV[7],
596
+ promoteDelayedJobs(KEYS[7], KEYS[14], target, KEYS[3], eventStreamKey, ARGV[7],
620
597
  timestamp, KEYS[10], paused)
621
598
  local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
622
599
  -- Check if we are rate limited first.
@@ -634,20 +611,20 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
634
611
  if jobId == "0:0" then
635
612
  jobId = moveJobFromPriorityToActive(KEYS[3], KEYS[2],
636
613
  KEYS[10])
637
- return prepareJobForProcessing(KEYS, ARGV[7], target, jobId,
614
+ return prepareJobForProcessing(ARGV[7], KEYS[6], eventStreamKey, jobId,
638
615
  timestamp, maxJobs,
639
- expireTime, opts)
616
+ opts)
640
617
  end
641
618
  else
642
- return prepareJobForProcessing(KEYS, ARGV[7], target, jobId,
643
- timestamp, maxJobs, expireTime,
619
+ return prepareJobForProcessing(ARGV[7], KEYS[6], eventStreamKey, jobId,
620
+ timestamp, maxJobs,
644
621
  opts)
645
622
  end
646
623
  else
647
624
  jobId = moveJobFromPriorityToActive(KEYS[3], KEYS[2], KEYS[10])
648
625
  if jobId then
649
- return prepareJobForProcessing(KEYS, ARGV[7], target, jobId,
650
- timestamp, maxJobs, expireTime,
626
+ return prepareJobForProcessing(ARGV[7], KEYS[6], eventStreamKey, jobId,
627
+ timestamp, maxJobs,
651
628
  opts)
652
629
  end
653
630
  end
@@ -665,7 +642,7 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
665
642
  if activeLen == 0 then
666
643
  local prioritizedLen = rcall("ZCARD", KEYS[3])
667
644
  if prioritizedLen == 0 then
668
- rcall("XADD", KEYS[4], "*", "event", "drained")
645
+ rcall("XADD", eventStreamKey, "*", "event", "drained")
669
646
  end
670
647
  end
671
648
  end