Skip to content

Commit

Permalink
fix(job): validate job existence when adding a log (#2562)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored May 16, 2024
1 parent cce0774 commit f87e3fe
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
20 changes: 3 additions & 17 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,29 +409,15 @@ export class Job<
*
* @returns The total number of log entries for this job so far.
*/
static async addJobLog(
static addJobLog(
queue: MinimalQueue,
jobId: string,
logRow: string,
keepLogs?: number,
): Promise<number> {
const client = await queue.client;
const logsKey = queue.toKey(jobId) + ':logs';

const multi = client.multi();

multi.rpush(logsKey, logRow);

if (keepLogs) {
multi.ltrim(logsKey, -keepLogs, -1);
}

const result = (await multi.exec()) as [
[Error, number],
[Error, string] | undefined,
];
const scripts = (queue as any).scripts as Scripts;

return keepLogs ? Math.min(keepLogs, result[0][1]) : result[0][1];
return scripts.addLog(jobId, logRow, keepLogs);
}

toJSON() {
Expand Down
29 changes: 28 additions & 1 deletion src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export class Scripts {
}
}

async updateProgress<T = any, R = any, N extends string = string>(
async updateProgress(
jobId: string,
progress: number | object,
): Promise<void> {
Expand All @@ -352,6 +352,33 @@ export class Scripts {
}
}

async addLog(
jobId: string,
logRow: string,
keepLogs?: number,
): Promise<number> {
const client = await this.queue.client;

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

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

if (result < 0) {
throw this.finishedErrors({
code: result,
jobId,
command: 'addLog',
});
}

return result;
}

protected moveToFinishedArgs<T = any, R = any, N extends string = string>(
job: MinimalJob<T, R, N>,
val: any,
Expand Down
30 changes: 30 additions & 0 deletions src/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
10 changes: 10 additions & 0 deletions tests/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ describe('Job', function () {

expect(logs).to.be.eql({ logs: [firstLog, secondLog], count: 2 });
});

describe('when job is removed', () => {
it('throws error', async function () {
const job = await Job.create(queue, 'test', { foo: 'bar' });
await job.remove();
await expect(job.log('oneLog')).to.be.rejectedWith(
`Missing key for job ${job.id}. addLog`,
);
});
});
});

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

0 comments on commit f87e3fe

Please sign in to comment.