bullmq 3.15.0 → 3.15.1

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.
@@ -1,10 +1,14 @@
1
- local function getRateLimitTTL(opts, limiterKey)
2
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
1
+ local function getRateLimitTTL(maxJobs, rateLimiterKey)
3
2
  if maxJobs then
4
- local jobCounter = tonumber(rcall("GET", limiterKey))
5
- if jobCounter ~= nil and jobCounter >= maxJobs then
6
- local pttl = rcall("PTTL", limiterKey)
7
- if pttl > 0 then
3
+ local pttl = rcall("PTTL", rateLimiterKey)
4
+
5
+ if pttl <= 0 then
6
+ rcall("DEL", rateLimiterKey)
7
+ end
8
+
9
+ local jobCounter = tonumber(rcall("GET", rateLimiterKey) or 0)
10
+ if jobCounter >= maxJobs then
11
+ if pttl > 0 then
8
12
  return pttl
9
13
  end
10
14
  end
@@ -17,19 +17,11 @@
17
17
  opts - limiter
18
18
  ]]
19
19
 
20
- local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts)
20
+ local function moveJobFromWaitToActive(keys, keyPrefix, targetKey, jobId, processedOn,
21
+ maxJobs, expireTime, opts)
21
22
  -- Check if we need to perform rate limiting.
22
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
23
- local expireTime
24
-
25
- if(maxJobs) then
23
+ if maxJobs then
26
24
  local rateLimiterKey = keys[6];
27
-
28
- expireTime = tonumber(rcall("PTTL", rateLimiterKey))
29
- if expireTime <= 0 then
30
- rcall("DEL", rateLimiterKey)
31
- end
32
-
33
25
  local jobCounter = tonumber(rcall("INCR", rateLimiterKey))
34
26
 
35
27
  if jobCounter == 1 then
@@ -39,13 +31,13 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
39
31
  end
40
32
 
41
33
  -- check if we passed rate limit, we need to remove the job and return expireTime
42
- if jobCounter > maxJobs then
34
+ if expireTime > 0 then
43
35
  -- remove from active queue and add back to the wait list
44
36
  rcall("LREM", keys[2], 1, jobId)
45
- rcall("RPUSH", keys[1], jobId)
37
+ rcall("RPUSH", targetKey, jobId)
46
38
 
47
39
  -- Return when we can process more jobs
48
- return {0, 0, expireTime}
40
+ return {0, 0, expireTime, 0}
49
41
  end
50
42
  end
51
43
 
@@ -62,5 +54,5 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
62
54
  rcall("HSET", jobKey, "processedOn", processedOn)
63
55
  rcall("HINCRBY", jobKey, "attemptsMade", 1)
64
56
 
65
- return {rcall("HGETALL", jobKey), jobId, expireTime} -- get job data
57
+ return {rcall("HGETALL", jobKey), jobId, 0, 0} -- get job data
66
58
  end
@@ -46,17 +46,17 @@ local target = getTargetQueueList(KEYS[9], KEYS[1], KEYS[8])
46
46
  -- Check if there are delayed jobs that we can move to wait.
47
47
  promoteDelayedJobs(KEYS[7], target, KEYS[3], KEYS[4], ARGV[1], ARGV[2])
48
48
 
49
- local opts
49
+ local opts = cmsgpack.unpack(ARGV[4])
50
+ local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
51
+ local expireTime = getRateLimitTTL(maxJobs, KEYS[6])
50
52
  if (ARGV[3] ~= "") then
51
53
  jobId = ARGV[3]
52
54
  -- clean stalled key
53
55
  rcall("SREM", KEYS[5], jobId)
54
56
  else
55
57
  -- Check if we are rate limited first.
56
- opts = cmsgpack.unpack(ARGV[4])
57
- local pttl = getRateLimitTTL(opts, KEYS[6])
58
- if pttl > 0 then
59
- return { 0, 0, pttl }
58
+ if expireTime > 0 then
59
+ return { 0, 0, expireTime, 0 }
60
60
  end
61
61
 
62
62
  -- no job ID, try non-blocking move from wait to active
@@ -67,6 +67,11 @@ end
67
67
  if jobId then
68
68
  if string.sub(jobId, 1, 2) == "0:" then
69
69
  rcall("LREM", KEYS[2], 1, jobId)
70
+
71
+ if expireTime > 0 then
72
+ return { 0, 0, expireTime, 0 }
73
+ end
74
+
70
75
  -- Move again since we just got the marker job.
71
76
  jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])
72
77
 
@@ -79,9 +84,8 @@ if jobId then
79
84
  end
80
85
 
81
86
  if jobId then
82
- opts = opts or cmsgpack.unpack(ARGV[4])
83
87
  -- this script is not really moving, it is preparing the job for processing
