bullmq 5.39.2 → 5.40.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 (32) hide show
  1. package/dist/cjs/classes/job-scheduler.js +44 -63
  2. package/dist/cjs/classes/job-scheduler.js.map +1 -1
  3. package/dist/cjs/classes/scripts.js +5 -37
  4. package/dist/cjs/classes/scripts.js.map +1 -1
  5. package/dist/cjs/commands/addJobScheduler-2.lua +88 -0
  6. package/dist/cjs/scripts/addJobScheduler-2.js +234 -0
  7. package/dist/cjs/scripts/addJobScheduler-2.js.map +1 -0
  8. package/dist/cjs/scripts/index.js +1 -1
  9. package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
  10. package/dist/cjs/version.js +1 -1
  11. package/dist/esm/classes/job-scheduler.d.ts +1 -1
  12. package/dist/esm/classes/job-scheduler.js +44 -63
  13. package/dist/esm/classes/job-scheduler.js.map +1 -1
  14. package/dist/esm/classes/scripts.d.ts +3 -3
  15. package/dist/esm/classes/scripts.js +5 -37
  16. package/dist/esm/classes/scripts.js.map +1 -1
  17. package/dist/esm/commands/addJobScheduler-2.lua +88 -0
  18. package/dist/esm/scripts/addJobScheduler-2.js +231 -0
  19. package/dist/esm/scripts/addJobScheduler-2.js.map +1 -0
  20. package/dist/esm/scripts/index.d.ts +1 -1
  21. package/dist/esm/scripts/index.js +1 -1
  22. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  23. package/dist/esm/version.d.ts +1 -1
  24. package/dist/esm/version.js +1 -1
  25. package/package.json +1 -1
  26. package/dist/cjs/commands/addJobScheduler-6.lua +0 -125
  27. package/dist/cjs/scripts/addJobScheduler-6.js +0 -369
  28. package/dist/cjs/scripts/addJobScheduler-6.js.map +0 -1
  29. package/dist/esm/commands/addJobScheduler-6.lua +0 -125
  30. package/dist/esm/scripts/addJobScheduler-6.js +0 -366
  31. package/dist/esm/scripts/addJobScheduler-6.js.map +0 -1
  32. /package/dist/esm/scripts/{addJobScheduler-6.d.ts → addJobScheduler-2.d.ts} +0 -0
