Skip to content

Commit

Permalink
fix(job): consider passing stackTraceLimit as 0 (#2692) ref #2487
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Aug 7, 2024
1 parent 5e6154f commit 509a36b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,9 @@ export class Job<

if (err?.stack) {
this.stacktrace.push(err.stack);
if (this.opts.stackTraceLimit) {
if (this.opts.stackTraceLimit === 0) {
this.stacktrace = [];
} else if (this.opts.stackTraceLimit) {
this.stacktrace = this.stacktrace.slice(-this.opts.stackTraceLimit);
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,36 @@ describe('Job', function () {
await worker.close();
});

describe('when stackTraceLimit is provided as 0', function () {
it('keep stacktrace empty', async function () {
const worker = new Worker(queueName, null, { connection, prefix });
const token = 'my-token';
const stackTraceLimit = 0;
await Job.create(
queue,
'test',
{ foo: 'bar' },
{ stackTraceLimit: stackTraceLimit, attempts: 2 },
);
const job = (await worker.getNextJob(token)) as Job;
const isFailed = await job.isFailed();
expect(isFailed).to.be.equal(false);
// first time failed.
await job.moveToFailed(new Error('failed once'), '0', true);
const isFailed1 = await job.isFailed();
expect(isFailed1).to.be.false;
expect(job.stacktrace.length).to.be.equal(stackTraceLimit);
// second time failed.
const again = (await worker.getNextJob(token)) as Job;
await again.moveToFailed(new Error('failed twice'), '0', true);
const isFailed2 = await again.isFailed();
expect(isFailed2).to.be.true;
expect(again.name).to.be.equal(job.name);
expect(again.stacktrace.length).to.be.equal(stackTraceLimit);
await worker.close();
});
});

it('saves error stacktrace', async function () {
const worker = new Worker(queueName, null, { connection, prefix });
const token = 'my-token';
Expand Down

0 comments on commit 509a36b

Please sign in to comment.