Skip to content

Commit

Permalink
Allow non-positive integer delays for immediate backoff strategy
Browse files Browse the repository at this point in the history
Per [the
documentation](https://github.com/mixmaxhq/bee-queue#jobbackoffstrategy-delayfactor)
you should be able to call `job.backoff('immediate')` to set an
immediate backoff strategy for retried jobs. However, the `backoff`
method explicitly disallowed this by checking for
`Number.isSafeInteger(undefined)` which returns false. This means
you currently cannot specify an immediate backoff strategy without also specifying a positive delay value.

To fix this, only check for valid integers when the strategy is not
`immediate`.
  • Loading branch information
Garret Meier authored and Eli Skeggs committed Jan 27, 2020
1 parent 223151c commit dada8ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/job.js
Expand Up @@ -124,7 +124,9 @@ class Job extends Emitter {
if (!strategies.has(strategy)) {
throw new Error('unknown strategy');
}
if (!Number.isSafeInteger(delay) || delay <= 0) {

const isInvalidDelay = !Number.isSafeInteger(delay) || delay <= 0;
if (strategy !== 'immediate' && isInvalidDelay) {
throw new Error('delay must be a positive integer');
}
this.options.backoff = {
Expand Down
34 changes: 34 additions & 0 deletions test/queue-test.js
Expand Up @@ -1464,6 +1464,40 @@ describe('Queue', (it) => {
t.true(calls[1] - calls[0] >= 100);
});

it('should handle immediate backoff', async (t) => {
const queue = t.context.makeQueue({
activateDelayedJobs: true
});

const calls = [];

queue.process(async (job) => {
t.deepEqual(job.options.backoff, {
strategy: 'immediate'
});
t.deepEqual(job.data, {is: 'immediate'});
calls.push(Date.now());
if (calls.length === 1) {
throw new Error('forced retry');
}
t.is(calls.length, 2);
});

const succeed = helpers.waitOn(queue, 'succeeded', true);

await queue.createJob({is: 'immediate'})
.retries(2)
.backoff('immediate')
.save();

await succeed;

t.is(calls.length, 2);

// Ensure there no delay.
t.true(calls[1] - calls[0] <= 100);
});

it('should handle exponential backoff', async (t) => {
const queue = t.context.makeQueue({
activateDelayedJobs: true
Expand Down

0 comments on commit dada8ad

Please sign in to comment.