bullmq 5.41.8 → 5.42.0

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 (98) hide show
  1. package/dist/cjs/classes/job-scheduler.js +14 -18
  2. package/dist/cjs/classes/job-scheduler.js.map +1 -1
  3. package/dist/cjs/classes/job.js +20 -13
  4. package/dist/cjs/classes/job.js.map +1 -1
  5. package/dist/cjs/classes/scripts.js +33 -3
  6. package/dist/cjs/classes/scripts.js.map +1 -1
  7. package/dist/cjs/classes/worker.js +4 -8
  8. package/dist/cjs/classes/worker.js.map +1 -1
  9. package/dist/cjs/commands/addJobScheduler-10.lua +9 -36
  10. package/dist/cjs/commands/includes/addJobFromScheduler.lua +41 -0
  11. package/dist/cjs/commands/includes/moveParentFromWaitingChildrenToFailed.lua +11 -1
  12. package/dist/cjs/commands/includes/removeJobKeys.lua +2 -2
  13. package/dist/cjs/commands/includes/storeJobScheduler.lua +1 -1
  14. package/dist/cjs/commands/moveStalledJobsToWait-9.lua +5 -0
  15. package/dist/cjs/commands/moveToFinished-14.lua +2 -0
  16. package/dist/cjs/commands/moveToWaitingChildren-8.lua +133 -0
  17. package/dist/cjs/commands/removeJob-3.lua +10 -0
  18. package/dist/{esm/commands/updateJobScheduler-7.lua → cjs/commands/updateJobScheduler-11.lua} +28 -21
  19. package/dist/cjs/scripts/addJobScheduler-10.js +80 -70
  20. package/dist/cjs/scripts/addJobScheduler-10.js.map +1 -1
  21. package/dist/cjs/scripts/addRepeatableJob-2.js +2 -2
  22. package/dist/cjs/scripts/cleanJobsInSet-3.js +2 -2
  23. package/dist/cjs/scripts/drain-5.js +2 -2
  24. package/dist/cjs/scripts/index.js +2 -2
  25. package/dist/cjs/scripts/index.js.map +1 -1
  26. package/dist/cjs/scripts/moveStalledJobsToWait-9.js +17 -3
  27. package/dist/cjs/scripts/moveStalledJobsToWait-9.js.map +1 -1
  28. package/dist/cjs/scripts/moveToFinished-14.js +14 -3
  29. package/dist/cjs/scripts/moveToFinished-14.js.map +1 -1
  30. package/dist/cjs/scripts/moveToWaitingChildren-8.js +549 -0
  31. package/dist/cjs/scripts/moveToWaitingChildren-8.js.map +1 -0
  32. package/dist/cjs/scripts/obliterate-2.js +2 -2
  33. package/dist/cjs/scripts/removeChildDependency-1.js +2 -2
  34. package/dist/cjs/scripts/removeJob-3.js +10 -2
  35. package/dist/cjs/scripts/removeJob-3.js.map +1 -1
  36. package/dist/cjs/scripts/removeJobScheduler-3.js +2 -2
  37. package/dist/cjs/scripts/removeRepeatable-3.js +2 -2
  38. package/dist/cjs/scripts/updateJobScheduler-11.js +246 -0
  39. package/dist/cjs/scripts/updateJobScheduler-11.js.map +1 -0
  40. package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
  41. package/dist/cjs/version.js +1 -1
  42. package/dist/esm/classes/job-scheduler.js +14 -18
  43. package/dist/esm/classes/job-scheduler.js.map +1 -1
  44. package/dist/esm/classes/job.js +20 -13
  45. package/dist/esm/classes/job.js.map +1 -1
  46. package/dist/esm/classes/scripts.d.ts +1 -1
  47. package/dist/esm/classes/scripts.js +33 -3
  48. package/dist/esm/classes/scripts.js.map +1 -1
  49. package/dist/esm/classes/worker.js +4 -8
  50. package/dist/esm/classes/worker.js.map +1 -1
  51. package/dist/esm/commands/addJobScheduler-10.lua +9 -36
  52. package/dist/esm/commands/includes/addJobFromScheduler.lua +41 -0
  53. package/dist/esm/commands/includes/moveParentFromWaitingChildrenToFailed.lua +11 -1
  54. package/dist/esm/commands/includes/removeJobKeys.lua +2 -2
  55. package/dist/esm/commands/includes/storeJobScheduler.lua +1 -1
  56. package/dist/esm/commands/moveStalledJobsToWait-9.lua +5 -0
  57. package/dist/esm/commands/moveToFinished-14.lua +2 -0
  58. package/dist/esm/commands/moveToWaitingChildren-8.lua +133 -0
  59. package/dist/esm/commands/removeJob-3.lua +10 -0
  60. package/dist/{cjs/commands/updateJobScheduler-7.lua → esm/commands/updateJobScheduler-11.lua} +28 -21
  61. package/dist/esm/scripts/addJobScheduler-10.js +80 -70
  62. package/dist/esm/scripts/addJobScheduler-10.js.map +1 -1
  63. package/dist/esm/scripts/addRepeatableJob-2.js +2 -2
  64. package/dist/esm/scripts/cleanJobsInSet-3.js +2 -2
  65. package/dist/esm/scripts/drain-5.js +2 -2
  66. package/dist/esm/scripts/index.d.ts +2 -2
  67. package/dist/esm/scripts/index.js +2 -2
  68. package/dist/esm/scripts/index.js.map +1 -1
  69. package/dist/esm/scripts/moveStalledJobsToWait-9.js +17 -3
  70. package/dist/esm/scripts/moveStalledJobsToWait-9.js.map +1 -1
  71. package/dist/esm/scripts/moveToFinished-14.js +14 -3
  72. package/dist/esm/scripts/moveToFinished-14.js.map +1 -1
  73. package/dist/esm/scripts/moveToWaitingChildren-8.js +546 -0
  74. package/dist/esm/scripts/moveToWaitingChildren-8.js.map +1 -0
  75. package/dist/esm/scripts/obliterate-2.js +2 -2
  76. package/dist/esm/scripts/removeChildDependency-1.js +2 -2
  77. package/dist/esm/scripts/removeJob-3.js +10 -2
  78. package/dist/esm/scripts/removeJob-3.js.map +1 -1
  79. package/dist/esm/scripts/removeJobScheduler-3.js +2 -2
  80. package/dist/esm/scripts/removeRepeatable-3.js +2 -2
  81. package/dist/esm/scripts/updateJobScheduler-11.js +243 -0
  82. package/dist/esm/scripts/updateJobScheduler-11.js.map +1 -0
  83. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  84. package/dist/esm/version.d.ts +1 -1
  85. package/dist/esm/version.js +1 -1
  86. package/package.json +1 -1
  87. package/dist/cjs/commands/moveToWaitingChildren-5.lua +0 -68
  88. package/dist/cjs/scripts/moveToWaitingChildren-5.js +0 -83
  89. package/dist/cjs/scripts/moveToWaitingChildren-5.js.map +0 -1
  90. package/dist/cjs/scripts/updateJobScheduler-7.js +0 -144
  91. package/dist/cjs/scripts/updateJobScheduler-7.js.map +0 -1
  92. package/dist/esm/commands/moveToWaitingChildren-5.lua +0 -68
  93. package/dist/esm/scripts/moveToWaitingChildren-5.js +0 -80
  94. package/dist/esm/scripts/moveToWaitingChildren-5.js.map +0 -1
  95. package/dist/esm/scripts/updateJobScheduler-7.js +0 -141
  96. package/dist/esm/scripts/updateJobScheduler-7.js.map +0 -1
  97. /package/dist/esm/scripts/{moveToWaitingChildren-5.d.ts → moveToWaitingChildren-8.d.ts} +0 -0
  98. /package/dist/esm/scripts/{updateJobScheduler-7.d.ts → updateJobScheduler-11.d.ts} +0 -0