84
- return moveJobFromWaitToActive(KEYS, ARGV[1], jobId, ARGV[2], opts)
88
+ return moveJobFromWaitToActive(KEYS, ARGV[1], target, jobId, ARGV[2], maxJobs, expireTime, opts)
85
89
  end
86
90
  end
87
91
 
@@ -90,3 +94,5 @@ local nextTimestamp = getNextDelayedTimestamp(KEYS[7])
90
94
  if (nextTimestamp ~= nil) then
91
95
  return { 0, 0, 0, nextTimestamp}
92
96
  end
97
+
98
+ return { 0, 0, 0, 0}
@@ -192,9 +192,11 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
192
192
  promoteDelayedJobs(KEYS[7], target, KEYS[3],
193
193
  KEYS[4], ARGV[8], timestamp)
194
194
 
195
+ local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
195
196
  -- Check if we are rate limited first.
196
- local pttl = getRateLimitTTL(opts, KEYS[6])
197
- if pttl > 0 then return {0, 0, pttl} end
197
+ local expireTime = getRateLimitTTL(maxJobs, KEYS[6])
198
+
199
+ if expireTime > 0 then return {0, 0, expireTime, 0} end
198
200
 
199
201
  jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])
200
202
 
@@ -203,15 +205,14 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
203
205
  if string.sub(jobId, 1, 2) == "0:" then
204
206
  rcall("LREM", KEYS[2], 1, jobId)
205
207
  else
206
- opts = opts or cmsgpack.unpack(ARGV[4])
207
208
  -- this script is not really moving, it is preparing the job for processing
208
- return moveJobFromWaitToActive(KEYS, ARGV[8], jobId, timestamp, opts)
209
+ return moveJobFromWaitToActive(KEYS, ARGV[8], target, jobId, timestamp, maxJobs, expireTime, opts)
209
210
  end
210
211
  end
211
212
 
212
213
  -- Return the timestamp for the next delayed job if any.
213
214
  local nextTimestamp = getNextDelayedTimestamp(KEYS[7])
214
- if (nextTimestamp ~= nil) then
215
+ if nextTimestamp ~= nil then
215
216
  -- The result is guaranteed to be positive, since the
216
217
  -- ZRANGEBYSCORE command would have return a job otherwise.
217
218
  return {0, 0, 0, nextTimestamp}
@@ -43,16 +43,11 @@ local rcall = redis.call
43
43
  opts - lockDuration
44
44
  opts - limiter
45
45
  ]]
46
- local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts)
46
+ local function moveJobFromWaitToActive(keys, keyPrefix, targetKey, jobId, processedOn,
47
+ maxJobs, expireTime, opts)
47
48
  -- Check if we need to perform rate limiting.
48
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
49
- local expireTime
50
- if(maxJobs) then
49
+ if maxJobs then
51
50
  local rateLimiterKey = keys[6];
52
- expireTime = tonumber(rcall("PTTL", rateLimiterKey))
53
- if expireTime <= 0 then
54
- rcall("DEL", rateLimiterKey)
55
- end
56
51
  local jobCounter = tonumber(rcall("INCR", rateLimiterKey))
57
52
  if jobCounter == 1 then
58
53
  local limiterDuration = opts['limiter'] and opts['limiter']['duration']
@@ -60,12 +55,12 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
60
55
  rcall("PEXPIRE", rateLimiterKey, integerDuration)
61
56
  end
62
57
  -- check if we passed rate limit, we need to remove the job and return expireTime
63
- if jobCounter > maxJobs then
58
+ if expireTime > 0 then
64
59
  -- remove from active queue and add back to the wait list
65
60
  rcall("LREM", keys[2], 1, jobId)
66
- rcall("RPUSH", keys[1], jobId)
61
+ rcall("RPUSH", targetKey, jobId)
67
62
  -- Return when we can process more jobs
68
- return {0, 0, expireTime}
63
+ return {0, 0, expireTime, 0}
69
64
  end
70
65
  end
71
66
  local jobKey = keyPrefix .. jobId
@@ -78,7 +73,7 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
78
73
  rcall("XADD", keys[4], "*", "event", "active", "jobId", jobId, "prev", "waiting")
79
74
  rcall("HSET", jobKey, "processedOn", processedOn)
80
75
  rcall("HINCRBY", jobKey, "attemptsMade", 1)
81
- return {rcall("HGETALL", jobKey), jobId, expireTime} -- get job data
76
+ return {rcall("HGETALL", jobKey), jobId, 0, 0} -- get job data
82
77
  end
