Question: What are BullMQ queue events and how can they be used?

Answer

In BullMQ, a sophisticated Redis-based job queue for Node.js, events are emitted during the lifecycle of jobs. These events can be utilized to monitor job progress or trigger certain actions when something occurs.

Here are some common events:

  1. completed: Emitted every time a job has been completed successfully.
  2. failed: Emitted when a job fails to execute.
  3. stalled: This is emitted when a job has been stalled. Note that job stalling is a sign of improper shutdown of your processing function.
  4. progress: Emitted when a job updates its progress percentage.
  5. waiting: Emitted when a job is waiting in the queue (i.e., it has been added).
  6. active: Emitted when a job starts to be processed (i.e., moved from waiting state to active).

Here's an example of how you can listen to these events:

const queue = new Queue('my-queue'); queue.on('completed', (job, result) => { console.log(`Job with id ${job.id} has completed. Result: ${result}`); }); queue.on('failed', (job, err) => { console.log(`Job with id ${job.id} has failed with error ${err.message}`); }); queue.on('progress', (job, progress) => { console.log(`Job with id ${job.id} is ${progress}% complete.`); });

Remember, Jobs are atomic and every event is tied to each specific job instance. You should not use these events as a mechanism to issue commands to other processes or as inter-process communication.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

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