@@ -0,0 +1,549 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.moveToWaitingChildren = void 0;
4
+ const content = `--[[
5
+ Moves job from active to waiting children set.
6
+ Input:
7
+ KEYS[1] active key
8
+ KEYS[2] wait-children key
9
+ KEYS[3] job key
10
+ KEYS[4] job dependencies key
11
+ KEYS[5] job unsuccessful key
12
+ KEYS[6] stalled key
13
+ KEYS[7] failed key
14
+ KEYS[8] events key
15
+ ARGV[1] token
16
+ ARGV[2] child key
17
+ ARGV[3] timestamp
18
+ ARGV[4] jobId
19
+ ARGV[5] prefix
20
+ Output:
21
+ 0 - OK
22
+ 1 - There are not pending dependencies.
23
+ -1 - Missing job.
24
+ -2 - Missing lock
25
+ -3 - Job not in active set
26
+ ]]
27
+ local rcall = redis.call
28
+ local activeKey = KEYS[1]
29
+ local waitingChildrenKey = KEYS[2]
30
+ local jobKey = KEYS[3]
31
+ local jobDependenciesKey = KEYS[4]
32
+ local jobUnsuccessfulKey = KEYS[5]
33
+ local stalledKey = KEYS[6]
34
+ local failedKey = KEYS[7]
35
+ local timestamp = ARGV[3]
36
+ local jobId = ARGV[4]
37
+ --- Includes
38
+ --[[
39
+ Function to recursively move from waitingChildren to failed.
40
+ ]]
41
+ -- Includes
42
+ --[[
43
+ Validate and move parent to active if needed.
44
+ ]]
45
+ -- Includes
46
+ --[[
47
+ Add delay marker if needed.
48
+ ]]
49
+ -- Includes
50
+ --[[
51
+ Function to return the next delayed job timestamp.
52
+ ]]
53
+ local function getNextDelayedTimestamp(delayedKey)
54
+ local result = rcall("ZRANGE", delayedKey, 0, 0, "WITHSCORES")
55
+ if #result then
56
+ local nextTimestamp = tonumber(result[2])
57
+ if nextTimestamp ~= nil then
58
+ return nextTimestamp / 0x1000
59
+ end
60
+ end
61
+ end
62
+ local function addDelayMarkerIfNeeded(markerKey, delayedKey)
63
+ local nextTimestamp = getNextDelayedTimestamp(delayedKey)
64
+ if nextTimestamp ~= nil then
65
+ -- Replace the score of the marker with the newest known
66
+ -- next timestamp.
67
+ rcall("ZADD", markerKey, nextTimestamp, "1")
68
+ end
69
+ end
70
+ --[[
71
+ Function to add job in target list and add marker if needed.
72
+ ]]
73
+ -- Includes
74
+ --[[
75
+ Add marker if needed when a job is available.
76
+ ]]
77
+ local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
78
+ if not isPausedOrMaxed then
79
+ rcall("ZADD", markerKey, 0, "0")
80
+ end
81
+ end
82
+ local function addJobInTargetList(targetKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
83
+ rcall(pushCmd, targetKey, jobId)
84
+ addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
85
+ end
86
+ --[[
87
+ Function to add job considering priority.
88
+ ]]
89
+ -- Includes
90
+ --[[
91
+ Function to get priority score.
92
+ ]]
93
+ local function getPriorityScore(priority, priorityCounterKey)
94
+ local prioCounter = rcall("INCR", priorityCounterKey)
95
+ return priority * 0x100000000 + prioCounter % 0x100000000
96
+ end
97
+ local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey,
98
+ isPausedOrMaxed)
99
+ local score = getPriorityScore(priority, priorityCounterKey)
100
+ rcall("ZADD", prioritizedKey, score, jobId)
101
+ addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
102
+ end
103
+ --[[
104
+ Function to check if queue is paused or maxed
105
+ (since an empty list and !EXISTS are not really the same).
106
+ ]]
107
+ local function isQueuePausedOrMaxed(queueMetaKey, activeKey)
108
+ local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency")
109
+ if queueAttributes[1] then
110
+ return true
111
+ else
112
+ if queueAttributes[2] then
113
+ local activeCount = rcall("LLEN", activeKey)
114
+ return activeCount >= tonumber(queueAttributes[2])
115
+ end
116
+ end
117
+ return false
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, activeKey, waitKey, pausedKey)
124
+ local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency")
125
+ if queueAttributes[1] then
126
+ return pausedKey, true
127
+ else
128
+ if queueAttributes[2] then
129
+ local activeCount = rcall("LLEN", activeKey)
130
+ if activeCount >= tonumber(queueAttributes[2]) then
131
+ return waitKey, true
132
+ else
133
+ return waitKey, false
134
+ end
135
+ end
136
+ end
137
+ return waitKey, false
138
+ end
139
+ local function moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey,
140
+ parentKey, parentId, timestamp)
141
+ local isParentActive = rcall("ZSCORE",
142
+ parentQueueKey .. ":waiting-children", parentId)
143
+ if rcall("SCARD", parentDependenciesKey) == 0 and isParentActive then
144
+ rcall("ZREM", parentQueueKey .. ":waiting-children", parentId)
145
+ local parentWaitKey = parentQueueKey .. ":wait"
146
+ local parentPausedKey = parentQueueKey .. ":paused"
147
+ local parentActiveKey = parentQueueKey .. ":active"
148
+ local parentMetaKey = parentQueueKey .. ":meta"
149
+ local parentMarkerKey = parentQueueKey .. ":marker"
150
+ local jobAttributes = rcall("HMGET", parentKey, "priority", "delay")
151
+ local priority = tonumber(jobAttributes[1]) or 0
152
+ local delay = tonumber(jobAttributes[2]) or 0
153
+ if delay > 0 then
154
+ local delayedTimestamp = tonumber(timestamp) + delay
155
+ local score = delayedTimestamp * 0x1000
156
+ local parentDelayedKey = parentQueueKey .. ":delayed"
157
+ rcall("ZADD", parentDelayedKey, score, parentId)
158
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "delayed",
159
+ "jobId", parentId, "delay", delayedTimestamp)
160
+ addDelayMarkerIfNeeded(parentMarkerKey, parentDelayedKey)
161
+ else
162
+ if priority == 0 then
163
+ local parentTarget, isParentPausedOrMaxed =
164
+ getTargetQueueList(parentMetaKey, parentActiveKey, parentWaitKey,
165
+ parentPausedKey)
166
+ addJobInTargetList(parentTarget, parentMarkerKey, "RPUSH", isParentPausedOrMaxed,
167
+ parentId)
168
+ else
169
+ local isPausedOrMaxed = isQueuePausedOrMaxed(parentMetaKey, parentActiveKey)
170
+ addJobWithPriority(parentMarkerKey,
171
+ parentQueueKey .. ":prioritized", priority,
172
+ parentId, parentQueueKey .. ":pc", isPausedOrMaxed)
173
+ end
174
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "waiting",
175
+ "jobId", parentId, "prev", "waiting-children")
176
+ end
177
+ end
178
+ end
179
+ --[[
180
+ Function to remove deduplication key if needed.
181
+ ]]
182
+ local function removeDeduplicationKeyIfNeeded(prefixKey, deduplicationId)
183
+ if deduplicationId then
184
+ local deduplicationKey = prefixKey .. "de:" .. deduplicationId
185
+ local pttl = rcall("PTTL", deduplicationKey)
186
+ if pttl == 0 or pttl == -1 then
187
+ rcall("DEL", deduplicationKey)
188
+ end
189
+ end
190
+ end
191
+ --[[
192
+ Functions to remove jobs when removeOnFail option is provided.
193
+ ]]
194
+ -- Includes
195
+ --[[
196
+ Function to remove job.
197
+ ]]
198
+ -- Includes
199
+ --[[
200
+ Function to remove deduplication key.
201
+ ]]
202
+ local function removeDeduplicationKey(prefixKey, jobKey)
203
+ local deduplicationId = rcall("HGET", jobKey, "deid")
204
+ if deduplicationId then
205
+ local deduplicationKey = prefixKey .. "de:" .. deduplicationId
206
+ rcall("DEL", deduplicationKey)
207
+ end
208
+ end
209
+ --[[
210
+ Function to remove job keys.
211
+ ]]
212
+ local function removeJobKeys(jobKey)
213
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
214
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
215
+ end
216
+ --[[
217
+ Check if this job has a parent. If so we will just remove it from
218
+ the parent child list, but if it is the last child we should move the parent to "wait/paused"
219
+ which requires code from "moveToFinished"
220
+ ]]
221
+ -- Includes
222
+ --[[
223
+ Functions to destructure job key.
224
+ Just a bit of warning, these functions may be a bit slow and affect performance significantly.
225
+ ]]
226
+ local getJobIdFromKey = function (jobKey)
227
+ return string.match(jobKey, ".*:(.*)")
228
+ end
229
+ local getJobKeyPrefix = function (jobKey, jobId)
230
+ return string.sub(jobKey, 0, #jobKey - #jobId)
231
+ end
232
+ local function moveParentToWait(parentPrefix, parentId, emitEvent)
233
+ local parentTarget, isPausedOrMaxed = getTargetQueueList(parentPrefix .. "meta", parentPrefix .. "active",
234
+ parentPrefix .. "wait", parentPrefix .. "paused")
235
+ addJobInTargetList(parentTarget, parentPrefix .. "marker", "RPUSH", isPausedOrMaxed, parentId)
236
+ if emitEvent then
237
+ local parentEventStream = parentPrefix .. "events"
238
+ rcall("XADD", parentEventStream, "*", "event", "waiting", "jobId", parentId, "prev", "waiting-children")
239
+ end
240
+ end
241
+ local function removeParentDependencyKey(jobKey, hard, parentKey, baseKey, debounceId)
242
+ if parentKey then
243
+ local parentDependenciesKey = parentKey .. ":dependencies"
244
+ local result = rcall("SREM", parentDependenciesKey, jobKey)
245
+ if result > 0 then
246
+ local pendingDependencies = rcall("SCARD", parentDependenciesKey)
247
+ if pendingDependencies == 0 then
248
+ local parentId = getJobIdFromKey(parentKey)
249
+ local parentPrefix = getJobKeyPrefix(parentKey, parentId)
250
+ local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
251
+ if numRemovedElements == 1 then
252
+ if hard then -- remove parent in same queue
253
+ if parentPrefix == baseKey then
254
+ removeParentDependencyKey(parentKey, hard, nil, baseKey, nil)
255
+ removeJobKeys(parentKey)
256
+ if debounceId then
257
+ rcall("DEL", parentPrefix .. "de:" .. debounceId)
258
+ end
259
+ else
260
+ moveParentToWait(parentPrefix, parentId)
261
+ end
262
+ else
263
+ moveParentToWait(parentPrefix, parentId, true)
264
+ end
265
+ end
266
+ end
267
+ return true
268
+ end
269
+ else
270
+ local parentAttributes = rcall("HMGET", jobKey, "parentKey", "deid")
271
+ local missedParentKey = parentAttributes[1]
272
+ if( (type(missedParentKey) == "string") and missedParentKey ~= ""
273
+ and (rcall("EXISTS", missedParentKey) == 1)) then
274
+ local parentDependenciesKey = missedParentKey .. ":dependencies"
275
+ local result = rcall("SREM", parentDependenciesKey, jobKey)
276
+ if result > 0 then
277
+ local pendingDependencies = rcall("SCARD", parentDependenciesKey)
278
+ if pendingDependencies == 0 then
279
+ local parentId = getJobIdFromKey(missedParentKey)
280
+ local parentPrefix = getJobKeyPrefix(missedParentKey, parentId)
281
+ local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
282
+ if numRemovedElements == 1 then
283
+ if hard then
284
+ if parentPrefix == baseKey then
285
+ removeParentDependencyKey(missedParentKey, hard, nil, baseKey, nil)
286
+ removeJobKeys(missedParentKey)
287
+ if parentAttributes[2] then
288
+ rcall("DEL", parentPrefix .. "de:" .. parentAttributes[2])
289
+ end
290
+ else
291
+ moveParentToWait(parentPrefix, parentId)
292
+ end
293
+ else
294
+ moveParentToWait(parentPrefix, parentId, true)
295
+ end
296
+ end
297
+ end
298
+ return true
299
+ end
300
+ end
301
+ end
302
+ return false
303
+ end
304
+ local function removeJob(jobId, hard, baseKey, shouldRemoveDeduplicationKey)
305
+ local jobKey = baseKey .. jobId
306
+ removeParentDependencyKey(jobKey, hard, nil, baseKey)
307
+ if shouldRemoveDeduplicationKey then
308
+ removeDeduplicationKey(baseKey, jobKey)
309
+ end
310
+ removeJobKeys(jobKey)
311
+ end
312
+ --[[
313
+ Functions to remove jobs by max age.
314
+ ]]
315
+ -- Includes
316
+ local function removeJobsByMaxAge(timestamp, maxAge, targetSet, prefix,
317
+ shouldRemoveDebounceKey)
318
+ local start = timestamp - maxAge * 1000
319
+ local jobIds = rcall("ZREVRANGEBYSCORE", targetSet, start, "-inf")
320
+ for i, jobId in ipairs(jobIds) do
321
+ removeJob(jobId, false, prefix, false --[[remove debounce key]])
322
+ end
323
+ rcall("ZREMRANGEBYSCORE", targetSet, "-inf", start)
324
+ end
325
+ --[[
326
+ Functions to remove jobs by max count.
327
+ ]]
328
+ -- Includes
329
+ local function removeJobsByMaxCount(maxCount, targetSet, prefix)
330
+ local start = maxCount
331
+ local jobIds = rcall("ZREVRANGE", targetSet, start, -1)
332
+ for i, jobId in ipairs(jobIds) do
333
+ removeJob(jobId, false, prefix, false --[[remove debounce key]])
334
+ end
335
+ rcall("ZREMRANGEBYRANK", targetSet, 0, -(maxCount + 1))
336
+ end
337
+ local function removeJobsOnFail(queueKeyPrefix, failedKey, jobId, opts, timestamp)
338
+ local removeOnFailType = type(opts["removeOnFail"])
339
+ if removeOnFailType == "number" then
340
+ removeJobsByMaxCount(opts["removeOnFail"],
341
+ failedKey, queueKeyPrefix)
342
+ elseif removeOnFailType == "boolean" then
343
+ if opts["removeOnFail"] then
344
+ removeJob(jobId, false, queueKeyPrefix,
345
+ false --[[remove debounce key]])
346
+ rcall("ZREM", failedKey, jobId)
347
+ end
348
+ elseif removeOnFailType ~= "nil" then
349
+ local maxAge = opts["removeOnFail"]["age"]
350
+ local maxCount = opts["removeOnFail"]["count"]
351
+ if maxAge ~= nil then
352
+ removeJobsByMaxAge(timestamp, maxAge,
353
+ failedKey, queueKeyPrefix)
354
+ end
355
+ if maxCount ~= nil and maxCount > 0 then
356
+ removeJobsByMaxCount(maxCount, failedKey,
357
+ queueKeyPrefix)
358
+ end
359
+ end
360
+ end
361
+ local function moveParentFromWaitingChildrenToFailed(parentQueueKey, parentKey, parentId, jobIdKey, timestamp)
362
+ local parentWaitingChildrenKey = parentQueueKey .. ":waiting-children"
363
+ local parentDelayedKey = parentQueueKey .. ":delayed"
364
+ if rcall("ZSCORE", parentWaitingChildrenKey, parentId) ~= false then
365
+ rcall("ZREM", parentWaitingChildrenKey, parentId)
366
+ local parentQueuePrefix = parentQueueKey .. ":"
367
+ local parentFailedKey = parentQueueKey .. ":failed"
368
+ rcall("ZADD", parentFailedKey, timestamp, parentId)
369
+ local failedReason = "child " .. jobIdKey .. " failed"
370
+ rcall("HSET", parentKey, "failedReason", failedReason, "finishedOn", timestamp)
371
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "failed", "jobId", parentId, "failedReason",
372
+ failedReason, "prev", "waiting-children")
373
+ local jobAttributes = rcall("HMGET", parentKey, "parent", "deid", "opts")
374
+ removeDeduplicationKeyIfNeeded(parentQueueKey .. ":", jobAttributes[2])
375
+ if jobAttributes[1] then
376
+ local parentData = cjson.decode(jobAttributes[1])
377
+ local grandParentKey = parentData['queueKey'] .. ':' .. parentData['id']
378
+ local grandParentUnsuccesssful = grandParentKey .. ":unsuccessful"
379
+ rcall("ZADD", grandParentUnsuccesssful, timestamp, parentKey)
380
+ if parentData['fpof'] then
381
+ moveParentFromWaitingChildrenToFailed(
382
+ parentData['queueKey'],
383
+ parentData['queueKey'] .. ':' .. parentData['id'],
384
+ parentData['id'],
385
+ parentKey,
386
+ timestamp
387
+ )
388
+ elseif parentData['idof'] or parentData['rdof'] then
389
+ local grandParentKey = parentData['queueKey'] .. ':' .. parentData['id']
390
+ local grandParentDependenciesSet = grandParentKey .. ":dependencies"
391
+ if rcall("SREM", grandParentDependenciesSet, parentKey) == 1 then
392
+ moveParentToWaitIfNeeded(parentData['queueKey'], grandParentDependenciesSet,
393
+ grandParentKey, parentData['id'], timestamp)
394
+ if parentData['idof'] then
395
+ local grandParentFailedSet = grandParentKey .. ":failed"
396
+ rcall("HSET", grandParentFailedSet, parentKey, failedReason)
397
+ end
398
+ end
399
+ end
400
+ end
401
+ local parentRawOpts = jobAttributes[3]
402
+ local parentOpts = cjson.decode(parentRawOpts)
403
+ removeJobsOnFail(parentQueuePrefix, parentFailedKey, parentId, parentOpts, timestamp)
404
+ elseif rcall("ZSCORE", parentDelayedKey, parentId) ~= false then
405
+ rcall("ZREM", parentDelayedKey, parentId)
406
+ local parentQueuePrefix = parentQueueKey .. ":"
407
+ local parentFailedKey = parentQueueKey .. ":failed"
408
+ rcall("ZADD", parentFailedKey, timestamp, parentId)
409
+ local failedReason = "child " .. jobIdKey .. " failed"
410
+ rcall("HMSET", parentKey, "failedReason", failedReason, "finishedOn", timestamp)
411
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "failed", "jobId", parentId, "failedReason",
412
+ failedReason, "prev", "delayed")
413
+ local jobAttributes = rcall("HMGET", parentKey, "parent", "deid", "opts")
414
+ removeDeduplicationKeyIfNeeded(parentQueueKey .. ":", jobAttributes[2])
415
+ if jobAttributes[1] then
416
+ local parentData = cjson.decode(jobAttributes[1])
417
+ if parentData['fpof'] then
418
+ moveParentFromWaitingChildrenToFailed(
419
+ parentData['queueKey'],
420
+ parentData['queueKey'] .. ':' .. parentData['id'],
421
+ parentData['id'],
422
+ parentKey,
423
+ timestamp
424
+ )
425
+ elseif parentData['idof'] or parentData['rdof'] then
426
+ local grandParentKey = parentData['queueKey'] .. ':' .. parentData['id']
427
+ local grandParentDependenciesSet = grandParentKey .. ":dependencies"
428
+ if rcall("SREM", grandParentDependenciesSet, parentKey) == 1 then
429
+ moveParentToWaitIfNeeded(parentData['queueKey'], grandParentDependenciesSet,
430
+ grandParentKey, parentData['id'], timestamp)
431
+ if parentData['idof'] then
432
+ local grandParentFailedSet = grandParentKey .. ":failed"
433
+ rcall("HSET", grandParentFailedSet, parentKey, failedReason)
434
+ end
435
+ end
436
+ end
437
+ end
438
+ local parentRawOpts = jobAttributes[3]
439
+ local parentOpts = cjson.decode(parentRawOpts)
440
+ removeJobsOnFail(parentQueuePrefix, parentFailedKey, parentId, parentOpts, timestamp)
441
+ else
442
+ local grandParentKey = rcall("HGET", parentKey, "parentKey")
443
+ if grandParentKey then
444
+ local grandParentUnsuccesssfulSet = grandParentKey .. ":unsuccessful"
445
+ rcall("ZADD", grandParentUnsuccesssfulSet, timestamp, parentKey)
446
+ end
447
+ end
448
+ end
449
+ local function removeLock(jobKey, stalledKey, token, jobId)
450
+ if token ~= "0" then
451
+ local lockKey = jobKey .. ':lock'
452
+ local lockToken = rcall("GET", lockKey)
453
+ if lockToken == token then
454
+ rcall("DEL", lockKey)
455
+ rcall("SREM", stalledKey, jobId)
456
+ else
457
+ if lockToken then
458
+ -- Lock exists but token does not match
459
+ return -6
460
+ else
461
+ -- Lock is missing completely
462
+ return -2
463
+ end
464
+ end
465
+ end
466
+ return 0
467
+ end
468
+ local function moveToWaitingChildren (activeKey, waitingChildrenKey, jobId,
469
+ timestamp)
470
+ local score = tonumber(timestamp)
471
+ local numRemovedElements = rcall("LREM", activeKey, -1, jobId)
472
+ if(numRemovedElements < 1) then
473
+ return -3
474
+ end
475
+ rcall("ZADD", waitingChildrenKey, score, jobId)
476
+ return 0
477
+ end
478
+ if rcall("EXISTS", jobKey) == 1 then
479
+ if rcall("ZCARD", jobUnsuccessfulKey) ~= 0 then
480
+ -- TODO: refactor this logic in an include later
481
+ local jobAttributes = rcall("HMGET", jobKey, "parent", "deid", "opts")
482
+ removeDeduplicationKeyIfNeeded(ARGV[5], jobAttributes[2])
483
+ local failedReason = "children are failed"
484
+ rcall("ZADD", failedKey, timestamp, jobId)
485
+ rcall("HSET", jobKey, "finishedOn", timestamp)
486
+ rcall("XADD", KEYS[8], "*", "event", "failed", "jobId", jobId, "failedReason",
487
+ failedReason, "prev", "active")
488
+ local rawParentData = jobAttributes[1]
489
+ local rawOpts = jobAttributes[3]
490
+ local opts = cjson.decode(rawOpts)
491
+ if rawParentData ~= false then
492
+ if opts['fpof'] then
493
+ local parentData = cjson.decode(rawParentData)
494
+ local parentKey = parentData['queueKey'] .. ':' .. parentData['id']
495
+ local parentUnsuccesssful = parentKey .. ":unsuccessful"
496
+ rcall("ZADD", parentUnsuccesssful, timestamp, jobKey)
497
+ moveParentFromWaitingChildrenToFailed(
498
+ parentData['queueKey'],
499
+ parentData['queueKey'] .. ':' .. parentData['id'],
500
+ parentData['id'],
501
+ jobKey,
502
+ timestamp
503
+ )
504
+ elseif opts['idof'] or opts['rdof'] then
505
+ local parentData = cjson.decode(rawParentData)
506
+ local parentKey = parentData['queueKey'] .. ':' .. parentData['id']
507
+ local dependenciesSet = parentKey .. ":dependencies"
508
+ if rcall("SREM", dependenciesSet, jobKey) == 1 then
509
+ moveParentToWaitIfNeeded(parentData['queueKey'], dependenciesSet,
510
+ parentKey, parentData['id'], timestamp)
511
+ if opts['idof'] then
512
+ local failedSet = parentKey .. ":failed"
513
+ rcall("HSET", failedSet, jobKey, failedReason)
514
+ end
515
+ end
516
+ end
517
+ end
518
+ removeJobsOnFail(ARGV[5], failedKey, jobId, opts, timestamp)
519
+ return 0
520
+ else
521
+ if ARGV[2] ~= "" then
522
+ if rcall("SISMEMBER", jobDependenciesKey, ARGV[2]) ~= 0 then
523
+ local errorCode = removeLock(jobKey, stalledKey, ARGV[1], jobId)
524
+ if errorCode < 0 then
525
+ return errorCode
526
+ end
527
+ return moveToWaitingChildren(activeKey, waitingChildrenKey, jobId, timestamp)
528
+ end
529
+ return 1
530
+ else
531
+ if rcall("SCARD", jobDependenciesKey) ~= 0 then
532
+ local errorCode = removeLock(jobKey, stalledKey, ARGV[1], jobId)
533
+ if errorCode < 0 then
534
+ return errorCode
535
+ end
536
+ return moveToWaitingChildren(activeKey, waitingChildrenKey, jobId, timestamp)
537
+ end
538
+ return 1
539
+ end
540
+ end
541
+ end
542
+ return -1
543
+ `;
544
+ exports.moveToWaitingChildren = {
545
+ name: 'moveToWaitingChildren',
546
+ content,
547
+ keys: 8,
548
+ };
549
+ //# sourceMappingURL=moveToWaitingChildren-8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moveToWaitingChildren-8.js","sourceRoot":"","sources":["../../../src/scripts/moveToWaitingChildren-8.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2hBf,CAAC;AACW,QAAA,qBAAqB,GAAG;IACnC,IAAI,EAAE,uBAAuB;IAC7B,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
@@ -41,8 +41,8 @@ end
41
41
  Function to remove job keys.