83
78
  --[[
84
79
  Function to return the next delayed job timestamp.
@@ -93,13 +88,15 @@ local function getNextDelayedTimestamp(delayedKey)
93
88
  return nextTimestamp
94
89
  end
95
90
  end
96
- local function getRateLimitTTL(opts, limiterKey)
97
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
91
+ local function getRateLimitTTL(maxJobs, rateLimiterKey)
98
92
  if maxJobs then
99
- local jobCounter = tonumber(rcall("GET", limiterKey))
100
- if jobCounter ~= nil and jobCounter >= maxJobs then
101
- local pttl = rcall("PTTL", limiterKey)
102
- if pttl > 0 then
93
+ local pttl = rcall("PTTL", rateLimiterKey)
94
+ if pttl <= 0 then
95
+ rcall("DEL", rateLimiterKey)
96
+ end
97
+ local jobCounter = tonumber(rcall("GET", rateLimiterKey) or 0)
98
+ if jobCounter >= maxJobs then
99
+ if pttl > 0 then
103
100
  return pttl
104
101
  end
105
102
  end
@@ -163,17 +160,17 @@ end
163
160
  local target = getTargetQueueList(KEYS[9], KEYS[1], KEYS[8])
164
161
  -- Check if there are delayed jobs that we can move to wait.
165
162
  promoteDelayedJobs(KEYS[7], target, KEYS[3], KEYS[4], ARGV[1], ARGV[2])
166
- local opts
163
+ local opts = cmsgpack.unpack(ARGV[4])
164
+ local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
165
+ local expireTime = getRateLimitTTL(maxJobs, KEYS[6])
167
166
  if (ARGV[3] ~= "") then
168
167
  jobId = ARGV[3]
169
168
  -- clean stalled key
170
169
  rcall("SREM", KEYS[5], jobId)
171
170
  else
172
171
  -- Check if we are rate limited first.
173
- opts = cmsgpack.unpack(ARGV[4])
174
- local pttl = getRateLimitTTL(opts, KEYS[6])
175
- if pttl > 0 then
176
- return { 0, 0, pttl }
172
+ if expireTime > 0 then
173
+ return { 0, 0, expireTime, 0 }
177
174
  end
178
175
  -- no job ID, try non-blocking move from wait to active
179
176
  jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])
@@ -182,6 +179,9 @@ end
182
179
  if jobId then
183
180
  if string.sub(jobId, 1, 2) == "0:" then
184
181
  rcall("LREM", KEYS[2], 1, jobId)
182
+ if expireTime > 0 then
183
+ return { 0, 0, expireTime, 0 }
184
+ end
185
185
  -- Move again since we just got the marker job.
186
186
  jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])
187
187
  -- Since it is possible that between a call to BRPOPLPUSH and moveToActive
@@ -192,9 +192,8 @@ if jobId then
192
192
  end
193
193
  end
194
194
  if jobId then
195
- opts = opts or cmsgpack.unpack(ARGV[4])
196
195
  -- this script is not really moving, it is preparing the job for processing
197
- return moveJobFromWaitToActive(KEYS, ARGV[1], jobId, ARGV[2], opts)
196
+ return moveJobFromWaitToActive(KEYS, ARGV[1], target, jobId, ARGV[2], maxJobs, expireTime, opts)
198
197
  end
199
198
  end
200
199
  -- Return the timestamp for the next delayed job if any.
@@ -202,6 +201,7 @@ local nextTimestamp = getNextDelayedTimestamp(KEYS[7])
202
201
  if (nextTimestamp ~= nil) then
203
202
  return { 0, 0, 0, nextTimestamp}
204
203
  end
