From c55583701e5bdd4e6436a61c833e506bc05749de Mon Sep 17 00:00:00 2001 From: Manuel Astudillo Date: Sun, 20 Oct 2019 11:53:19 +0200 Subject: [PATCH] feat(workers): support for async backoffs --- src/classes/job.ts | 2 +- src/test/test_worker.ts | 54 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/classes/job.ts b/src/classes/job.ts index fb9d8a8f26..5a0ab3533c 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -254,7 +254,7 @@ export class Job { const opts = queue.opts as WorkerOptions; // Check if backoff is needed - const delay = Backoffs.calculate( + const delay = await Backoffs.calculate( this.opts.backoff, this.attemptsMade, opts.settings && opts.settings.backoffStrategies, diff --git a/src/test/test_worker.ts b/src/test/test_worker.ts index 10886d181d..30ffd557ca 100644 --- a/src/test/test_worker.ts +++ b/src/test/test_worker.ts @@ -1346,7 +1346,53 @@ describe('workers', function() { await queueScheduler.close(); }); - it.skip('should not retry a job that has been removed', async () => { + it('should be able to handle a custom backoff if it returns a promise', async function() { + this.timeout(12000); + + const queueScheduler = new QueueScheduler(queueName); + await queueScheduler.waitUntilReady(); + + const worker = new Worker( + queueName, + async (job: Job) => { + if (job.attemptsMade < 2) { + throw new Error('some error'); + } + }, + { + settings: { + backoffStrategies: { + async custom() { + return delay(500); + }, + }, + }, + }, + ); + const start = Date.now(); + await queue.add( + 'test', + { foo: 'bar' }, + { + attempts: 3, + backoff: { + type: 'custom', + }, + }, + ); + + await new Promise(resolve => { + worker.on('completed', () => { + const elapse = Date.now() - start; + expect(elapse).to.be.greaterThan(1000); + resolve(); + }); + }); + + await worker.close(); + }); + + it('should not retry a job that has been removed', async () => { const queueScheduler = new QueueScheduler(queueName); await queueScheduler.waitUntilReady(); @@ -1379,7 +1425,8 @@ describe('workers', function() { try { await job.retry(); } catch (err) { - expect(err.message).to.equal(RetryErrors.JobNotExist); + // expect(err.message).to.equal(RetryErrors.JobNotExist); + expect(err.message).to.equal('Retried job not exist'); } const completedCount = await queue.getCompletedCount(); @@ -1389,6 +1436,7 @@ describe('workers', function() { } catch (err) { reject(err); } + resolve(); }); worker.on('failed', failedHandler); @@ -1396,7 +1444,7 @@ describe('workers', function() { await worker.close(); await queueScheduler.close(); - }); + }).timeout(5000); it.skip('should not retry a job that has been retried already', async () => { let attempts = 0;