Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bull separate process for job processing unable to catch events on queue

In my node.js application i am implementing background job processing using bull module. I need to catch certain events on the queue like completed, failed, error.

I am trying to have a separate process for process function, but the problem is after moving the job processing to a process function i am unable to catch any of the event like completed, failed, error.

Below is my code

processor.js

module.exports =  async function(job, done) {
  try {
    await processExport.performExport(job.data.params, done);
  } catch(err) {
    console.log(err);
    console.log('in error handling');
  };
}

worker.js

const csvExportProcessing = require('../queue');
csvExportProcessing.process(5, __dirname + '/processor.js');

csvExportProcessing.on('completed', function(job, result){
  console.log('job is now completed');
});

csvExportProcessing.on('failed', function(job, err){
  if(job.attemptsMade == job.opts.attempts) {
    //send a postback
  }
});


csvExportProcessing.on('global:error', function(job, err){
  console.log('Is last attempt?  => ', (job.attemptsMade === job.opts.attempts));
});

like image 955
opensource-developer Avatar asked Dec 23 '25 02:12

opensource-developer


1 Answers

I was able to catch the events using the below code, here is the relevant code to catch failed event

const myQueue =  process.env.NODE_ENV == 'development' ? new Queue('myqueuename') : new Queue('myqueuename', 'redis://redis:6379/13');
setQueues([myQueue]);
myQueue.on('failed', async function(job, err){
  if(job.attemptsMade == job.opts.attempts) {
    //do something
  }
});
like image 153
opensource-developer Avatar answered Dec 24 '25 16:12

opensource-developer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!