204
+ return { 0, 0, 0, 0}
205
205
  `;
206
206
  export const moveToActive = {
207
207
  name: 'moveToActive',
@@ -127,16 +127,11 @@ end
127
127
  opts - lockDuration
128
128
  opts - limiter
129
129
  ]]
130
- local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts)
130
+ local function moveJobFromWaitToActive(keys, keyPrefix, targetKey, jobId, processedOn,
131
+ maxJobs, expireTime, opts)
131
132
  -- Check if we need to perform rate limiting.
132
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
133
- local expireTime
134
- if(maxJobs) then
133
+ if maxJobs then
135
134
  local rateLimiterKey = keys[6];
136
- expireTime = tonumber(rcall("PTTL", rateLimiterKey))
137
- if expireTime <= 0 then
138
- rcall("DEL", rateLimiterKey)
139
- end
140
135
  local jobCounter = tonumber(rcall("INCR", rateLimiterKey))
141
136
  if jobCounter == 1 then
142
137
  local limiterDuration = opts['limiter'] and opts['limiter']['duration']
@@ -144,12 +139,12 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
144
139
  rcall("PEXPIRE", rateLimiterKey, integerDuration)
145
140
  end
146
141
  -- check if we passed rate limit, we need to remove the job and return expireTime
147
- if jobCounter > maxJobs then
142
+ if expireTime > 0 then
148
143
  -- remove from active queue and add back to the wait list
149
144
  rcall("LREM", keys[2], 1, jobId)
150
- rcall("RPUSH", keys[1], jobId)
145
+ rcall("RPUSH", targetKey, jobId)
151
146
  -- Return when we can process more jobs
152
- return {0, 0, expireTime}
147
+ return {0, 0, expireTime, 0}
153
148
  end
154
149
  end
155
150
  local jobKey = keyPrefix .. jobId
@@ -162,7 +157,7 @@ local function moveJobFromWaitToActive(keys, keyPrefix, jobId, processedOn, opts
162
157
  rcall("XADD", keys[4], "*", "event", "active", "jobId", jobId, "prev", "waiting")
163
158
  rcall("HSET", jobKey, "processedOn", processedOn)
164
159
  rcall("HINCRBY", jobKey, "attemptsMade", 1)
165
- return {rcall("HGETALL", jobKey), jobId, expireTime} -- get job data
160
+ return {rcall("HGETALL", jobKey), jobId, 0, 0} -- get job data
166
161
  end
167
162
  --[[
168
163
  Function to recursively move from waitingChildren to failed.
@@ -408,13 +403,15 @@ local function updateParentDepsIfNeeded(parentKey, parentQueueKey, parentDepende
408
403
  rcall("XADD", parentQueueKey .. ":events", "*", "event", "waiting", "jobId", parentId, "prev", "waiting-children")
409
404
  end
410
405
  end
411
- local function getRateLimitTTL(opts, limiterKey)
412
- local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
406
+ local function getRateLimitTTL(maxJobs, rateLimiterKey)
413
407
  if maxJobs then
414
- local jobCounter = tonumber(rcall("GET", limiterKey))
415
- if jobCounter ~= nil and jobCounter >= maxJobs then
416
- local pttl = rcall("PTTL", limiterKey)
417
- if pttl > 0 then
408
+ local pttl = rcall("PTTL", rateLimiterKey)
409
+ if pttl <= 0 then
410
+ rcall("DEL", rateLimiterKey)
411
+ end
412
+ local jobCounter = tonumber(rcall("GET", rateLimiterKey) or 0)
413
+ if jobCounter >= maxJobs then
414
+ if pttl > 0 then
418
415
  return pttl
419
416
  end
420
417
  end
@@ -527,23 +524,23 @@ if rcall("EXISTS", jobIdKey) == 1 then -- // Make sure job exists
527
524
  -- Check if there are delayed jobs that can be promoted
528
525
  promoteDelayedJobs(KEYS[7], target, KEYS[3],
529
526
  KEYS[4], ARGV[8], timestamp)
527
+ local maxJobs = tonumber(opts['limiter'] and opts['limiter']['max'])
530
528
  -- Check if we are rate limited first.
531
- local pttl = getRateLimitTTL(opts, KEYS[6])
532
- if pttl > 0 then return {0, 0, pttl} end
529
+ local expireTime = getRateLimitTTL(maxJobs, KEYS[6])
530
+ if expireTime > 0 then return {0, 0, expireTime, 0} end
533
531
  jobId = rcall("RPOPLPUSH", KEYS[1], KEYS[2])
534
532
  -- If jobId is special ID 0:delay, then there is no job to process
535
533
  if jobId then
536
534
  if string.sub(jobId, 1, 2) == "0:" then
537
535
  rcall("LREM", KEYS[2], 1, jobId)
538
536
  else
539
- opts = opts or cmsgpack.unpack(ARGV[4])
540
537
  -- this script is not really moving, it is preparing the job for processing
541
- return moveJobFromWaitToActive(KEYS, ARGV[8], jobId, timestamp, opts)
538
+ return moveJobFromWaitToActive(KEYS, ARGV[8], target, jobId, timestamp, maxJobs, expireTime, opts)
542
539
  end
543
540
  end
544
541
  -- Return the timestamp for the next delayed job if any.
545
542
  local nextTimestamp = getNextDelayedTimestamp(KEYS[7])
546
- if (nextTimestamp ~= nil) then
543
+ if nextTimestamp ~= nil then
547
544
  -- The result is guaranteed to be positive, since the
548
545
  -- ZRANGEBYSCORE command would have return a job otherwise.
549
546
  return {0, 0, 0, nextTimestamp}
@@ -1 +1 @@
1
- {"version":3,"file":"moveToFinished-12.js","sourceRoot":"","sources":["../../../src/scripts/moveToFinished-12.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkjBf,CAAC;AACF,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,gBAAgB;IACtB,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}
1
+ {"version":3,"file":"moveToFinished-12.js","sourceRoot":"","sources":["../../../src/scripts/moveToFinished-12.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+iBf,CAAC;AACF,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,IAAI,EAAE,gBAAgB;IACtB,OAAO;IACP,IAAI,EAAE,EAAE;CACT,CAAC"}