@@ -1,125 +0,0 @@
1
- --[[
2
- Adds a job scheduler, i.e. a job factory that creates jobs based on a given schedule (repeat options).
3
-
4
- Input:
5
- KEYS[1] 'marker',
6
- KEYS[2] 'meta'
7
- KEYS[3] 'id'
8
- KEYS[4] 'delayed'
9
- KEYS[5] events stream key
10
- KEYS[6] 'repeat' key
11
-
12
- ARGV[1] next milliseconds
13
- ARGV[2] msgpacked options
14
- [1] name
15
- [2] tz?
16
- [3] patten?
17
- [4] endDate?
18
- [5] every?
19
- ARGV[3] jobs scheduler id
20
- ARGV[4] Json stringified template data
21
- ARGV[5] msgpacked template opts
22
- ARGV[6] msgpacked delayed opts
23
- ARGV[7] timestamp
24
- ARGV[8] prefix key
25
- ARGV[9] producer key
26
-
27
- Output:
28
- next delayed job id - OK
29
- ]]
30
- local rcall = redis.call
31
- local repeatKey = KEYS[6]
32
- local delayedKey = KEYS[4]
33
- local timestamp = ARGV[7]
34
- local nextMillis = ARGV[1]
35
- local jobSchedulerId = ARGV[3]
36
- local templateOpts = cmsgpack.unpack(ARGV[5])
37
- local prefixKey = ARGV[8]
38
-
39
- -- Includes
40
- --- @include "includes/addDelayedJob"
41
- --- @include "includes/getOrSetMaxEvents"
42
- --- @include "includes/removeJob"
43
-
44
- local function storeRepeatableJob(schedulerId, schedulerKey, repeatKey, nextMillis, opts, templateData, templateOpts)
45
- rcall("ZADD", repeatKey, nextMillis, schedulerId)
46
-
47
- local optionalValues = {}
48
- if opts['tz'] then
49
- table.insert(optionalValues, "tz")
50
- table.insert(optionalValues, opts['tz'])
51
- end
52
-
53
- if opts['limit'] then
54
- table.insert(optionalValues, "limit")
55
- table.insert(optionalValues, opts['limit'])
56
- end
57
-
58
- if opts['pattern'] then
59
- table.insert(optionalValues, "pattern")
60
- table.insert(optionalValues, opts['pattern'])
61
- end
62
-
63
- if opts['endDate'] then
64
- table.insert(optionalValues, "endDate")
65
- table.insert(optionalValues, opts['endDate'])
66
- end
67
-
68
- if opts['every'] then
69
- table.insert(optionalValues, "every")
70
- table.insert(optionalValues, opts['every'])
71
- end
72
-
73
- local jsonTemplateOpts = cjson.encode(templateOpts)
74
- if jsonTemplateOpts and jsonTemplateOpts ~= '{}' then
75
- table.insert(optionalValues, "opts")
76
- table.insert(optionalValues, jsonTemplateOpts)
77
- end
78
-
79
- if templateData and templateData ~= '{}' then
80
- table.insert(optionalValues, "data")
81
- table.insert(optionalValues, templateData)
82
- end
83
-
84
- rcall("HMSET", schedulerKey, "name", opts['name'], "ic", 1,
85
- unpack(optionalValues))
86
- end
87
-
88
- local schedulerKey = repeatKey .. ":" .. jobSchedulerId
89
- local nextDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. nextMillis
90
- local nextDelayedJobKey = schedulerKey .. ":" .. nextMillis
91
-
92
- -- If we are overriding a repeatable job we must delete the delayed job for
93
- -- the next iteration.
94
- local prevMillis = rcall("ZSCORE", repeatKey, jobSchedulerId)
95
- if prevMillis ~= false then
96
- local delayedJobId = "repeat:" .. jobSchedulerId .. ":" .. prevMillis
97
-
98
- if rcall("ZSCORE", delayedKey, delayedJobId) ~= false
99
- and (rcall("EXISTS", nextDelayedJobKey) ~= 1
100
- or delayedJobId == nextDelayedJobId) then
101
- removeJob(delayedJobId, true, prefixKey, true --[[remove debounce key]])
102
- rcall("ZREM", delayedKey, delayedJobId)
103
- end
104
- end
105
-
106
- local schedulerOpts = cmsgpack.unpack(ARGV[2])
107
-
108
- storeRepeatableJob(jobSchedulerId, schedulerKey, repeatKey, nextMillis, schedulerOpts, ARGV[4], templateOpts)
109
-
110
- local eventsKey = KEYS[5]
111
- local metaKey = KEYS[2]
112
- local maxEvents = getOrSetMaxEvents(metaKey)
113
-
114
- rcall("INCR", KEYS[3])
115
-
116
- local delayedOpts = cmsgpack.unpack(ARGV[6])
117
-
118
- addDelayedJob(nextDelayedJobKey, nextDelayedJobId, delayedKey, eventsKey, schedulerOpts['name'], ARGV[4], delayedOpts,
119
- timestamp, jobSchedulerId, maxEvents, KEYS[1], nil, nil)
120
-
121
- if ARGV[9] ~= "" then
122
- rcall("HSET", ARGV[9], "nrjid", nextDelayedJobId)
123
- end
124
-
125
- return nextDelayedJobId .. "" -- convert to string
@@ -1,369 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addJobScheduler = void 0;
4
- const content = `--[[
5
- Adds a job scheduler, i.e. a job factory that creates jobs based on a given schedule (repeat options).
6
- Input:
7
- KEYS[1] 'marker',
8
- KEYS[2] 'meta'
9
- KEYS[3] 'id'
10
- KEYS[4] 'delayed'
11
- KEYS[5] events stream key
12
- KEYS[6] 'repeat' key
13
- ARGV[1] next milliseconds
14
- ARGV[2] msgpacked options
15
- [1] name
16
- [2] tz?
17
- [3] patten?
18
- [4] endDate?
19
- [5] every?
20
- ARGV[3] jobs scheduler id
21
- ARGV[4] Json stringified template data
22
- ARGV[5] msgpacked template opts
23
- ARGV[6] msgpacked delayed opts
24
- ARGV[7] timestamp
25
- ARGV[8] prefix key
26
- ARGV[9] producer key
27
- Output:
28
- next delayed job id - OK
29
- ]]
30
- local rcall = redis.call
31
- local repeatKey = KEYS[6]
32
- local delayedKey = KEYS[4]
33
- local timestamp = ARGV[7]
34
- local nextMillis = ARGV[1]
35
- local jobSchedulerId = ARGV[3]
36
- local templateOpts = cmsgpack.unpack(ARGV[5])
37
- local prefixKey = ARGV[8]
38
- -- Includes
39
- --[[
40
- Adds a delayed job to the queue by doing the following:
41
- - Creates a new job key with the job data.
42
- - adds to delayed zset.
43
- - Emits a global event 'delayed' if the job is delayed.
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
- Bake in the job id first 12 bits into the timestamp
72
- to guarantee correct execution order of delayed jobs
73
- (up to 4096 jobs per given timestamp or 4096 jobs apart per timestamp)
74
- WARNING: Jobs that are so far apart that they wrap around will cause FIFO to fail
75
- ]]
76
- local function getDelayedScore(delayedKey, timestamp, delay)
77
- local delayedTimestamp = (delay > 0 and (tonumber(timestamp) + delay)) or tonumber(timestamp)
78
- local minScore = delayedTimestamp * 0x1000
79
- local maxScore = (delayedTimestamp + 1 ) * 0x1000 - 1
80
- local result = rcall("ZREVRANGEBYSCORE", delayedKey, maxScore,
81
- minScore, "WITHSCORES","LIMIT", 0, 1)
82
- if #result then
83
- local currentMaxScore = tonumber(result[2])
84
- if currentMaxScore ~= nil then
85
- if currentMaxScore >= maxScore then
86
- return maxScore, delayedTimestamp
87
- else
88
- return currentMaxScore + 1, delayedTimestamp
89
- end
90
- end
91
- end
92
- return minScore, delayedTimestamp
93
- end
94
- --[[
95
- Function to store a job
96
- ]]
97
- local function storeJob(eventsKey, jobIdKey, jobId, name, data, opts, timestamp,
98
- parentKey, parentData, repeatJobKey)
99
- local jsonOpts = cjson.encode(opts)
100
- local delay = opts['delay'] or 0
101
- local priority = opts['priority'] or 0
102
- local debounceId = opts['de'] and opts['de']['id']
103
- local optionalValues = {}
104
- if parentKey ~= nil then
105
- table.insert(optionalValues, "parentKey")
106
- table.insert(optionalValues, parentKey)
107
- table.insert(optionalValues, "parent")
108
- table.insert(optionalValues, parentData)
109
- end
110
- if repeatJobKey ~= nil then
111
- table.insert(optionalValues, "rjk")
112
- table.insert(optionalValues, repeatJobKey)
113
- end
114
- if debounceId then
115
- table.insert(optionalValues, "deid")
116
- table.insert(optionalValues, debounceId)
117
- end
118
- rcall("HMSET", jobIdKey, "name", name, "data", data, "opts", jsonOpts,
119
- "timestamp", timestamp, "delay", delay, "priority", priority,
120
- unpack(optionalValues))
121
- rcall("XADD", eventsKey, "*", "event", "added", "jobId", jobId, "name", name)
122
- return delay, priority
123
- end
124
- local function addDelayedJob(jobIdKey, jobId, delayedKey, eventsKey, name, data, opts, timestamp, repeatJobKey,
125
- maxEvents, markerKey, parentKey, parentData)
126
- -- Store the job.
127
- local delay, priority = storeJob(eventsKey, jobIdKey, jobId, name, data,
128
- opts, timestamp, parentKey, parentData, repeatJobKey)
129
- local score, delayedTimestamp = getDelayedScore(delayedKey, timestamp, tonumber(delay))
130
- rcall("ZADD", delayedKey, score, jobId)
131
- rcall("XADD", eventsKey, "MAXLEN", "~", maxEvents, "*", "event", "delayed",
132
- "jobId", jobId, "delay", delayedTimestamp)
133
- -- mark that a delayed job is available
134
- addDelayMarkerIfNeeded(markerKey, delayedKey)
135
- end
136
- --[[
137
- Function to get max events value or set by default 10000.
138
- ]]
139
- local function getOrSetMaxEvents(metaKey)
140
- local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents")
141
- if not maxEvents then
142
- maxEvents = 10000
143
- rcall("HSET", metaKey, "opts.maxLenEvents", maxEvents)
144
- end
145
- return maxEvents
146
- end
147
- --[[
148
- Function to remove job.
149
- ]]
150
- -- Includes
151
- --[[
152
- Function to remove deduplication key.
153
- ]]
154
- local function removeDeduplicationKey(prefixKey, jobKey)
155
- local deduplicationId = rcall("HGET", jobKey, "deid")
156
- if deduplicationId then
157
- local deduplicationKey = prefixKey .. "de:" .. deduplicationId
158
- rcall("DEL", deduplicationKey)
159
- end
160
- end
161
- --[[
162
- Function to remove job keys.
163
- ]]
164
- local function removeJobKeys(jobKey)
165
- return rcall("DEL", jobKey, jobKey .. ':logs',
166
- jobKey .. ':dependencies', jobKey .. ':processed', jobKey .. ':failed')
167
- end
168
- --[[
169
- Check if this job has a parent. If so we will just remove it from
170
- the parent child list, but if it is the last child we should move the parent to "wait/paused"
171
- which requires code from "moveToFinished"
172
- ]]
173
- -- Includes
174
- --[[
175
- Function to add job in target list and add marker if needed.
176
- ]]
177
- -- Includes
178
- --[[
179
- Add marker if needed when a job is available.
180
- ]]
181
- local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
182
- if not isPausedOrMaxed then
183
- rcall("ZADD", markerKey, 0, "0")
184
- end
185
- end
186
- local function addJobInTargetList(targetKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
187
- rcall(pushCmd, targetKey, jobId)
188
- addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
189
- end
190
- --[[
191
- Functions to destructure job key.
192
- Just a bit of warning, these functions may be a bit slow and affect performance significantly.
193
- ]]
194
- local getJobIdFromKey = function (jobKey)
195
- return string.match(jobKey, ".*:(.*)")
196
- end
197
- local getJobKeyPrefix = function (jobKey, jobId)
198
- return string.sub(jobKey, 0, #jobKey - #jobId)
199
- end
200
- --[[
201
- Function to check for the meta.paused key to decide if we are paused or not
202
- (since an empty list and !EXISTS are not really the same).
203
- ]]
204
- local function getTargetQueueList(queueMetaKey, activeKey, waitKey, pausedKey)
205
- local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency")
206
- if queueAttributes[1] then
207
- return pausedKey, true
208
- else
209
- if queueAttributes[2] then
210
- local activeCount = rcall("LLEN", activeKey)
211
- if activeCount >= tonumber(queueAttributes[2]) then
212
- return waitKey, true
213
- else
214
- return waitKey, false
215
- end
216
- end
217
- end
218
- return waitKey, false
219
- end
220
- local function moveParentToWait(parentPrefix, parentId, emitEvent)
221
- local parentTarget, isPausedOrMaxed = getTargetQueueList(parentPrefix .. "meta", parentPrefix .. "active",
222
- parentPrefix .. "wait", parentPrefix .. "paused")
223
- addJobInTargetList(parentTarget, parentPrefix .. "marker", "RPUSH", isPausedOrMaxed, parentId)
224
- if emitEvent then
225
- local parentEventStream = parentPrefix .. "events"
226
- rcall("XADD", parentEventStream, "*", "event", "waiting", "jobId", parentId, "prev", "waiting-children")
227
- end
228
- end
229
- local function removeParentDependencyKey(jobKey, hard, parentKey, baseKey, debounceId)
230
- if parentKey then
231
- local parentDependenciesKey = parentKey .. ":dependencies"
232
- local result = rcall("SREM", parentDependenciesKey, jobKey)
233
- if result > 0 then
234
- local pendingDependencies = rcall("SCARD", parentDependenciesKey)
235
- if pendingDependencies == 0 then
236
- local parentId = getJobIdFromKey(parentKey)
237
- local parentPrefix = getJobKeyPrefix(parentKey, parentId)
238
- local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
239
- if numRemovedElements == 1 then
240
- if hard then -- remove parent in same queue
241
- if parentPrefix == baseKey then
242
- removeParentDependencyKey(parentKey, hard, nil, baseKey, nil)
243
- removeJobKeys(parentKey)
244
- if debounceId then
245
- rcall("DEL", parentPrefix .. "de:" .. debounceId)
246
- end
247
- else
248
- moveParentToWait(parentPrefix, parentId)
249
- end
250
- else
251
- moveParentToWait(parentPrefix, parentId, true)
252
- end
253
- end
254
- end
255
- return true
256
- end
257
- else
258
- local parentAttributes = rcall("HMGET", jobKey, "parentKey", "deid")
259
- local missedParentKey = parentAttributes[1]
260
- if( (type(missedParentKey) == "string") and missedParentKey ~= ""
261
- and (rcall("EXISTS", missedParentKey) == 1)) then
262
- local parentDependenciesKey = missedParentKey .. ":dependencies"
263
- local result = rcall("SREM", parentDependenciesKey, jobKey)
264
- if result > 0 then
265
- local pendingDependencies = rcall("SCARD", parentDependenciesKey)
266
- if pendingDependencies == 0 then
267
- local parentId = getJobIdFromKey(missedParentKey)
268
- local parentPrefix = getJobKeyPrefix(missedParentKey, parentId)
269
- local numRemovedElements = rcall("ZREM", parentPrefix .. "waiting-children", parentId)
270
- if numRemovedElements == 1 then
271
- if hard then
272
- if parentPrefix == baseKey then
273
- removeParentDependencyKey(missedParentKey, hard, nil, baseKey, nil)
274
- removeJobKeys(missedParentKey)
275
- if parentAttributes[2] then
276
- rcall("DEL", parentPrefix .. "de:" .. parentAttributes[2])
277
- end
278
- else
279
- moveParentToWait(parentPrefix, parentId)
280
- end
281
- else
282
- moveParentToWait(parentPrefix, parentId, true)
283
- end
284
- end
285
- end
286
- return true
287
- end
288
- end
289
- end
290
- return false
291
- end
292
- local function removeJob(jobId, hard, baseKey, shouldRemoveDeduplicationKey)
293
- local jobKey = baseKey .. jobId
294
- removeParentDependencyKey(jobKey, hard, nil, baseKey)
295
- if shouldRemoveDeduplicationKey then
296
- removeDeduplicationKey(baseKey, jobKey)
297
- end
298
- removeJobKeys(jobKey)
299
- end
300
- local function storeRepeatableJob(schedulerId, schedulerKey, repeatKey, nextMillis, opts, templateData, templateOpts)
301
- rcall("ZADD", repeatKey, nextMillis, schedulerId)
302
- local optionalValues = {}
303
- if opts['tz'] then
304
- table.insert(optionalValues, "tz")
305
- table.insert(optionalValues, opts['tz'])
306
- end
307
- if opts['limit'] then
308
- table.insert(optionalValues, "limit")
309
- table.insert(optionalValues, opts['limit'])
310
- end
311
- if opts['pattern'] then
312
- table.insert(optionalValues, "pattern")
313
- table.insert(optionalValues, opts['pattern'])
314
- end
315
- if opts['endDate'] then
316
- table.insert(optionalValues, "endDate")
317
- table.insert(optionalValues, opts['endDate'])
318
- end
319
- if opts['every'] then
320
- table.insert(optionalValues, "every")
321
- table.insert(optionalValues, opts['every'])
322
- end
323
- local jsonTemplateOpts = cjson.encode(templateOpts)
324
- if jsonTemplateOpts and jsonTemplateOpts ~= '{}' then
325
- table.insert(optionalValues, "opts")
326
- table.insert(optionalValues, jsonTemplateOpts)
327
- end
328
- if templateData and templateData ~= '{}' then
329
- table.insert(optionalValues, "data")
330
- table.insert(optionalValues, templateData)
331
- end
332
- rcall("HMSET", schedulerKey, "name", opts['name'], "ic", 1,
333
- unpack(optionalValues))
334
- end
335
- local schedulerKey = repeatKey .. ":" .. jobSchedulerId
336
- local nextDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. nextMillis
337
- local nextDelayedJobKey = schedulerKey .. ":" .. nextMillis
338
- -- If we are overriding a repeatable job we must delete the delayed job for
339
- -- the next iteration.
340
- local prevMillis = rcall("ZSCORE", repeatKey, jobSchedulerId)
341
- if prevMillis ~= false then
342
- local delayedJobId = "repeat:" .. jobSchedulerId .. ":" .. prevMillis
343
- if rcall("ZSCORE", delayedKey, delayedJobId) ~= false
344
- and (rcall("EXISTS", nextDelayedJobKey) ~= 1
345
- or delayedJobId == nextDelayedJobId) then
346
- removeJob(delayedJobId, true, prefixKey, true --[[remove debounce key]])
347
- rcall("ZREM", delayedKey, delayedJobId)
348
- end
349
- end
350
- local schedulerOpts = cmsgpack.unpack(ARGV[2])
351
- storeRepeatableJob(jobSchedulerId, schedulerKey, repeatKey, nextMillis, schedulerOpts, ARGV[4], templateOpts)
352
- local eventsKey = KEYS[5]
353
- local metaKey = KEYS[2]
354
- local maxEvents = getOrSetMaxEvents(metaKey)
355
- rcall("INCR", KEYS[3])
356
- local delayedOpts = cmsgpack.unpack(ARGV[6])
357
- addDelayedJob(nextDelayedJobKey, nextDelayedJobId, delayedKey, eventsKey, schedulerOpts['name'], ARGV[4], delayedOpts,
358
- timestamp, jobSchedulerId, maxEvents, KEYS[1], nil, nil)
359
- if ARGV[9] ~= "" then
360
- rcall("HSET", ARGV[9], "nrjid", nextDelayedJobId)
361
- end
362
- return nextDelayedJobId .. "" -- convert to string
363
- `;
364
- exports.addJobScheduler = {
365
- name: 'addJobScheduler',
366
- content,
367
- keys: 6,
368
- };
369
- //# sourceMappingURL=addJobScheduler-6.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"addJobScheduler-6.js","sourceRoot":"","sources":["../../../src/scripts/addJobScheduler-6.ts"],"names":[],"mappings":";;;AAAA,MAAM,OAAO,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuWf,CAAC;AACW,QAAA,eAAe,GAAG;IAC7B,IAAI,EAAE,iBAAiB;IACvB,OAAO;IACP,IAAI,EAAE,CAAC;CACR,CAAC"}
@@ -1,125 +0,0 @@
1
- --[[
2
- Adds a job scheduler, i.e. a job factory that creates jobs based on a given schedule (repeat options).
3
-
4
- Input:
5
- KEYS[1] 'marker',
6
- KEYS[2] 'meta'
7
- KEYS[3] 'id'
8
- KEYS[4] 'delayed'
9
- KEYS[5] events stream key
10
- KEYS[6] 'repeat' key
11
-
12
- ARGV[1] next milliseconds
13
- ARGV[2] msgpacked options
14
- [1] name
15
- [2] tz?
16
- [3] patten?
17
- [4] endDate?
18
- [5] every?
19
- ARGV[3] jobs scheduler id
20
- ARGV[4] Json stringified template data
21
- ARGV[5] msgpacked template opts
22
- ARGV[6] msgpacked delayed opts
23
- ARGV[7] timestamp
24
- ARGV[8] prefix key
25
- ARGV[9] producer key
26
-
27
- Output:
28
- next delayed job id - OK
29
- ]]
30
- local rcall = redis.call
31
- local repeatKey = KEYS[6]
32
- local delayedKey = KEYS[4]
33
- local timestamp = ARGV[7]
34
- local nextMillis = ARGV[1]
35
- local jobSchedulerId = ARGV[3]
36
- local templateOpts = cmsgpack.unpack(ARGV[5])
37
- local prefixKey = ARGV[8]
38
-
39
- -- Includes
40
- --- @include "includes/addDelayedJob"
41
- --- @include "includes/getOrSetMaxEvents"
42
- --- @include "includes/removeJob"
43
-
44
- local function storeRepeatableJob(schedulerId, schedulerKey, repeatKey, nextMillis, opts, templateData, templateOpts)
45
- rcall("ZADD", repeatKey, nextMillis, schedulerId)
46
-
47
- local optionalValues = {}
48
- if opts['tz'] then
49
- table.insert(optionalValues, "tz")
50
- table.insert(optionalValues, opts['tz'])
51
- end
52
-
53
- if opts['limit'] then
54
- table.insert(optionalValues, "limit")
55
- table.insert(optionalValues, opts['limit'])
56
- end
57
-
58
- if opts['pattern'] then
59
- table.insert(optionalValues, "pattern")
60
- table.insert(optionalValues, opts['pattern'])
61
- end
62
-
63
- if opts['endDate'] then
64
- table.insert(optionalValues, "endDate")
65
- table.insert(optionalValues, opts['endDate'])
66
- end
67
-
68
- if opts['every'] then
69
- table.insert(optionalValues, "every")
70
- table.insert(optionalValues, opts['every'])
71
- end
72
-
73
- local jsonTemplateOpts = cjson.encode(templateOpts)
74
- if jsonTemplateOpts and jsonTemplateOpts ~= '{}' then
75
- table.insert(optionalValues, "opts")
76
- table.insert(optionalValues, jsonTemplateOpts)
77
- end
78
-
79
- if templateData and templateData ~= '{}' then
80
- table.insert(optionalValues, "data")
81
- table.insert(optionalValues, templateData)
82
- end
83
-
84
- rcall("HMSET", schedulerKey, "name", opts['name'], "ic", 1,
85
- unpack(optionalValues))
86
- end
87
-
88
- local schedulerKey = repeatKey .. ":" .. jobSchedulerId
89
- local nextDelayedJobId = "repeat:" .. jobSchedulerId .. ":" .. nextMillis
90
- local nextDelayedJobKey = schedulerKey .. ":" .. nextMillis
91
-
92
- -- If we are overriding a repeatable job we must delete the delayed job for
93
- -- the next iteration.
94
- local prevMillis = rcall("ZSCORE", repeatKey, jobSchedulerId)
95
- if prevMillis ~= false then
96
- local delayedJobId = "repeat:" .. jobSchedulerId .. ":" .. prevMillis
97
-
98
- if rcall("ZSCORE", delayedKey, delayedJobId) ~= false
99
- and (rcall("EXISTS", nextDelayedJobKey) ~= 1
100
- or delayedJobId == nextDelayedJobId) then
101
- removeJob(delayedJobId, true, prefixKey, true --[[remove debounce key]])
102
- rcall("ZREM", delayedKey, delayedJobId)
103
- end
104
- end
105
-
106
- local schedulerOpts = cmsgpack.unpack(ARGV[2])
107
-
108
- storeRepeatableJob(jobSchedulerId, schedulerKey, repeatKey, nextMillis, schedulerOpts, ARGV[4], templateOpts)
109
-
110
- local eventsKey = KEYS[5]
111
- local metaKey = KEYS[2]
112
- local maxEvents = getOrSetMaxEvents(metaKey)
113
-
114
- rcall("INCR", KEYS[3])
115
-
116
- local delayedOpts = cmsgpack.unpack(ARGV[6])
117
-
118
- addDelayedJob(nextDelayedJobKey, nextDelayedJobId, delayedKey, eventsKey, schedulerOpts['name'], ARGV[4], delayedOpts,
119
- timestamp, jobSchedulerId, maxEvents, KEYS[1], nil, nil)
120
-
121
- if ARGV[9] ~= "" then
122
- rcall("HSET", ARGV[9], "nrjid", nextDelayedJobId)
123
- end
124
-
125
- return nextDelayedJobId .. "" -- convert to string