Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(job): validate job existence when adding log #2738

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading