Skip to content

Commit

Permalink
feat(job): add moveToWait method for manual processing
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Dec 20, 2024
1 parent 347b618 commit 2ba80a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ export class Job<
return result;
}

/**
* Moves a job to the wait or prioritized state.
*
* @param token - Worker token used to acquire completed job.
* @returns Returns pttl.
*/
moveToWait(token: string): Promise<number> {
return this.scripts.moveJobFromActiveToWait(this.id, token);
}

private async shouldRetryJob(err: Error): Promise<[boolean, number]> {
if (
this.attemptsMade + 1 < this.opts.attempts &&
Expand Down
2 changes: 1 addition & 1 deletion src/classes/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,6 @@ will never work with more accuracy than 1ms. */
job: Job<DataType, ResultType, NameType>,
token: string,
) {
return this.scripts.moveJobFromActiveToWait(job.id, token);
return job.moveToWait(token);
}
}
15 changes: 15 additions & 0 deletions tests/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,21 @@ describe('Job', function () {
});
});

describe('.moveToWait', () => {
it('moves job to wait from active', async function () {
const worker = new Worker(queueName, null, { connection, prefix });
const token = 'my-token';
await Job.create(queue, 'test', { foo: 'bar' });
const job = (await worker.getNextJob(token)) as Job;
const isWaiting = await job.isWaiting();
expect(isWaiting).to.be.equal(false);
await job.moveToWait('0');
const isisWaiting2 = await job.isWaiting();
expect(isisWaiting2).to.be.equal(true);
await worker.close();
});
});

describe('.changeDelay', () => {
it('can change delay of a delayed job', async function () {
this.timeout(8000);
Expand Down

0 comments on commit 2ba80a6

Please sign in to comment.