Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ const counts = await queue.checkHealth();
console.log('job state counts:', counts);
```

#### Queue#close([cb])
#### Queue#close([timeout], [cb])

Closes the queue's connections to Redis. Idempotent.

Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare class BeeQueue<T = any> extends EventEmitter {
jobs: any;
paused: boolean;
settings: any;
backoffStrategies: Map<string, (job: BeeQueue.Job) => number>;

constructor(name: string, settings?: BeeQueue.QueueSettings);

Expand Down Expand Up @@ -116,7 +117,7 @@ declare namespace BeeQueue {
setId(id: string): this;
retries(n: number): this;
backoff(
strategy: 'immediate' | 'fixed' | 'exponential',
strategy: keyof typeof this.backoffStrategies,
delayFactor?: number
): this;
delayUntil(dateOrTimestamp: Date | number): this;
Expand Down
4 changes: 1 addition & 3 deletions lib/job.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

const Emitter = require('events').EventEmitter;

const helpers = require('./helpers');
const strategies = require('./backoff');

class Job extends Emitter {
constructor(queue, jobId, data, options) {
Expand Down Expand Up @@ -146,7 +144,7 @@ class Job extends Emitter {
}

backoff(strategy, delay) {
if (!strategies.has(strategy)) {
if (!this.queue.backoffStrategies.has(strategy)) {
throw new Error('unknown strategy');
}

Expand Down
5 changes: 4 additions & 1 deletion lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Queue extends Emitter {
this.jobs = new Map();
this.activeJobs = new Set();
this.checkTimer = null;
this.backoffStrategies = new Map(backoff);

this._closed = null;
this._isClosed = false;
Expand Down Expand Up @@ -581,7 +582,9 @@ class Queue extends Emitter {
? job.options.backoff.strategy
: 'immediate';
const strategy =
job.options.retries > 0 ? backoff.get(strategyName) : null;
job.options.retries > 0
? this.backoffStrategies.get(strategyName)
: null;
const delay = strategy ? strategy(job) : -1;
if (delay < 0) {
job.status = 'failed';
Expand Down
41 changes: 41 additions & 0 deletions test/queue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,47 @@ describe('Queue', (it) => {
t.throws(() => job.backoff('fixed', 44.5), /positive integer/i);
});

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

queue.backoffStrategies.set(
'my-custom-backoff',
(job) => job.options.backoff.delay
);

const calls = [];

queue.process(async (job) => {
t.deepEqual(job.options.backoff, {
strategy: 'my-custom-backoff',
delay: 100,
});
t.deepEqual(job.data, {is: 'my-custom-backoff'});
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: 'my-custom-backoff'})
.retries(2)
.backoff('my-custom-backoff', 100)
.save();

await succeed;

t.is(calls.length, 2);

// Ensure there was a delay.
t.true(calls[1] - calls[0] >= 100);
});

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