Skip to content

Commit

Permalink
fix(job): check jobKey when saving stacktrace (#2755)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Jul 4, 2024
1 parent 18004a0 commit 96675f5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
24 changes: 24 additions & 0 deletions lib/commands/saveStacktrace-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--[[
Save stacktrace and failedReason.
Input:
KEYS[1] job key
ARGV[1] stacktrace
ARGV[2] failedReason
ARGV[3] attemptsMade
Output:
0 - OK
-1 - Missing key
]]
local rcall = redis.call

if rcall("EXISTS", KEYS[1]) == 1 then
rcall("HMSET", KEYS[1], "stacktrace", ARGV[1], "failedReason", ARGV[2],
"attemptsMade", ARGV[3])

return 0
else
return -1
end
21 changes: 12 additions & 9 deletions lib/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,19 +582,22 @@ Job.prototype._isInList = function(list) {
Job.prototype._saveAttempt = function(multi, err) {
this.attemptsMade++;

const params = {
attemptsMade: this.attemptsMade
};
this.stacktrace = this.stacktrace || [];

if (this.opts.stackTraceLimit) {
this.stacktrace = this.stacktrace.slice(0, this.opts.stackTraceLimit - 1);
if (err && err.stack) {
this.stacktrace.push(err.stack);
if (this.opts.stackTraceLimit) {
this.stacktrace = this.stacktrace.slice(-this.opts.stackTraceLimit);
}
}

this.stacktrace.push(err.stack);
params.stacktrace = JSON.stringify(this.stacktrace);
params.failedReason = err.message;
const args = scripts.saveStacktraceArgs(
this,
JSON.stringify(this.stacktrace),
err && err.message,
);

multi.hmset(this.queue.toKey(this.id), params);
multi.saveStacktrace(args);
};

Job.fromJSON = function(queue, json, jobId) {
Expand Down
12 changes: 12 additions & 0 deletions lib/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ const scripts = {
return queue.client.updateData(keys, [dataJson]);
},

saveStacktraceArgs(
job,
stacktrace,
failedReason
) {
const queue = job.queue;

const keys = [queue.toKey(job.id)];

return keys.concat([stacktrace, failedReason, job.attemptsMade]);
},

retryJobsArgs(queue, count) {
const keys = [
queue.toKey(''),
Expand Down

0 comments on commit 96675f5

Please sign in to comment.