diff --git a/src/commands/retryJob-11.lua b/src/commands/retryJob-11.lua index c563da8d98..e620728225 100644 --- a/src/commands/retryJob-11.lua +++ b/src/commands/retryJob-11.lua @@ -27,10 +27,12 @@ 0 - OK -1 - Missing key -2 - Missing lock + -3 - Job not in active set ]] local rcall = redis.call -- Includes +--- @include "includes/addJobInTargetList" --- @include "includes/addJobWithPriority" --- @include "includes/getOrSetMaxEvents" --- @include "includes/getTargetQueueList" @@ -50,14 +52,14 @@ if rcall("EXISTS", KEYS[4]) == 1 then return errorCode end - rcall("LREM", KEYS[1], 0, ARGV[4]) + local numRemovedElements = rcall("LREM", KEYS[1], -1, ARGV[4]) + if (numRemovedElements < 1) then return -3 end local priority = tonumber(rcall("HGET", KEYS[4], "priority")) or 0 -- Standard or priority add if priority == 0 then - rcall(ARGV[3], target, ARGV[4]) - -- TODO: check if we need to add marker in this case too + addJobInTargetList(target, KEYS[10], ARGV[3], paused, ARGV[4]) else addJobWithPriority(markerKey, KEYS[8], priority, ARGV[4], KEYS[9], paused) end diff --git a/tests/test_job.ts b/tests/test_job.ts index 97e4908d25..fec64641eb 100644 --- a/tests/test_job.ts +++ b/tests/test_job.ts @@ -699,13 +699,12 @@ describe('Job', function () { it('moves the job to wait for retry if attempts are given', async function () { const queueEvents = new QueueEvents(queueName, { connection, prefix }); await queueEvents.waitUntilReady(); + const worker = new Worker(queueName, null, { connection, prefix }); + + await Job.create(queue, 'test', { foo: 'bar' }, { attempts: 3 }); + const token = 'my-token'; + const job = (await worker.getNextJob(token)) as Job; - const job = await Job.create( - queue, - 'test', - { foo: 'bar' }, - { attempts: 3 }, - ); const isFailed = await job.isFailed(); expect(isFailed).to.be.equal(false); @@ -725,6 +724,31 @@ describe('Job', function () { expect(isWaiting).to.be.equal(true); await queueEvents.close(); + await worker.close(); + }); + + describe('when job is not in active state', function () { + it('throws an error', async function () { + const queueEvents = new QueueEvents(queueName, { connection, prefix }); + await queueEvents.waitUntilReady(); + + const job = await Job.create( + queue, + 'test', + { foo: 'bar' }, + { attempts: 3 }, + ); + const isFailed = await job.isFailed(); + expect(isFailed).to.be.equal(false); + + await expect( + job.moveToFailed(new Error('test error'), '0', true), + ).to.be.rejectedWith( + `Job ${job.id} is not in the active state. retryJob`, + ); + + await queueEvents.close(); + }); }); describe('when job is removed', function () {