Skip to content

Commit

Permalink
perf(worker): fetch next job on failure (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Jul 19, 2024
1 parent b5500c4 commit f917b80
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
tryCatch,
} from '../utils';
import { Backoffs } from './backoffs';
import { Scripts } from './scripts';
import { Scripts, raw2NextJobData } from './scripts';
import { UnrecoverableError } from './errors/unrecoverable-error';
import type { QueueEvents } from './queue-events';

Expand Down Expand Up @@ -634,7 +634,7 @@ export class Job<
err: E,
token: string,
fetchNext = false,
): Promise<void> {
): Promise<void | any[]> {
const client = await this.queue.client;
const message = err?.message;

Expand Down Expand Up @@ -711,10 +711,10 @@ export class Job<
);
}

const code = results[results.length - 1][1] as number;
if (code < 0) {
const result = results[results.length - 1][1] as number;
if (result < 0) {
throw this.scripts.finishedErrors({
code,
code: result,
jobId: this.id,
command,
state: 'active',
Expand All @@ -730,6 +730,10 @@ export class Job<
}

this.attemptsMade += 1;

if (Array.isArray(result)) {
return raw2NextJobData(result);
}
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,14 @@ will never work with more accuracy than 1ms. */
return;
}

await job.moveToFailed(err, token);
const result = await job.moveToFailed(err, token, true);
this.emit('failed', job, err, 'active');

if (result) {
const [jobData, jobId, limitUntil, delayUntil] = result;
this.updateDelays(limitUntil, delayUntil);
return this.nextJobFromJobData(jobData, jobId, token);
}
} catch (err) {
this.emit('error', <Error>err);
// It probably means that the job has lost the lock before completion
Expand Down

0 comments on commit f917b80

Please sign in to comment.