FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(backoff): allow no delay arg when setting immediate strategy (#154) · bee-queue/bee-queue@6f1d62f · GitHub

Commit 6f1d62f

Browse files
andauthored
fix(backoff): allow no delay arg when setting immediate strategy (#154)
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`. Co-authored-by: Hugh Secker-Walker <hsw@hodain.net>
1 parent b9ffac9 commit 6f1d62f

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎index.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ declare namespace BeeQueue {
8585

8686
setId(id: string): this;
8787
retries(n: number): this;
88-
backoff(strategy: "immediate" | "fixed" | "exponential", delayFactor: number): this;
88+
backoff(strategy: "immediate" | "fixed" | "exponential", delayFactor?: number): this;
8989
delayUntil(dateOrTimestamp: Date | number): this;
9090
timeout(milliseconds: number): this;
9191
save(): Promise<this>;

‎lib/job.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ class Job extends Emitter {
124124
if (!strategies.has(strategy)) {
125125
throw new Error('unknown strategy');
126126
}
127-
if (!Number.isSafeInteger(delay) || delay <= 0) {
127+
128+
const isInvalidDelay = !Number.isSafeInteger(delay) || delay <= 0;
129+
if (strategy !== 'immediate' && isInvalidDelay) {
128130
throw new Error('delay must be a positive integer');
129131
}
130132
this.options.backoff = {

‎test/queue-test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,40 @@ describe('Queue', (it) => {
14641464
t.true(calls[1] - calls[0] >= 100);
14651465
});
14661466

1467+
it('should handle immediate backoff', async (t) => {
1468+
const queue = t.context.makeQueue({
1469+
activateDelayedJobs: true
1470+
});
1471+
1472+
const calls = [];
1473+
1474+
queue.process(async (job) => {
1475+
t.deepEqual(job.options.backoff, {
1476+
strategy: 'immediate'
1477+
});
1478+
t.deepEqual(job.data, {is: 'immediate'});
1479+
calls.push(Date.now());
1480+
if (calls.length === 1) {
1481+
throw new Error('forced retry');
1482+
}
1483+
t.is(calls.length, 2);
1484+
});
1485+
1486+
const succeed = helpers.waitOn(queue, 'succeeded', true);
1487+
1488+
await queue.createJob({is: 'immediate'})
1489+
.retries(2)
1490+
.backoff('immediate')
1491+
.save();
1492+
1493+
await succeed;
1494+
1495+
t.is(calls.length, 2);
1496+
1497+
// Temporal sanity
1498+
t.true(calls[1] >= calls[0]);
1499+
});
1500+
14671501
it('should handle exponential backoff', async (t) => {
14681502
const queue = t.context.makeQueue({
14691503
activateDelayedJobs: true

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL