bullmq 4.14.1 → 4.14.3

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