Dragonfly Cloud is now available in the AWS Marketplace - learn more

Question: How can I log jobs in BullMQ?

Answer

BullMQ does not provide built-in functionality for job logging, but you can implement it by combining the event-driven nature of the system with some logging library like Winston or Bunyan. Here's how you could do it:

const Queue = require('bullmq').Queue; const queue = new Queue('my-queue'); queue.on('completed', (job) => { console.log(`Job with ID ${job.id} has been completed`); }); queue.on('failed', (job, err) => { console.error(`Job with ID ${job.id} has failed with error: ${err.message}`); });

In this example, we've attached listeners to the 'completed' and 'failed' events of the queue. When a job is completed or fails, a corresponding message will be logged.

To enrich the logging experience, consider integrating a proper logging library such as Winston, Bunyan, or any other preferred library.

Remember that jobs can also fail 'stalled' and 'progress' statuses for which you might want to add more handlers. But please note, the code above is a simple example to illustrate the concept, the actual implementation may vary based on your application needs and context.

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

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