Skip to content

Commit

Permalink
feat(backoff): add option to specify options for custom backoff strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Jul 15, 2021
1 parent 0ba93e7 commit e573010
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
35 changes: 35 additions & 0 deletions PATTERNS.md
Expand Up @@ -166,6 +166,41 @@ myQueue.add({foo: 'bar'}, {
});
```

You may specify options for your strategy:
```js
var Queue = require('bull');

var myQueue = new Queue("Server B", {
settings: {
backoffStrategies: {
// truncated binary exponential backoff
binaryExponential: function (attemptsMade, err, options) {
// Options can be undefined, you need to handle it by yourself
if (!options) {
options = {}
}
var delay = options.delay || 1000;
var truncate = options.truncate || 1000;
console.error({attemptsMade, err, options});
return Math.round(Math.random() * (Math.pow(2, Math.max(attemptsMade, truncate)) - 1) * delay)
}
}
}
});

myQueue.add({foo: 'bar'}, {
attempts: 10,
backoff: {
type: 'binaryExponential',
options: {
delay: 500,
truncate: 5
}
}
});

```

You may base your backoff strategy on the error that the job throws:
```js
const Queue = require('bull');
Expand Down
10 changes: 7 additions & 3 deletions lib/backoffs.js
Expand Up @@ -42,11 +42,15 @@ module.exports = {
}
},

calculate(backoff, attemptsMade, customStrategies, err) {
calculate(backoff, attemptsMade, customStrategies, err, strategyOptions) {
if (backoff) {
const strategy = lookupStrategy(backoff, customStrategies);
const strategy = lookupStrategy(
backoff,
customStrategies,
strategyOptions
);

return strategy(attemptsMade, err);
return strategy(attemptsMade, err, strategyOptions);
}
}
};
4 changes: 3 additions & 1 deletion lib/job.js
Expand Up @@ -274,6 +274,7 @@ Job.prototype.moveToFailed = async function(err, ignoreLock) {
err = err || { message: 'Unknown reason' };

this.failedReason = err.message;

await this.queue.isReady();

let command;
Expand All @@ -288,7 +289,8 @@ Job.prototype.moveToFailed = async function(err, ignoreLock) {
this.opts.backoff,
this.attemptsMade,
this.queue.settings.backoffStrategies,
err
err,
_.get(this, 'opts.backoff.options', null)
);

if (delay === -1) {
Expand Down
42 changes: 42 additions & 0 deletions test/test_queue.js
Expand Up @@ -2273,6 +2273,48 @@ describe('Queue', () => {
});
});

it('should pass strategy options to custom backoff', function(done) {
this.timeout(12000);
queue = utils.buildQueue('test retries and backoffs', {
settings: {
backoffStrategies: {
custom(attemptsMade, err, strategyOptions) {
expect(strategyOptions.id).to.be.equal('FOO42');
return attemptsMade * 1000;
}
}
}
});
let start;
queue.isReady().then(() => {
queue.process((job, jobDone) => {
if (job.attemptsMade < 2) {
throw new Error('Not yet!');
}
jobDone();
});

start = Date.now();
queue.add(
{ foo: 'bar' },
{
attempts: 3,
backoff: {
type: 'custom',
options: {
id: 'FOO42'
}
}
}
);
});
queue.on('completed', () => {
const elapse = Date.now() - start;
expect(elapse).to.be.greaterThan(3000);
done();
});
});

it('should not retry a job if the custom backoff returns -1', done => {
queue = utils.buildQueue('test retries and backoffs', {
settings: {
Expand Down

0 comments on commit e573010

Please sign in to comment.