From 2ba80a6288606de60189758bff088af6a95306a2 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Thu, 19 Dec 2024 23:22:57 -0500 Subject: [PATCH] feat(job): add moveToWait method for manual processing --- src/classes/job.ts | 10 ++++++++++ src/classes/worker.ts | 2 +- tests/test_job.ts | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/classes/job.ts b/src/classes/job.ts index 623330f91a..5681701804 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -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 { + return this.scripts.moveJobFromActiveToWait(this.id, token); + } + private async shouldRetryJob(err: Error): Promise<[boolean, number]> { if ( this.attemptsMade + 1 < this.opts.attempts && diff --git a/src/classes/worker.ts b/src/classes/worker.ts index b79ddf4648..ce45df96a3 100644 --- a/src/classes/worker.ts +++ b/src/classes/worker.ts @@ -1285,6 +1285,6 @@ will never work with more accuracy than 1ms. */ job: Job, token: string, ) { - return this.scripts.moveJobFromActiveToWait(job.id, token); + return job.moveToWait(token); } } diff --git a/tests/test_job.ts b/tests/test_job.ts index 0393fe006e..c4538b1b30 100644 --- a/tests/test_job.ts +++ b/tests/test_job.ts @@ -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);