bullmq 4.14.0 → 4.14.2

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 (61) hide show
  1. package/dist/cjs/classes/async-fifo-queue.js +12 -5
  2. package/dist/cjs/classes/async-fifo-queue.js.map +1 -1
  3. package/dist/cjs/classes/scripts.js +57 -4
  4. package/dist/cjs/classes/scripts.js.map +1 -1
  5. package/dist/cjs/classes/worker.js +5 -3
  6. package/dist/cjs/classes/worker.js.map +1 -1
  7. package/dist/cjs/commands/addDelayedJob-7.lua +123 -0
  8. package/dist/cjs/commands/addParentJob-4.lua +100 -0
  9. package/dist/cjs/commands/addPrioritizedJob-8.lua +116 -0
  10. package/dist/cjs/commands/addStandardJob-6.lua +115 -0
  11. package/dist/cjs/commands/includes/storeJob.lua +30 -0
  12. package/dist/cjs/commands/includes/updateExistingJobsParent.lua +25 -0
  13. package/dist/cjs/commands/updateProgress-3.lua +30 -0
  14. package/dist/cjs/scripts/addDelayedJob-7.js +267 -0
  15. package/dist/cjs/scripts/addDelayedJob-7.js.map +1 -0
  16. package/dist/cjs/scripts/addParentJob-4.js +251 -0
  17. package/dist/cjs/scripts/addParentJob-4.js.map +1 -0
  18. package/dist/cjs/scripts/addPrioritizedJob-8.js +262 -0
  19. package/dist/cjs/scripts/addPrioritizedJob-8.js.map +1 -0
  20. package/dist/cjs/scripts/{addJob-9.js → addStandardJob-6.js} +87 -91
  21. package/dist/cjs/scripts/addStandardJob-6.js.map +1 -0
  22. package/dist/cjs/scripts/index.js +4 -1
  23. package/dist/cjs/scripts/index.js.map +1 -1
  24. package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
  25. package/dist/esm/classes/async-fifo-queue.d.ts +13 -1
  26. package/dist/esm/classes/async-fifo-queue.js +12 -5
  27. package/dist/esm/classes/async-fifo-queue.js.map +1 -1
  28. package/dist/esm/classes/job.d.ts +1 -1
  29. package/dist/esm/classes/scripts.d.ts +3 -0
  30. package/dist/esm/classes/scripts.js +57 -4
  31. package/dist/esm/classes/scripts.js.map +1 -1
  32. package/dist/esm/classes/worker.js +5 -3
  33. package/dist/esm/classes/worker.js.map +1 -1
  34. package/dist/esm/commands/addDelayedJob-7.lua +123 -0
  35. package/dist/esm/commands/addParentJob-4.lua +100 -0
  36. package/dist/esm/commands/addPrioritizedJob-8.lua +116 -0
  37. package/dist/esm/commands/addStandardJob-6.lua +115 -0
  38. package/dist/esm/commands/includes/storeJob.lua +30 -0
  39. package/dist/esm/commands/includes/updateExistingJobsParent.lua +25 -0
  40. package/dist/esm/commands/updateProgress-3.lua +30 -0
  41. package/dist/esm/scripts/addDelayedJob-7.d.ts +5 -0
  42. package/dist/esm/scripts/addDelayedJob-7.js +264 -0
  43. package/dist/esm/scripts/addDelayedJob-7.js.map +1 -0
  44. package/dist/esm/scripts/{addJob-9.d.ts → addParentJob-4.d.ts} +1 -1
  45. package/dist/esm/scripts/addParentJob-4.js +248 -0
  46. package/dist/esm/scripts/addParentJob-4.js.map +1 -0
  47. package/dist/esm/scripts/addPrioritizedJob-8.d.ts +5 -0
  48. package/dist/esm/scripts/addPrioritizedJob-8.js +259 -0
  49. package/dist/esm/scripts/addPrioritizedJob-8.js.map +1 -0
  50. package/dist/esm/scripts/addStandardJob-6.d.ts +5 -0
  51. package/dist/esm/scripts/{addJob-9.js → addStandardJob-6.js} +86 -90
  52. package/dist/esm/scripts/addStandardJob-6.js.map +1 -0
  53. package/dist/esm/scripts/index.d.ts +4 -1
  54. package/dist/esm/scripts/index.js +4 -1
  55. package/dist/esm/scripts/index.js.map +1 -1
  56. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  57. package/package.json +1 -1
  58. package/dist/cjs/commands/addJob-9.lua +0 -173
  59. package/dist/cjs/scripts/addJob-9.js.map +0 -1
  60. package/dist/esm/commands/addJob-9.lua +0 -173
  61. package/dist/esm/scripts/addJob-9.js.map +0 -1
