Question: How can I set up repeatable jobs in BullMQ?


In BullMQ, a powerful Redis-based job queue for handling distributed jobs or messages in Node.js, you can create repeatable jobs. Repeatable jobs are those that will run periodically based on a cron expression or every certain amount of milliseconds.

Here's an example of how to create a repeatable job using BullMQ:

const Queue = require('bullmq').Queue; // Instantiate new queue. const myQueue = new Queue('myQueue'); // Add a repeatable job. myQueue.add('myRepeatJob', {foo: 'bar'}, { // Job would repeat every 5 seconds. repeat: {every: 5000} });

In this code, you're first creating a new queue called 'myQueue'. You then add a repeatable job to the queue, which is named 'myRepeatJob', with foo: bar as the job data. The {repeat: {every: 5000}} option sets the job to repeat every 5000 milliseconds (or 5 seconds).

Remember that when you're adding repeatable jobs, the job ID must be the same for jobs of the same type. Also, you have to consider job uniqueness, delayed jobs, and failed jobs when working with repeatable jobs.

For instance, to schedule a job with a cron expression, you may use the cron option:

myQueue.add('myRepeatJob', {foo: 'bar'}, { // Job would repeat at every minute start (00 * * * *) repeat: {cron: '0 * * * *'} });

This will schedule the job to execute at the start of every hour.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.