Skip to content

Commit

Permalink
fix(job): validate job existence when adding log (#2738)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored May 17, 2024
1 parent e9cd7bd commit 1fb1562
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
30 changes: 30 additions & 0 deletions lib/commands/addLog-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--[[
Add job log
Input:
KEYS[1] job id key
KEYS[2] job logs key
ARGV[1] id
ARGV[2] log
ARGV[3] keepLogs
Output:
-1 - Missing job.
]]
local rcall = redis.call

if rcall("EXISTS", KEYS[1]) == 1 then -- // Make sure job exists
local logCount = rcall("RPUSH", KEYS[2], ARGV[2])

if ARGV[3] ~= '' then
local keepLogs = tonumber(ARGV[3])
rcall("LTRIM", KEYS[2], -keepLogs, -1)

return math.min(keepLogs, logCount)
end

return logCount
else
return -1
end
3 changes: 1 addition & 2 deletions lib/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ Job.prototype.retry = function() {
*
*/
Job.prototype.log = function(logRow) {
const logsKey = this.toKey(this.id) + ':logs';
return this.queue.client.rpush(logsKey, logRow);
return scripts.addLog(this.queue, this.id, logRow);
};

Job.prototype.isCompleted = function() {
Expand Down
16 changes: 16 additions & 0 deletions lib/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ const scripts = {
return queue.client.pause(keys.concat([pause ? 'paused' : 'resumed']));
},

async addLog(queue, jobId, logRow, keepLogs) {
const client = await queue.client;

const keys = [queue.toKey(jobId), queue.toKey(jobId) + ':logs'];

const result = await client.addLog(
keys.concat([jobId, logRow, keepLogs ? keepLogs : ''])
);

if (result < 0) {
throw scripts.finishedErrors(result, jobId, 'addLog');
}

return result;
},

moveToActive(queue, jobId) {
const queueKeys = queue.keys;
const keys = [queueKeys.wait, queueKeys.active, queueKeys.priority];
Expand Down
10 changes: 10 additions & 0 deletions test/test_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,16 @@ describe('Job', () => {
.then(logs => expect(logs).to.be.eql({ logs: [], count: 0 }))
);
});

describe('when job was removed', () => {
it('throws an error', async () => {
const job = await Job.create(queue, { foo: 'bar' });
await job.remove();
await job.log('some log text 1').catch(err => {
expect(err.message).to.be.equal('Missing key for job 1 addLog');
});
});
});
});

describe('.moveToCompleted', () => {
Expand Down

0 comments on commit 1fb1562

Please sign in to comment.