@@ -0,0 +1,251 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addParentJob = void 0;
4
+ const content = `--[[
5
+ Adds a parent job to the queue by doing the following:
6
+ - Increases the job counter if needed.
7
+ - Creates a new job key with the job data.
8
+ - adds the job to the waiting-children zset
9
+ Input:
10
+ KEYS[1] 'meta'
11
+ KEYS[2] 'id'
12
+ KEYS[3] 'completed'
13
+ KEYS[4] events stream key
14
+ ARGV[1] msgpacked arguments array
15
+ [1] key prefix,
16
+ [2] custom id (will not generate one automatically)
17
+ [3] name
18
+ [4] timestamp
19
+ [5] parentKey?
20
+ [6] waitChildrenKey key.
21
+ [7] parent dependencies key.
22
+ [8] parent? {id, queueKey}
23
+ [9] repeat job key
24
+ ARGV[2] Json stringified job data
25
+ ARGV[3] msgpacked options
26
+ Output:
27
+ jobId - OK
28
+ -5 - Missing parent key
29
+ ]]
30
+ local metaKey = KEYS[1]
31
+ local idKey = KEYS[2]
32
+ local completedKey = KEYS[3]
33
+ local eventsKey = KEYS[4]
34
+ local jobId
35
+ local jobIdKey
36
+ local rcall = redis.call
37
+ local args = cmsgpack.unpack(ARGV[1])
38
+ local data = ARGV[2]
39
+ local opts = cmsgpack.unpack(ARGV[3])
40
+ local parentKey = args[5]
41
+ local repeatJobKey = args[9]
42
+ local parent = args[8]
43
+ local parentData
44
+ -- Includes
45
+ --[[
46
+ Function to store a job
47
+ ]]
48
+ local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
49
+ parentKey, parentData, repeatJobKey)
50
+ local jsonOpts = cjson.encode(opts)
51
+ local delay = opts['delay'] or 0
52
+ local priority = opts['priority'] or 0
53
+ local optionalValues = {}
54
+ if parentKey ~= nil then
55
+ table.insert(optionalValues, "parentKey")
56
+ table.insert(optionalValues, parentKey)
57
+ table.insert(optionalValues, "parent")
58
+ table.insert(optionalValues, parentData)
59
+ end
60
+ if repeatJobKey ~= nil then
61
+ table.insert(optionalValues, "rjk")
62
+ table.insert(optionalValues, repeatJobKey)
63
+ end
64
+ rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
65
+ "timestamp", timestamp, "delay", delay, "priority", priority,
66
+ unpack(optionalValues))
67
+ rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
68
+ return delay, priority
69
+ end
70
+ --[[
71
+ Validate and move or add dependencies to parent.
72
+ ]]
73
+ -- Includes
74
+ --[[
75
+ Validate and move parent to active if needed.
76
+ ]]
77
+ -- Includes
78
+ --[[
79
+ Add delay marker if needed.
80
+ ]]
81
+ -- Includes
82
+ --[[
83
+ Function to return the next delayed job timestamp.
84
+ ]]
85
+ local function getNextDelayedTimestamp(delayedKey)
86
+ local result = rcall("ZRANGE", delayedKey, 0, 0, "WITHSCORES")
87
+ if #result then
88
+ local nextTimestamp = tonumber(result[2])
89
+ if (nextTimestamp ~= nil) then
90
+ nextTimestamp = nextTimestamp / 0x1000
91
+ end
92
+ return nextTimestamp
93
+ end
94
+ end
95
+ local function addDelayMarkerIfNeeded(targetKey, delayedKey)
96
+ local waitLen = rcall("LLEN", targetKey)
97
+ if waitLen <= 1 then
98
+ local nextTimestamp = getNextDelayedTimestamp(delayedKey)
99
+ if nextTimestamp ~= nil then
100
+ -- Check if there is already a marker with older timestamp
101
+ -- if there is, we need to replace it.
102
+ if waitLen == 1 then
103
+ local marker = rcall("LINDEX", targetKey, 0)
104
+ local oldTimestamp = tonumber(marker:sub(3))
105
+ if oldTimestamp and oldTimestamp > nextTimestamp then
106
+ rcall("LSET", targetKey, 0, "0:" .. nextTimestamp)
107
+ end
108
+ else
109
+ -- if there is no marker, then we need to add one
110
+ rcall("LPUSH", targetKey, "0:" .. nextTimestamp)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ --[[
116
+ Function to add job considering priority.
117
+ ]]
118
+ -- Includes
119
+ --[[
120
+ Function priority marker to wait if needed
121
+ in order to wake up our workers and to respect priority
122
+ order as much as possible
123
+ ]]
124
+ local function addPriorityMarkerIfNeeded(waitKey)
125
+ local waitLen = rcall("LLEN", waitKey)
126
+ if waitLen == 0 then
127
+ rcall("LPUSH", waitKey, "0:0")
128
+ end
129
+ end
130
+ local function addJobWithPriority(waitKey, prioritizedKey, priority, paused, jobId, priorityCounterKey)
131
+ local prioCounter = rcall("INCR", priorityCounterKey)
132
+ local score = priority * 0x100000000 + bit.band(prioCounter, 0xffffffffffff)
133
+ rcall("ZADD", prioritizedKey, score, jobId)
134
+ if not paused then
135
+ addPriorityMarkerIfNeeded(waitKey)
136
+ end
137
+ end
138
+ --[[
139
+ Function to check for the meta.paused key to decide if we are paused or not
140
+ (since an empty list and !EXISTS are not really the same).
141
+ ]]
142
+ local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
143
+ if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
144
+ return waitKey, false
145
+ else
146
+ return pausedKey, true
147
+ end
148
+ end
149
+ local function moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey, parentKey, parentId, timestamp)
150
+ local isParentActive = rcall("ZSCORE", parentQueueKey .. ":waiting-children", parentId)
151
+ if rcall("SCARD", parentDependenciesKey) == 0 and isParentActive then
152
+ rcall("ZREM", parentQueueKey .. ":waiting-children", parentId)
153
+ local parentWaitKey = parentQueueKey .. ":wait"
154
+ local parentTarget, paused = getTargetQueueList(parentQueueKey .. ":meta", parentWaitKey,
155
+ parentQueueKey .. ":paused")
156
+ local jobAttributes = rcall("HMGET", parentKey, "priority", "delay")
157
+ local priority = tonumber(jobAttributes[1]) or 0
158
+ local delay = tonumber(jobAttributes[2]) or 0
159
+ if delay > 0 then
160
+ local delayedTimestamp = tonumber(timestamp) + delay
161
+ local score = delayedTimestamp * 0x1000
162
+ local parentDelayedKey = parentQueueKey .. ":delayed"
163
+ rcall("ZADD", parentDelayedKey, score, parentId)
164
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "delayed", "jobId", parentId,
165
+ "delay", delayedTimestamp)
166
+ addDelayMarkerIfNeeded(parentTarget, parentDelayedKey)
167
+ else
168
+ if priority == 0 then
169
+ rcall("RPUSH", parentTarget, parentId)
170
+ else
171
+ addJobWithPriority(parentWaitKey, parentQueueKey .. ":prioritized", priority, paused,
172
+ parentId, parentQueueKey .. ":pc")
173
+ end
174
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "waiting", "jobId", parentId,
175
+ "prev", "waiting-children")
176
+ end
177
+ end
178
+ end
179
+ local function updateParentDepsIfNeeded(parentKey, parentQueueKey, parentDependenciesKey,
180
+ parentId, jobIdKey, returnvalue, timestamp )
181
+ local processedSet = parentKey .. ":processed"
182
+ rcall("HSET", processedSet, jobIdKey, returnvalue)
183
+ moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey, parentKey, parentId, timestamp)
184
+ end
185
+ --[[
186
+ This function is used to update the parent's dependencies if the job
187
+ is already completed and about to be ignored. The parent must get its
188
+ dependencies updated to avoid the parent job being stuck forever in
189
+ the waiting-children state.
190
+ ]]
191
+ local function updateExistingJobsParent(parentKey, parent, parentData,
192
+ parentDependenciesKey, completedKey,
193
+ jobIdKey, jobId, timestamp)
194
+ if parentKey ~= nil then
195
+ if rcall("ZSCORE", completedKey, jobId) ~= false then
196
+ local returnvalue = rcall("HGET", jobIdKey, "returnvalue")
197
+ updateParentDepsIfNeeded(parentKey, parent['queueKey'],
198
+ parentDependenciesKey, parent['id'],
199
+ jobIdKey, returnvalue, timestamp)
200
+ else
201
+ if parentDependenciesKey ~= nil then
202
+ rcall("SADD", parentDependenciesKey, jobIdKey)
203
+ end
204
+ end
205
+ rcall("HMSET", jobIdKey, "parentKey", parentKey, "parent", parentData)
206
+ end
207
+ end
208
+ if parentKey ~= nil then
209
+ if rcall("EXISTS", parentKey) ~= 1 then return -5 end
210
+ parentData = cjson.encode(parent)
211
+ end
212
+ local jobCounter = rcall("INCR", idKey)
213
+ local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents") or 10000
214
+ local parentDependenciesKey = args[7]
215
+ local timestamp = args[4]
216
+ if args[2] == "" then
217
+ jobId = jobCounter
218
+ jobIdKey = args[1] .. jobId
219
+ else
220
+ jobId = args[2]
221
+ jobIdKey = args[1] .. jobId
222
+ if rcall("EXISTS", jobIdKey) == 1 then
223
+ updateExistingJobsParent(parentKey, parent, parentData,
224
+ parentDependenciesKey, completedKey, jobIdKey,
225
+ jobId, timestamp)
226
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
227
+ "duplicated", "jobId", jobId)
228
+ return jobId .. "" -- convert to string
229
+ end
230
+ end
231
+ -- Store the job.
232
+ storeJob(eventsKey, jobIdKey, jobId, args[3], ARGV[2], opts, timestamp,
233
+ parentKey, parentData, repeatJobKey)
234
+ local waitChildrenKey = args[6]
235
+ rcall("ZADD", waitChildrenKey, timestamp, jobId)
236
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
237
+ "waiting-children", "jobId", jobId)
238
+ -- Check if this job is a child of another job, if so add it to the parents dependencies
239
+ -- TODO: Should not be possible to add a child job to a parent that is not in the "waiting-children" status.
240
+ -- fail in this case.
241
+ if parentDependenciesKey ~= nil then
242
+ rcall("SADD", parentDependenciesKey, jobIdKey)
243
+ end
244
+ return jobId .. "" -- convert to string
245
+ `;
246
+ exports.addParentJob = {
247
+ name: 'addParentJob',
248
+ content,
249
+ keys: 4,
250
+ };
251
+ //# sourceMappingURL=addParentJob-4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addParentJob-4.js","sourceRoot":"","sources":["../../../src/scripts/addParentJob-4.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiPf,CAAC;AACW,QAAA,YAAY,GAAG;IAC1B,IAAI,EAAE,cAAc;IACpB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
@@ -0,0 +1,262 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addPrioritizedJob = void 0;
4
+ const content = `--[[
5
+ Adds a priotitized job to the queue by doing the following:
6
+ - Increases the job counter if needed.
7
+ - Creates a new job key with the job data.
8
+ - Adds the job to the "added" list so that workers gets notified.
9
+ Input:
10
+ KEYS[1] 'wait',
11
+ KEYS[2] 'paused'
12
+ KEYS[3] 'meta'
13
+ KEYS[4] 'id'
14
+ KEYS[5] 'prioritized'
15
+ KEYS[6] 'completed'
16
+ KEYS[7] events stream key
17
+ KEYS[8] 'pc' priority counter
18
+ ARGV[1] msgpacked arguments array
19
+ [1] key prefix,
20
+ [2] custom id (will not generate one automatically)
21
+ [3] name
22
+ [4] timestamp
23
+ [5] parentKey?
24
+ [6] waitChildrenKey key.
25
+ [7] parent dependencies key.
26
+ [8] parent? {id, queueKey}
27
+ [9] repeat job key
28
+ ARGV[2] Json stringified job data
29
+ ARGV[3] msgpacked options
30
+ Output:
31
+ jobId - OK
32
+ -5 - Missing parent key
33
+ ]]
34
+ local waitKey = KEYS[1]
35
+ local pausedKey = KEYS[2]
36
+ local metaKey = KEYS[3]
37
+ local idKey = KEYS[4]
38
+ local priorityKey = KEYS[5]
39
+ local completedKey = KEYS[6]
40
+ local eventsKey = KEYS[7]
41
+ local priorityCounterKey = KEYS[8]
42
+ local jobId
43
+ local jobIdKey
44
+ local rcall = redis.call
45
+ local args = cmsgpack.unpack(ARGV[1])
46
+ local data = ARGV[2]
47
+ local opts = cmsgpack.unpack(ARGV[3])
48
+ local parentKey = args[5]
49
+ local repeatJobKey = args[9]
50
+ local parent = args[8]
51
+ local parentData
52
+ -- Includes
53
+ --[[
54
+ Function to store a job
55
+ ]]
56
+ local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
57
+ parentKey, parentData, repeatJobKey)
58
+ local jsonOpts = cjson.encode(opts)
59
+ local delay = opts['delay'] or 0
60
+ local priority = opts['priority'] or 0
61
+ local optionalValues = {}
62
+ if parentKey ~= nil then
63
+ table.insert(optionalValues, "parentKey")
64
+ table.insert(optionalValues, parentKey)
65
+ table.insert(optionalValues, "parent")
66
+ table.insert(optionalValues, parentData)
67
+ end
68
+ if repeatJobKey ~= nil then
69
+ table.insert(optionalValues, "rjk")
70
+ table.insert(optionalValues, repeatJobKey)
71
+ end
72
+ rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
73
+ "timestamp", timestamp, "delay", delay, "priority", priority,
74
+ unpack(optionalValues))
75
+ rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
76
+ return delay, priority
77
+ end
78
+ --[[
79
+ Function to add job considering priority.
80
+ ]]
81
+ -- Includes
82
+ --[[
83
+ Function priority marker to wait if needed
84
+ in order to wake up our workers and to respect priority
85
+ order as much as possible
86
+ ]]
87
+ local function addPriorityMarkerIfNeeded(waitKey)
88
+ local waitLen = rcall("LLEN", waitKey)
89
+ if waitLen == 0 then
90
+ rcall("LPUSH", waitKey, "0:0")
91
+ end
92
+ end
93
+ local function addJobWithPriority(waitKey, prioritizedKey, priority, paused, jobId, priorityCounterKey)
94
+ local prioCounter = rcall("INCR", priorityCounterKey)
95
+ local score = priority * 0x100000000 + bit.band(prioCounter, 0xffffffffffff)
96
+ rcall("ZADD", prioritizedKey, score, jobId)
97
+ if not paused then
98
+ addPriorityMarkerIfNeeded(waitKey)
99
+ end
100
+ end
101
+ --[[
102
+ Function to check for the meta.paused key to decide if we are paused or not
103
+ (since an empty list and !EXISTS are not really the same).
104
+ ]]
105
+ local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
106
+ if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
107
+ return waitKey, false
108
+ else
109
+ return pausedKey, true
110
+ end
111
+ end
112
+ --[[
113
+ Validate and move or add dependencies to parent.
114
+ ]]
115
+ -- Includes
116
+ --[[
117
+ Validate and move parent to active if needed.
118
+ ]]
119
+ -- Includes
120
+ --[[
121
+ Add delay marker if needed.
122
+ ]]
123
+ -- Includes
124
+ --[[
125
+ Function to return the next delayed job timestamp.
126
+ ]]
127
+ local function getNextDelayedTimestamp(delayedKey)
128
+ local result = rcall("ZRANGE", delayedKey, 0, 0, "WITHSCORES")
129
+ if #result then
130
+ local nextTimestamp = tonumber(result[2])
131
+ if (nextTimestamp ~= nil) then
132
+ nextTimestamp = nextTimestamp / 0x1000
133
+ end
134
+ return nextTimestamp
135
+ end
136
+ end
137
+ local function addDelayMarkerIfNeeded(targetKey, delayedKey)
138
+ local waitLen = rcall("LLEN", targetKey)
139
+ if waitLen <= 1 then
140
+ local nextTimestamp = getNextDelayedTimestamp(delayedKey)
141
+ if nextTimestamp ~= nil then
142
+ -- Check if there is already a marker with older timestamp
143
+ -- if there is, we need to replace it.
144
+ if waitLen == 1 then
145
+ local marker = rcall("LINDEX", targetKey, 0)
146
+ local oldTimestamp = tonumber(marker:sub(3))
147
+ if oldTimestamp and oldTimestamp > nextTimestamp then
148
+ rcall("LSET", targetKey, 0, "0:" .. nextTimestamp)
149
+ end
150
+ else
151
+ -- if there is no marker, then we need to add one
152
+ rcall("LPUSH", targetKey, "0:" .. nextTimestamp)
153
+ end
154
+ end
155
+ end
156
+ end
157
+ local function moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey, parentKey, parentId, timestamp)
158
+ local isParentActive = rcall("ZSCORE", parentQueueKey .. ":waiting-children", parentId)
159
+ if rcall("SCARD", parentDependenciesKey) == 0 and isParentActive then
160
+ rcall("ZREM", parentQueueKey .. ":waiting-children", parentId)
161
+ local parentWaitKey = parentQueueKey .. ":wait"
162
+ local parentTarget, paused = getTargetQueueList(parentQueueKey .. ":meta", parentWaitKey,
163
+ parentQueueKey .. ":paused")
164
+ local jobAttributes = rcall("HMGET", parentKey, "priority", "delay")
165
+ local priority = tonumber(jobAttributes[1]) or 0
166
+ local delay = tonumber(jobAttributes[2]) or 0
167
+ if delay > 0 then
168
+ local delayedTimestamp = tonumber(timestamp) + delay
169
+ local score = delayedTimestamp * 0x1000
170
+ local parentDelayedKey = parentQueueKey .. ":delayed"
171
+ rcall("ZADD", parentDelayedKey, score, parentId)
172
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "delayed", "jobId", parentId,
173
+ "delay", delayedTimestamp)
174
+ addDelayMarkerIfNeeded(parentTarget, parentDelayedKey)
175
+ else
176
+ if priority == 0 then
177
+ rcall("RPUSH", parentTarget, parentId)
178
+ else
179
+ addJobWithPriority(parentWaitKey, parentQueueKey .. ":prioritized", priority, paused,
180
+ parentId, parentQueueKey .. ":pc")
181
+ end
182
+ rcall("XADD", parentQueueKey .. ":events", "*", "event", "waiting", "jobId", parentId,
183
+ "prev", "waiting-children")
184
+ end
185
+ end
186
+ end
187
+ local function updateParentDepsIfNeeded(parentKey, parentQueueKey, parentDependenciesKey,
188
+ parentId, jobIdKey, returnvalue, timestamp )
189
+ local processedSet = parentKey .. ":processed"
190
+ rcall("HSET", processedSet, jobIdKey, returnvalue)
191
+ moveParentToWaitIfNeeded(parentQueueKey, parentDependenciesKey, parentKey, parentId, timestamp)
192
+ end
193
+ --[[
194
+ This function is used to update the parent's dependencies if the job
195
+ is already completed and about to be ignored. The parent must get its
196
+ dependencies updated to avoid the parent job being stuck forever in
197
+ the waiting-children state.
198
+ ]]
199
+ local function updateExistingJobsParent(parentKey, parent, parentData,
200
+ parentDependenciesKey, completedKey,
201
+ jobIdKey, jobId, timestamp)
202
+ if parentKey ~= nil then
203
+ if rcall("ZSCORE", completedKey, jobId) ~= false then
204
+ local returnvalue = rcall("HGET", jobIdKey, "returnvalue")
205
+ updateParentDepsIfNeeded(parentKey, parent['queueKey'],
206
+ parentDependenciesKey, parent['id'],
207
+ jobIdKey, returnvalue, timestamp)
208
+ else
209
+ if parentDependenciesKey ~= nil then
210
+ rcall("SADD", parentDependenciesKey, jobIdKey)
211
+ end
212
+ end
213
+ rcall("HMSET", jobIdKey, "parentKey", parentKey, "parent", parentData)
214
+ end
215
+ end
216
+ if parentKey ~= nil then
217
+ if rcall("EXISTS", parentKey) ~= 1 then return -5 end
218
+ parentData = cjson.encode(parent)
219
+ end
220
+ local jobCounter = rcall("INCR", idKey)
221
+ local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents") or 10000
222
+ local parentDependenciesKey = args[7]
223
+ local timestamp = args[4]
224
+ if args[2] == "" then
225
+ jobId = jobCounter
226
+ jobIdKey = args[1] .. jobId
227
+ else
228
+ jobId = args[2]
229
+ jobIdKey = args[1] .. jobId
230
+ if rcall("EXISTS", jobIdKey) == 1 then
231
+ updateExistingJobsParent(parentKey, parent, parentData,
232
+ parentDependenciesKey, completedKey, jobIdKey,
233
+ jobId, timestamp)
234
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
235
+ "duplicated", "jobId", jobId)
236
+ return jobId .. "" -- convert to string
237
+ end
238
+ end
239
+ -- Store the job.
240
+ local delay, priority = storeJob(eventsKey, jobIdKey, jobId, args[3], ARGV[2],
241
+ opts, timestamp, parentKey, parentData,
242
+ repeatJobKey)
243
+ local target, paused = getTargetQueueList(metaKey, waitKey, pausedKey)
244
+ addJobWithPriority(waitKey, priorityKey, priority, paused, jobId,
245
+ priorityCounterKey)
246
+ -- Emit waiting event
247
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting",
248
+ "jobId", jobId)
249
+ -- Check if this job is a child of another job, if so add it to the parents dependencies
250
+ -- TODO: Should not be possible to add a child job to a parent that is not in the "waiting-children" status.
251
+ -- fail in this case.
252
+ if parentDependenciesKey ~= nil then
253
+ rcall("SADD", parentDependenciesKey, jobIdKey)
254
+ end
255
+ return jobId .. "" -- convert to string
256
+ `;
257
+ exports.addPrioritizedJob = {
258
+ name: 'addPrioritizedJob',
259
+ content,
260
+ keys: 8,
261
+ };
262
+ //# sourceMappingURL=addPrioritizedJob-8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addPrioritizedJob-8.js","sourceRoot":"","sources":["../../../src/scripts/addPrioritizedJob-8.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4Pf,CAAC;AACW,QAAA,iBAAiB,GAAG;IAC/B,IAAI,EAAE,mBAAmB;IACzB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}