From 11eae6b960c83b47dadb22991b2b3e239c177508 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 21 Apr 2022 09:56:59 +0200 Subject: [PATCH] fix: unlock job when moving it to delayed (#2329) --- lib/commands/moveToDelayed-3.lua | 14 ++++++++++---- test/test_job.js | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/commands/moveToDelayed-3.lua b/lib/commands/moveToDelayed-3.lua index 94c003f4f..77825fa6b 100644 --- a/lib/commands/moveToDelayed-3.lua +++ b/lib/commands/moveToDelayed-3.lua @@ -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 @@ -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 diff --git a/test/test_job.js b/test/test_job.js index 01596b902..18c8051d5 100644 --- a/test/test_job.js +++ b/test/test_job.js @@ -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