Skip to content

Commit

Permalink
fix: unlock job when moving it to delayed (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou authored Apr 21, 2022
1 parent 61a87a8 commit 11eae6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/commands/moveToDelayed-3.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--[[
Moves job from active to delayed set.
Input:
Input:
KEYS[1] active key
KEYS[2] delayed key
KEYS[3] job key
Expand All @@ -21,21 +21,27 @@
local rcall = redis.call

if rcall("EXISTS", KEYS[3]) == 1 then
local lockKey
local lock

-- Check for job lock
if ARGV[3] ~= "0" then
local lockKey = KEYS[3] .. ':lock'
local lock = rcall("GET", lockKey)
lockKey = KEYS[3] .. ':lock'
lock = rcall("GET", lockKey)
if lock ~= ARGV[3] then
return -2
end
end

local score = tonumber(ARGV[1])
rcall("ZADD", KEYS[2], score, ARGV[2])
rcall("PUBLISH", KEYS[2], (score / 0x1000))
rcall("LREM", KEYS[1], 0, ARGV[2])

if lock then
rcall("DEL", lockKey)
end

return 0
else
return -1
Expand Down
22 changes: 22 additions & 0 deletions test/test_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,28 @@ describe('Job', () => {
});
});

it('unlocks the job when moving it to delayed', () => {
queue.process(() => {
throw new Error('Oh dear');
});
return Job.create(
queue,
{ foo: 'bar' },
{ attempts: 3, backoff: 100 }
).then(job => {
return new Promise(resolve => {
queue.once('failed', resolve);
})
.then(() => {
const client = new redis();
return client.get(job.lockKey());
})
.then(lockValue => {
expect(lockValue).to.be(null);
});
});
});

it('marks the job as failed when attempts made equal to attempts given', () => {
return Job.create(queue, { foo: 'bar' }, { attempts: 1 }).then(job => {
return job
Expand Down

0 comments on commit 11eae6b

Please sign in to comment.