Clarifying repeating/canceling jobs
Created by: jmonster
Description
I had a difficult time figuring out the proper incantation to create and destroy repeatable jobs. I'm opening this issue to provide a working example for anyone else who might be googling the issues I ran into.
I ran into the following scenarios as I trial-and-errored my way to this solution.
- a repeating job didn't fire until the repetition time (rather than immediately)
- the job couldn't be deleted
- only 1 job was being created (despite attempting to create 100 jobs with different data payloads)
- jobs weren't being removed when they were done or failed
The core point of confusion seemed to be where/how to pass jobId
; it needs to be passed as part of the repeatOpts
object.
Correct example
const ONE_SECOND = 1000;
const ARBITRARY_INTERVAL = 3000;
const Queue = require('bull');
const myQueue = new Queue('my-queue-name',
{
limiter: { max: 1, duration: ONE_SECOND }, // 1 job/second
defaultJobOptions: {
removeOnFail: true,
removeOnComplete: true
}
}
);
const someCollection = [1,2,3];
someCollection.forEach((jobId) => {
const repeatOpts = { jobId, every: ARBITRARY_INTERVAL }; // note that `jobId` is part of the `repeat` options
// adding to the queue
myQueue.add({ arbitraryDataKey: 123 }, { repeat: repeatOpts }); // an object with a `repeat` key
// removing from the queue
myQueue.removeRepeatable(repeatOpts); // the repeatOpts are passed directly; not an object with a `repeat` key
});