mirror of
https://github.com/OptimalBits/bull.git
synced 2026-07-02 08:27:43 +08:00
fix(job): validate job existence when adding log (#2738)
This commit is contained in:
@@ -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
|
||||
+1
-2
@@ -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() {
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user