Skip to content

Commit

Permalink
Merge 8f9bb1b into 0b7a73a
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginden committed May 13, 2020
2 parents 0b7a73a + 8f9bb1b commit 09c1a68
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
35 changes: 35 additions & 0 deletions PATTERNS.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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
var Queue = require('bull');
Expand Down
10 changes: 7 additions & 3 deletions lib/backoffs.js
Original file line number Diff line number Diff line change
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);
}
}
};
3 changes: 2 additions & 1 deletion lib/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ Job.prototype.moveToFailed = 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
47 changes: 46 additions & 1 deletion test/test_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,10 @@ describe('Queue', () => {
queue.add({});
queue.add({});

queue.on('completed', _.after(2, () => done()));
queue.on(
'completed',
_.after(2, () => done())
);
});

//This job use delay to check that at any time we have 4 process in parallel.
Expand Down Expand Up @@ -2192,6 +2195,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 09c1a68

Please sign in to comment.