| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- const content = `--[[
- Retries a failed job by moving it back to the wait queue.
- Input:
- KEYS[1] 'active',
- KEYS[2] 'wait'
- KEYS[3] 'paused'
- KEYS[4] job key
- KEYS[5] 'meta'
- KEYS[6] events stream
- KEYS[7] delayed key
- KEYS[8] prioritized key
- KEYS[9] 'pc' priority counter
- KEYS[10] 'marker'
- KEYS[11] 'stalled'
- ARGV[1] key prefix
- ARGV[2] timestamp
- ARGV[3] pushCmd
- ARGV[4] jobId
- ARGV[5] token
- ARGV[6] optional job fields to update
- Events:
- 'waiting'
- Output:
- 0 - OK
- -1 - Missing key
- -2 - Missing lock
- -3 - Job not in active set
- ]]
- local rcall = redis.call
- -- Includes
- --[[
- Function to add job in target list and add marker if needed.
- ]]
- -- Includes
- --[[
- Add marker if needed when a job is available.
- ]]
- local function addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
- if not isPausedOrMaxed then
- rcall("ZADD", markerKey, 0, "0")
- end
- end
- local function addJobInTargetList(targetKey, markerKey, pushCmd, isPausedOrMaxed, jobId)
- rcall(pushCmd, targetKey, jobId)
- addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
- end
- --[[
- Function to add job considering priority.
- ]]
- -- Includes
- --[[
- Function to get priority score.
- ]]
- local function getPriorityScore(priority, priorityCounterKey)
- local prioCounter = rcall("INCR", priorityCounterKey)
- return priority * 0x100000000 + prioCounter % 0x100000000
- end
- local function addJobWithPriority(markerKey, prioritizedKey, priority, jobId, priorityCounterKey,
- isPausedOrMaxed)
- local score = getPriorityScore(priority, priorityCounterKey)
- rcall("ZADD", prioritizedKey, score, jobId)
- addBaseMarkerIfNeeded(markerKey, isPausedOrMaxed)
- end
- --[[
- Function to get max events value or set by default 10000.
- ]]
- local function getOrSetMaxEvents(metaKey)
- local maxEvents = rcall("HGET", metaKey, "opts.maxLenEvents")
- if not maxEvents then
- maxEvents = 10000
- rcall("HSET", metaKey, "opts.maxLenEvents", maxEvents)
- end
- return maxEvents
- end
- --[[
- Function to check for the meta.paused key to decide if we are paused or not
- (since an empty list and !EXISTS are not really the same).
- ]]
- local function getTargetQueueList(queueMetaKey, activeKey, waitKey, pausedKey)
- local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency", "max", "duration")
- if queueAttributes[1] then
- return pausedKey, true, queueAttributes[3], queueAttributes[4]
- else
- if queueAttributes[2] then
- local activeCount = rcall("LLEN", activeKey)
- if activeCount >= tonumber(queueAttributes[2]) then
- return waitKey, true, queueAttributes[3], queueAttributes[4]
- else
- return waitKey, false, queueAttributes[3], queueAttributes[4]
- end
- end
- end
- return waitKey, false, queueAttributes[3], queueAttributes[4]
- end
- --[[
- Function to check if queue is paused or maxed
- (since an empty list and !EXISTS are not really the same).
- ]]
- local function isQueuePausedOrMaxed(queueMetaKey, activeKey)
- local queueAttributes = rcall("HMGET", queueMetaKey, "paused", "concurrency")
- if queueAttributes[1] then
- return true
- else
- if queueAttributes[2] then
- local activeCount = rcall("LLEN", activeKey)
- return activeCount >= tonumber(queueAttributes[2])
- end
- end
- return false
- end
- --[[
- Updates the delay set, by moving delayed jobs that should
- be processed now to "wait".
- Events:
- 'waiting'
- ]]
- -- Includes
- -- Try to get as much as 1000 jobs at once
- local function promoteDelayedJobs(delayedKey, markerKey, targetKey, prioritizedKey,
- eventStreamKey, prefix, timestamp, priorityCounterKey, isPaused)
- local jobs = rcall("ZRANGEBYSCORE", delayedKey, 0, (timestamp + 1) * 0x1000 - 1, "LIMIT", 0, 1000)
- if (#jobs > 0) then
- rcall("ZREM", delayedKey, unpack(jobs))
- for _, jobId in ipairs(jobs) do
- local jobKey = prefix .. jobId
- local priority =
- tonumber(rcall("HGET", jobKey, "priority")) or 0
- if priority == 0 then
- -- LIFO or FIFO
- rcall("LPUSH", targetKey, jobId)
- else
- local score = getPriorityScore(priority, priorityCounterKey)
- rcall("ZADD", prioritizedKey, score, jobId)
- end
- -- Emit waiting event
- rcall("XADD", eventStreamKey, "*", "event", "waiting", "jobId",
- jobId, "prev", "delayed")
- rcall("HSET", jobKey, "delay", 0)
- end
- addBaseMarkerIfNeeded(markerKey, isPaused)
- end
- end
- local function removeLock(jobKey, stalledKey, token, jobId)
- if token ~= "0" then
- local lockKey = jobKey .. ':lock'
- local lockToken = rcall("GET", lockKey)
- if lockToken == token then
- rcall("DEL", lockKey)
- rcall("SREM", stalledKey, jobId)
- else
- if lockToken then
- -- Lock exists but token does not match
- return -6
- else
- -- Lock is missing completely
- return -2
- end
- end
- end
- return 0
- end
- --[[
- Function to update a bunch of fields in a job.
- ]]
- local function updateJobFields(jobKey, msgpackedFields)
- if msgpackedFields and #msgpackedFields > 0 then
- local fieldsToUpdate = cmsgpack.unpack(msgpackedFields)
- if fieldsToUpdate then
- rcall("HMSET", jobKey, unpack(fieldsToUpdate))
- end
- end
- end
- local target, isPausedOrMaxed = getTargetQueueList(KEYS[5], KEYS[1], KEYS[2], KEYS[3])
- local markerKey = KEYS[10]
- -- Check if there are delayed jobs that we can move to wait.
- -- test example: when there are delayed jobs between retries
- promoteDelayedJobs(KEYS[7], markerKey, target, KEYS[8], KEYS[6], ARGV[1], ARGV[2], KEYS[9], isPausedOrMaxed)
- local jobKey = KEYS[4]
- if rcall("EXISTS", jobKey) == 1 then
- local errorCode = removeLock(jobKey, KEYS[11], ARGV[5], ARGV[4])
- if errorCode < 0 then
- return errorCode
- end
- updateJobFields(jobKey, ARGV[6])
- local numRemovedElements = rcall("LREM", KEYS[1], -1, ARGV[4])
- if (numRemovedElements < 1) then return -3 end
- local priority = tonumber(rcall("HGET", jobKey, "priority")) or 0
- --need to re-evaluate after removing job from active
- isPausedOrMaxed = isQueuePausedOrMaxed(KEYS[5], KEYS[1])
- -- Standard or priority add
- if priority == 0 then
- addJobInTargetList(target, markerKey, ARGV[3], isPausedOrMaxed, ARGV[4])
- else
- addJobWithPriority(markerKey, KEYS[8], priority, ARGV[4], KEYS[9], isPausedOrMaxed)
- end
- rcall("HINCRBY", jobKey, "atm", 1)
- local maxEvents = getOrSetMaxEvents(KEYS[5])
- -- Emit waiting event
- rcall("XADD", KEYS[6], "MAXLEN", "~", maxEvents, "*", "event", "waiting",
- "jobId", ARGV[4], "prev", "active")
- return 0
- else
- return -1
- end
- `;
- export const retryJob = {
- name: 'retryJob',
- content,
- keys: 11,
- };
- //# sourceMappingURL=retryJob-11.js.map
|