42
42
  ]]
43
43
  local function removeJobKeys(jobKey)
44
- return rcall("DEL", jobKey, jobKey .. ':logs',
45
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
44
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
45
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
46
46
  end
47
47
  --[[
48
48
  Check if this job has a parent. If so we will just remove it from
@@ -74,8 +74,8 @@ end
74
74
  Function to remove job keys.
75
75
  ]]
76
76
  local function removeJobKeys(jobKey)
77
- return rcall("DEL", jobKey, jobKey .. ':logs',
78
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
77
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
78
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
79
79
  end
80
80
  local function moveParentToWait(parentPrefix, parentId, emitEvent)
81
81
  local parentTarget, isPausedOrMaxed = getTargetQueueList(parentPrefix .. "meta", parentPrefix .. "active",
@@ -127,8 +127,8 @@ end
127
127
  Function to remove job keys.
128
128
  ]]
129
129
  local function removeJobKeys(jobKey)
130
- return rcall("DEL", jobKey, jobKey .. ':logs',
131
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
130
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
131
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
132
132
  end
133
133
  --[[
134
134
  Check if this job has a parent. If so we will just remove it from
@@ -277,6 +277,14 @@ local function removeJob(prefix, jobId, parentKey, removeChildren)
277
277
  removeJob(childJobPrefix, childJobId, jobKey, removeChildren)
278
278
  end
279
279
  end
280
+ local unsuccessful = rcall("ZRANGE", jobKey .. ":unsuccessful", 0, -1)
281
+ if (#unsuccessful > 0) then
282
+ for i = 1, #unsuccessful, 1 do
283
+ local childJobId = getJobIdFromKey(unsuccessful[i])
284
+ local childJobPrefix = getJobKeyPrefix(unsuccessful[i], childJobId)
285
+ removeJob(childJobPrefix, childJobId, jobKey, removeChildren)
286
+ end
287
+ end
280
288
  end
281
289
  local prev = removeJobFromAnyState(prefix, jobId)
282
290
  removeDeduplicationKey(prefix, jobKey)
@@ -1 +1 @@
1
- {"version":3,"file":"removeJob-3.js","sourceRoot":"","sources":["../../../src/scripts/removeJob-3.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0Sf,CAAC;AACW,QAAA,SAAS,GAAG;IACvB,IAAI,EAAE,WAAW;IACjB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
1
+ {"version":3,"file":"removeJob-3.js","sourceRoot":"","sources":["../../../src/scripts/removeJob-3.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkTf,CAAC;AACW,QAAA,SAAS,GAAG;IACvB,IAAI,EAAE,WAAW;IACjB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
@@ -21,8 +21,8 @@ local rcall = redis.call
21
21
  Function to remove job keys.
22
22
  ]]
23
23
  local function removeJobKeys(jobKey)
24
- return rcall("DEL", jobKey, jobKey .. ':logs',
25
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
24
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
25
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
26
26
  end
27
27
  local jobSchedulerId = ARGV[1]
28
28
  local prefix = ARGV[2]
@@ -24,8 +24,8 @@ local millis = rcall("ZSCORE", KEYS[1], ARGV[2])
24
24
  Function to remove job keys.
25
25
  ]]
26
26
  local function removeJobKeys(jobKey)
27
- return rcall("DEL", jobKey, jobKey .. ':logs',
28
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
27
+ return rcall("DEL", jobKey, jobKey .. ':logs', jobKey .. ':dependencies',
28
+ jobKey .. ':processed', jobKey .. ':failed', jobKey .. ':unsuccessful')
29
29
  end
30
30
  -- legacy removal TODO: remove in next breaking change
31
31
  if millis then