Skip to content

Commit

Permalink
fix: fix a couple of job tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Jul 27, 2019
1 parent c6cfe7c commit e66b97b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
4 changes: 4 additions & 0 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ export class Scripts {
const keys = ['active', 'delayed', jobId].map(function(name) {
return queue.toKey(name);
});
keys.push.apply(keys, [queue.eventStreamKey(), queue.delayStreamKey()]);

return keys.concat([JSON.stringify(timestamp), jobId]);
}

Expand All @@ -276,6 +278,8 @@ export class Scripts {
return queue.toKey(name);
});

keys.push(queue.eventStreamKey());

const pushCmd = (job.opts.lifo ? 'R' : 'L') + 'PUSH';

return keys.concat([pushCmd, jobId]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
KEYS[1] active key
KEYS[2] delayed key
KEYS[3] job key
KEYS[4] events stream
KEYS[5] delayed stream
ARGV[1] delayedTimestamp
ARGV[2] the id of the job
Expand All @@ -13,7 +15,6 @@
Output:
0 - OK
-1 - Missing job.
-2 - Job is locked.
Events:
- delayed key.
Expand All @@ -22,20 +23,15 @@ local rcall = redis.call

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

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

local score = tonumber(ARGV[1])
local delayedTimestamp = (score / 0x1000)

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

rcall("XADD", KEYS[4], "*", "event", "delayed", "jobId", jobId, "delay", delayedTimestamp);
rcall("XADD", KEYS[5], "*", "nextTimestamp", delayedTimestamp);

return 0
else
return -1
Expand Down
38 changes: 0 additions & 38 deletions src/commands/retryJob-3.lua

This file was deleted.

34 changes: 34 additions & 0 deletions src/commands/retryJob-4.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--[[
Retries a failed job by moving it back to the wait queue.
Input:
KEYS[1] 'active',
KEYS[2] 'wait'
KEYS[3] jobId
KEYS[4] events stream
ARGV[1] pushCmd
ARGV[2] jobId
ARGV[3] token
Events:
'prefix:added'
Output:
0 - OK
-1 - Missing key
]]
local rcall = redis.call

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

rcall("LREM", KEYS[1], 0, ARGV[2])
rcall(ARGV[1], KEYS[2], ARGV[2])

-- Emit waiting event
rcall("XADD", KEYS[4], "*", "event", "waiting", "jobId", ARGV[2], "prev", "failed");

return 0
else
return -1
end
14 changes: 14 additions & 0 deletions src/test/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ describe('Job', function() {
});

it('moves the job to wait for retry if attempts are given', async function() {
const queueEvents = new QueueEvents(queueName);
await queueEvents.init();

const job = await Job.create(
queue,
'test',
Expand All @@ -138,13 +141,24 @@ describe('Job', function() {
);
const isFailed = await job.isFailed();
expect(isFailed).to.be.equal(false);

const waiting = new Promise( resolve => {
queueEvents.on('waiting', resolve);
});


await job.moveToFailed(new Error('test error'), true);

await waiting;

const isFailed2 = await job.isFailed();
expect(isFailed2).to.be.equal(false);
expect(job.stacktrace).not.be.equal(null);
expect(job.stacktrace.length).to.be.equal(1);
const isWaiting = await job.isWaiting();
expect(isWaiting).to.be.equal(true);

await queueEvents.close();
});

it('marks the job as failed when attempts made equal to attempts given', async function() {
Expand Down

0 comments on commit e66b97b

Please sign in to comment.