Question: How can I set up repeatable jobs in BullMQ?
Answer
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?
Other Common BullMQ Questions (and Answers)
- What are the differences between BullMQ and Amazon SQS?
- What are the key differences between BullMQ and Agenda?
- What is the difference between BullMQ and RabbitMQ?
- What are the differences between BullMQ and Bull in job queueing?
- What are the differences between BullMQ and Celery?
- How can I use multiple consumers with BullMQ?
- How can I monitor the health of my BullMQ queue?
- How can I use BullMQ for job queue management in Node.js?
- How can you handle errors in BullMQ?
- What are the differences between BullMQ and Kafka?
- What are some best practices for using BullMQ?
- What are some common use cases of BullMQ?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost