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,116 @@
1
+ --[[
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
+
7
+ Input:
8
+ KEYS[1] 'wait',
9
+ KEYS[2] 'paused'
10
+ KEYS[3] 'meta'
11
+ KEYS[4] 'id'
12
+ KEYS[5] 'prioritized'
13
+ KEYS[6] 'completed'
14
+ KEYS[7] events stream key
15
+ KEYS[8] 'pc' priority counter
16
+
17
+ ARGV[1] msgpacked arguments array
18
+ [1] key prefix,
19
+ [2] custom id (will not generate one automatically)
20
+ [3] name
21
+ [4] timestamp
22
+ [5] parentKey?
23
+ [6] waitChildrenKey key.
24
+ [7] parent dependencies key.
25
+ [8] parent? {id, queueKey}
26
+ [9] repeat job key
27
+
28
+ ARGV[2] Json stringified job data
29
+ ARGV[3] msgpacked options
30
+
31
+ Output:
32
+ jobId - OK
33
+ -5 - Missing parent key
34
+ ]]
35
+ local waitKey = KEYS[1]
36
+ local pausedKey = KEYS[2]
37
+
38
+ local metaKey = KEYS[3]
39
+ local idKey = KEYS[4]
40
+ local priorityKey = KEYS[5]
41
+
42
+ local completedKey = KEYS[6]
43
+ local eventsKey = KEYS[7]
44
+ local priorityCounterKey = KEYS[8]
45
+
46
+ local jobId
47
+ local jobIdKey
48
+ local rcall = redis.call
49
+
50
+ local args = cmsgpack.unpack(ARGV[1])
51
+
52
+ local data = ARGV[2]
53
+ local opts = cmsgpack.unpack(ARGV[3])
54
+
55
+ local parentKey = args[5]
56
+ local repeatJobKey = args[9]
57
+ local parent = args[8]
58
+ local parentData
59
+
60
+ -- Includes
61
+ --- @include "includes/storeJob"
62
+ --- @include "includes/addJobWithPriority"
63
+ --- @include "includes/getTargetQueueList"
64
+ --- @include "includes/updateExistingJobsParent"
65
+
66
+ if parentKey ~= nil then
67
+ if rcall("EXISTS", parentKey) ~= 1 then return -5 end
68
+
69
+ parentData = cjson.encode(parent)
70
+ end
71
+
72
+ local jobCounter = rcall("INCR", idKey)
73
+
74
+ local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents") or 10000
75
+
76
+ local parentDependenciesKey = args[7]
77
+ local timestamp = args[4]
78
+ if args[2] == "" then
79
+ jobId = jobCounter
80
+ jobIdKey = args[1] .. jobId
81
+ else
82
+ jobId = args[2]
83
+ jobIdKey = args[1] .. jobId
84
+ if rcall("EXISTS", jobIdKey) == 1 then
85
+ updateExistingJobsParent(parentKey, parent, parentData,
86
+ parentDependenciesKey, completedKey, jobIdKey,
87
+ jobId, timestamp)
88
+
89
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
90
+ "duplicated", "jobId", jobId)
91
+
92
+ return jobId .. "" -- convert to string
93
+ end
94
+ end
95
+
96
+ -- Store the job.
97
+ local delay, priority = storeJob(eventsKey, jobIdKey, jobId, args[3], ARGV[2],
98
+ opts, timestamp, parentKey, parentData,
99
+ repeatJobKey)
100
+
101
+ local target, paused = getTargetQueueList(metaKey, waitKey, pausedKey)
102
+
103
+ addJobWithPriority(waitKey, priorityKey, priority, paused, jobId,
104
+ priorityCounterKey)
105
+ -- Emit waiting event
106
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting",
107
+ "jobId", jobId)
108
+
109
+ -- Check if this job is a child of another job, if so add it to the parents dependencies
110
+ -- TODO: Should not be possible to add a child job to a parent that is not in the "waiting-children" status.
111
+ -- fail in this case.
112
+ if parentDependenciesKey ~= nil then
113
+ rcall("SADD", parentDependenciesKey, jobIdKey)
114
+ end
115
+
116
+ return jobId .. "" -- convert to string
@@ -0,0 +1,115 @@
1
+ --[[
2
+ Adds a 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
+
6
+ - if delayed:
7
+ - computes timestamp.
8
+ - adds to delayed zset.
9
+ - Emits a global event 'delayed' if the job is delayed.
10
+ - if not delayed
11
+ - Adds the jobId to the wait/paused list in one of three ways:
12
+ - LIFO
13
+ - FIFO
14
+ - prioritized.
15
+ - Adds the job to the "added" list so that workers gets notified.
16
+
17
+ Input:
18
+ KEYS[1] 'wait',
19
+ KEYS[2] 'paused'
20
+ KEYS[3] 'meta'
21
+ KEYS[4] 'id'
22
+ KEYS[5] 'completed'
23
+ KEYS[6] events stream key
24
+
25
+ ARGV[1] msgpacked arguments array
26
+ [1] key prefix,
27
+ [2] custom id (will not generate one automatically)
28
+ [3] name
29
+ [4] timestamp
30
+ [5] parentKey?
31
+ [6] waitChildrenKey key.
32
+ [7] parent dependencies key.
33
+ [8] parent? {id, queueKey}
34
+ [9] repeat job key
35
+
36
+ ARGV[2] Json stringified job data
37
+ ARGV[3] msgpacked options
38
+
39
+ Output:
40
+ jobId - OK
41
+ -5 - Missing parent key
42
+ ]]
43
+ local eventsKey = KEYS[6]
44
+
45
+ local jobId
46
+ local jobIdKey
47
+ local rcall = redis.call
48
+
49
+ local args = cmsgpack.unpack(ARGV[1])
50
+
51
+ local data = ARGV[2]
52
+ local opts = cmsgpack.unpack(ARGV[3])
53
+
54
+ local parentKey = args[5]
55
+ local repeatJobKey = args[9]
56
+ local parent = args[8]
57
+ local parentData
58
+
59
+ -- Includes
60
+ --- @include "includes/storeJob"
61
+ --- @include "includes/updateExistingJobsParent"
62
+ --- @include "includes/getTargetQueueList"
63
+
64
+ if parentKey ~= nil then
65
+ if rcall("EXISTS", parentKey) ~= 1 then return -5 end
66
+
67
+ parentData = cjson.encode(parent)
68
+ end
69
+
70
+ local jobCounter = rcall("INCR", KEYS[4])
71
+
72
+ local maxEvents = rcall("HGET", KEYS[3], "opts.maxLenEvents") or 10000
73
+
74
+ local parentDependenciesKey = args[7]
75
+ local timestamp = args[4]
76
+ if args[2] == "" then
77
+ jobId = jobCounter
78
+ jobIdKey = args[1] .. jobId
79
+ else
80
+ jobId = args[2]
81
+ jobIdKey = args[1] .. jobId
82
+ if rcall("EXISTS", jobIdKey) == 1 then
83
+ updateExistingJobsParent(parentKey, parent, parentData,
84
+ parentDependenciesKey, KEYS[5], jobIdKey,
85
+ jobId, timestamp)
86
+
87
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
88
+ "duplicated", "jobId", jobId)
89
+
90
+ return jobId .. "" -- convert to string
91
+ end
92
+ end
93
+
94
+ -- Store the job.
95
+ storeJob(eventsKey, jobIdKey, jobId, args[3], ARGV[2], opts, timestamp,
96
+ parentKey, parentData, repeatJobKey)
97
+
98
+ local target, paused = getTargetQueueList(KEYS[3], KEYS[1], KEYS[2])
99
+
100
+ -- LIFO or FIFO
101
+ local pushCmd = opts['lifo'] and 'RPUSH' or 'LPUSH'
102
+ rcall(pushCmd, target, jobId)
103
+
104
+ -- Emit waiting event
105
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "waiting",
106
+ "jobId", jobId)
107
+
108
+ -- Check if this job is a child of another job, if so add it to the parents dependencies
109
+ -- TODO: Should not be possible to add a child job to a parent that is not in the "waiting-children" status.
110
+ -- fail in this case.
111
+ if parentDependenciesKey ~= nil then
112
+ rcall("SADD", parentDependenciesKey, jobIdKey)
113
+ end
114
+
115
+ return jobId .. "" -- convert to string
@@ -0,0 +1,30 @@
1
+ --[[
2
+ Function to store a job
3
+ ]]
4
+ local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
5
+ parentKey, parentData, repeatJobKey)
6
+ local jsonOpts = cjson.encode(opts)
7
+ local delay = opts['delay'] or 0
8
+ local priority = opts['priority'] or 0
9
+
10
+ local optionalValues = {}
11
+ if parentKey ~= nil then
12
+ table.insert(optionalValues, "parentKey")
13
+ table.insert(optionalValues, parentKey)
14
+ table.insert(optionalValues, "parent")
15
+ table.insert(optionalValues, parentData)
16
+ end
17
+
18
+ if repeatJobKey ~= nil then
19
+ table.insert(optionalValues, "rjk")
20
+ table.insert(optionalValues, repeatJobKey)
21
+ end
22
+
23
+ rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
24
+ "timestamp", timestamp, "delay", delay, "priority", priority,
25
+ unpack(optionalValues))
26
+
27
+ rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
28
+
29
+ return delay, priority
30
+ end
@@ -0,0 +1,25 @@
1
+ --- @include "updateParentDepsIfNeeded"
2
+
3
+ --[[
4
+ This function is used to update the parent's dependencies if the job
5
+ is already completed and about to be ignored. The parent must get its
6
+ dependencies updated to avoid the parent job being stuck forever in
7
+ the waiting-children state.
8
+ ]]
9
+ local function updateExistingJobsParent(parentKey, parent, parentData,
10
+ parentDependenciesKey, completedKey,
11
+ jobIdKey, jobId, timestamp)
12
+ if parentKey ~= nil then
13
+ if rcall("ZSCORE", completedKey, jobId) ~= false then
14
+ local returnvalue = rcall("HGET", jobIdKey, "returnvalue")
15
+ updateParentDepsIfNeeded(parentKey, parent['queueKey'],
16
+ parentDependenciesKey, parent['id'],
17
+ jobIdKey, returnvalue, timestamp)
18
+ else
19
+ if parentDependenciesKey ~= nil then
20
+ rcall("SADD", parentDependenciesKey, jobIdKey)
21
+ end
22
+ end
23
+ rcall("HMSET", jobIdKey, "parentKey", parentKey, "parent", parentData)
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ --[[
2
+ Update job progress
3
+
4
+ Input:
5
+ KEYS[1] Job id key
6
+ KEYS[2] event stream key
7
+ KEYS[3] meta key
8
+
9
+ ARGV[1] id
10
+ ARGV[2] progress
11
+
12
+ Output:
13
+ 0 - OK
14
+ -1 - Missing job.
15
+
16
+ Event:
17
+ progress(jobId, progress)
18
+ ]]
19
+ local rcall = redis.call
20
+
21
+ if rcall("EXISTS", KEYS[1]) == 1 then -- // Make sure job exists
22
+ local maxEvents = rcall("HGET", KEYS[3], "opts.maxLenEvents") or 10000
23
+
24
+ rcall("HSET", KEYS[1], "progress", ARGV[2])
25
+ rcall("XADD", KEYS[2], "MAXLEN", "~", maxEvents, "*", "event", "progress",
26
+ "jobId", ARGV[1], "data", ARGV[2]);
27
+ return 0
28
+ else
29
+ return -1
30
+ end
@@ -0,0 +1,267 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addDelayedJob = void 0;
4
+ const content = `--[[
5
+ Adds a delayed 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
+ - computes timestamp.
9
+ - adds to delayed zset.
10
+ - Emits a global event 'delayed' if the job is delayed.
11
+ Input:
12
+ KEYS[1] 'wait',
13
+ KEYS[2] 'paused'
14
+ KEYS[3] 'meta'
15
+ KEYS[4] 'id'
16
+ KEYS[5] 'delayed'
17
+ KEYS[6] 'completed'
18
+ KEYS[7] events stream key
19
+ ARGV[1] msgpacked arguments array
20
+ [1] key prefix,
21
+ [2] custom id (use custom instead of one generated automatically)
22
+ [3] name
23
+ [4] timestamp
24
+ [5] parentKey?
25
+ x [6] waitChildrenKey key.
26
+ [7] parent dependencies key.
27
+ [8] parent? {id, queueKey}
28
+ [9] repeat job key
29
+ ARGV[2] Json stringified job data
30
+ ARGV[3] msgpacked options
31
+ Output:
32
+ jobId - OK
33
+ -5 - Missing parent key
34
+ ]]
35
+ local waitKey = KEYS[1]
36
+ local pausedKey = KEYS[2]
37
+ local metaKey = KEYS[3]
38
+ local idKey = KEYS[4]
39
+ local delayedKey = KEYS[5]
40
+ local completedKey = KEYS[6]
41
+ local eventsKey = KEYS[7]
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
+ 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 check for the meta.paused key to decide if we are paused or not
117
+ (since an empty list and !EXISTS are not really the same).
118
+ ]]
119
+ local function getTargetQueueList(queueMetaKey, waitKey, pausedKey)
120
+ if rcall("HEXISTS", queueMetaKey, "paused") ~= 1 then
121
+ return waitKey, false
122
+ else
123
+ return pausedKey, true
124
+ end
125
+ end
126
+ --[[
127
+ Validate and move or add dependencies to parent.
128
+ ]]
129
+ -- Includes
130
+ --[[
131
+ Validate and move parent to active if needed.
132
+ ]]
133
+ -- Includes
134
+ --[[
135
+ Function to add job considering priority.
136
+ ]]
137
+ -- Includes
138
+ --[[
139
+ Function priority marker to wait if needed
140
+ in order to wake up our workers and to respect priority
141
+ order as much as possible
142
+ ]]
143
+ local function addPriorityMarkerIfNeeded(waitKey)
144
+ local waitLen = rcall("LLEN", waitKey)
145
+ if waitLen == 0 then
146
+ rcall("LPUSH", waitKey, "0:0")
147
+ end
148
+ end
149
+ local function addJobWithPriority(waitKey, prioritizedKey, priority, paused, jobId, priorityCounterKey)
150
+ local prioCounter = rcall("INCR", priorityCounterKey)
151
+ local score = priority * 0x100000000 + bit.band(prioCounter, 0xffffffffffff)
152
+ rcall("ZADD", prioritizedKey, score, jobId)
153
+ if not paused then
154
+ addPriorityMarkerIfNeeded(waitKey)
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
+ -- Refactor to: handleDuplicateJob.lua
229
+ jobId = args[2]
230
+ jobIdKey = args[1] .. jobId
231
+ if rcall("EXISTS", jobIdKey) == 1 then
232
+ updateExistingJobsParent(parentKey, parent, parentData,
233
+ parentDependenciesKey, completedKey, jobIdKey,
234
+ jobId, timestamp)
235
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event",
236
+ "duplicated", "jobId", jobId)
237
+ return jobId .. "" -- convert to string
238
+ end
239
+ end
240
+ -- Store the job.
241
+ local delay, priority = storeJob(eventsKey, jobIdKey, jobId, args[3], ARGV[2],
242
+ opts, timestamp, parentKey, parentData,
243
+ repeatJobKey)
244
+ -- Compute delayed timestamp and the score.
245
+ local delayedTimestamp = (delay > 0 and (timestamp + delay)) or 0
246
+ local score = delayedTimestamp * 0x1000 + bit.band(jobCounter, 0xfff)
247
+ rcall("ZADD", delayedKey, score, jobId)
248
+ rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "delayed",
249
+ "jobId", jobId, "delay", delayedTimestamp)
250
+ -- If wait list is empty, and this delayed job is the next one to be processed,
251
+ -- then we need to signal the workers by adding a dummy job (jobId 0:delay) to the wait list.
252
+ local target = getTargetQueueList(metaKey, KEYS[1], KEYS[2])
253
+ addDelayMarkerIfNeeded(target, delayedKey)
254
+ -- Check if this job is a child of another job, if so add it to the parents dependencies
255
+ -- TODO: Should not be possible to add a child job to a parent that is not in the "waiting-children" status.
256
+ -- fail in this case.
257
+ if parentDependenciesKey ~= nil then
258
+ rcall("SADD", parentDependenciesKey, jobIdKey)
259
+ end
260
+ return jobId .. "" -- convert to string
261
+ `;
262
+ exports.addDelayedJob = {
263
+ name: 'addDelayedJob',
264
+ content,
265
+ keys: 7,
266
+ };
267
+ //# sourceMappingURL=addDelayedJob-7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addDelayedJob-7.js","sourceRoot":"","sources":["../../../src/scripts/addDelayedJob-7.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiQf,CAAC;AACW,QAAA,aAAa,GAAG;IAC3B,IAAI,EAAE,eAAe;IACrB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}