Skip to content

Commit

Permalink
feat(workers): support for async backoffs
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Oct 20, 2019
1 parent 1e808d2 commit c555837
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<BackoffOptions>this.opts.backoff,
this.attemptsMade,
opts.settings && opts.settings.backoffStrategies,
Expand Down
54 changes: 51 additions & 3 deletions src/test/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -1389,14 +1436,15 @@ describe('workers', function() {
} catch (err) {
reject(err);
}
resolve();
});

worker.on('failed', failedHandler);
});

await worker.close();
await queueScheduler.close();
});
}).timeout(5000);

it.skip('should not retry a job that has been retried already', async () => {
let attempts = 0;
Expand Down

0 comments on commit c555837

Please sign in to comment.