Skip to content

Commit

Permalink
fix(retry-job): throw error when job is not in active state (#2576)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored May 23, 2024
1 parent e35dad7 commit ca207f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/commands/retryJob-11.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
36 changes: 30 additions & 6 deletions tests/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 () {
Expand Down

0 comments on commit ca207f5

Please sign in to comment.