-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not remove delayed jobs that belongs to a job scheduler when …
…using drain and clean
- Loading branch information
Showing
7 changed files
with
146 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--[[ | ||
Drains the queue, removes all jobs that are waiting | ||
or delayed, but not active, completed or failed | ||
Input: | ||
KEYS[1] 'wait', | ||
KEYS[2] 'paused' | ||
KEYS[3] 'delayed' | ||
KEYS[4] 'prioritized' | ||
KEYS[5] 'jobschedulers' (repeat) | ||
ARGV[1] queue key prefix | ||
]] | ||
local rcall = redis.call | ||
local queueBaseKey = ARGV[1] | ||
|
||
--- @include "includes/removeListJobs" | ||
--- @include "includes/removeZSetJobs" | ||
|
||
removeListJobs(KEYS[1], true, queueBaseKey, 0) -- wait | ||
removeListJobs(KEYS[2], true, queueBaseKey, 0) -- paused | ||
|
||
if KEYS[3] ~= "" then | ||
|
||
-- We must not remove delayed jobs if they are associated to a job scheduler. | ||
local scheduledJobs = {} | ||
local jobSchedulers = rcall("ZRANGE", KEYS[5], 0, -1, "WITHSCORES") | ||
|
||
-- For every job scheduler, get the current delayed job id. | ||
for i = 1, #jobSchedulers, 2 do | ||
local jobSchedulerId = jobSchedulers[i] | ||
local jobSchedulerMillis = jobSchedulers[i + 1] | ||
|
||
local delayedJobId = "repeat:" .. jobSchedulerId .. ":" .. jobSchedulerMillis | ||
scheduledJobs[delayedJobId] = true | ||
end | ||
|
||
removeZSetJobs(KEYS[3], true, queueBaseKey, 0, scheduledJobs) -- delayed | ||
end | ||
|
||
removeZSetJobs(KEYS[4], true, queueBaseKey, 0) -- prioritized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,66 @@ | ||
--[[ | ||
Function to clean job set. | ||
Returns jobIds and deleted count number. | ||
]] | ||
]] | ||
|
||
-- Includes | ||
--- @include "batches" | ||
--- @include "getJobsInZset" | ||
--- @include "getTimestamp" | ||
--- @include "removeJob" | ||
|
||
local function cleanSet(setKey, jobKeyPrefix, rangeEnd, timestamp, limit, attributes, isFinished) | ||
local jobs = getJobsInZset(setKey, rangeEnd, limit) | ||
local deleted = {} | ||
local deletedCount = 0 | ||
local jobTS | ||
for i, job in ipairs(jobs) do | ||
if limit > 0 and deletedCount >= limit then | ||
break | ||
local function isJobSchedulerJob(jobId, jobSchedulersKey) | ||
if jobSchedulersKey then | ||
local jobSchedulerId = jobId:match("repeat:(.*):%d+") | ||
if jobSchedulerId then | ||
return rcall("ZSCORE", jobSchedulersKey, jobSchedulerId) | ||
end | ||
end | ||
return false | ||
end | ||
|
||
local function cleanSet( | ||
setKey, | ||
jobKeyPrefix, | ||
rangeEnd, | ||
timestamp, | ||
limit, | ||
attributes, | ||
isFinished, | ||
jobSchedulersKey) | ||
local jobs = getJobsInZset(setKey, rangeEnd, limit) | ||
local deleted = {} | ||
local deletedCount = 0 | ||
local jobTS | ||
for i, job in ipairs(jobs) do | ||
if limit > 0 and deletedCount >= limit then | ||
break | ||
end | ||
|
||
local jobKey = jobKeyPrefix .. job | ||
if isFinished then | ||
removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]]) | ||
deletedCount = deletedCount + 1 | ||
table.insert(deleted, job) | ||
else | ||
-- * finishedOn says when the job was completed, but it isn't set unless the job has actually completed | ||
jobTS = getTimestamp(jobKey, attributes) | ||
if (not jobTS or jobTS <= timestamp) then | ||
removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]]) | ||
deletedCount = deletedCount + 1 | ||
table.insert(deleted, job) | ||
end | ||
-- Extract a Job Scheduler Id from jobId ("repeat:job-scheduler-id:millis") | ||
-- and check if it is in the scheduled jobs | ||
if not isJobSchedulerJob(job, jobSchedulersKey) then | ||
local jobKey = jobKeyPrefix .. job | ||
if isFinished then | ||
removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]] ) | ||
deletedCount = deletedCount + 1 | ||
table.insert(deleted, job) | ||
else | ||
-- * finishedOn says when the job was completed, but it isn't set unless the job has actually completed | ||
jobTS = getTimestamp(jobKey, attributes) | ||
if (not jobTS or jobTS <= timestamp) then | ||
removeJob(job, true, jobKeyPrefix, true --[[remove debounce key]] ) | ||
deletedCount = deletedCount + 1 | ||
table.insert(deleted, job) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if(#deleted > 0) then | ||
for from, to in batches(#deleted, 7000) do | ||
rcall("ZREM", setKey, unpack(deleted, from, to)) | ||
if (#deleted > 0) then | ||
for from, to in batches(#deleted, 7000) do | ||
rcall("ZREM", setKey, unpack(deleted, from, to)) | ||
end | ||
end | ||
end | ||
|
||
return {deleted, deletedCount} | ||
return {deleted, deletedCount} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters