Billy Adelphia
Billy Adelphia

Reputation: 1027

NodeJS how to retry queue job using bull in certain time after that job is failed

I'm trying to create a job that will retry in certain time after that job is failed using bull queue. But the job never delayed, always excute right after. Here my current code :

const Queue = require('bull');
const queue = new Queue('send notiffication to main app', 'redis://127.0.0.1:6379');
const sendDepositNotificationToMainAppJob = require('../jobs/sendDepositNotificationToMainApp');

 queue.process(new sendDepositNotificationToMainAppJob(depositSuccess));

sendDepositNotificationToMainApp.js

const Queue = require('bull');
const queue = new Queue('send notif to main app', 'redis://127.0.0.1:6379');
class sendDepositNotificationToMainApp {
    constructor(depositSuccess){
        return handle(depositSuccess);
    }
}

const handle = async (depositSuccess) => {
  try {
   //some function here
   }.catch(e){
     //if error retry job here 
      queue.add(new sendDepositNotificationToMainApp(depositSuccess), {delay : 5000})
   }

}

module.exports = sendDepositNotificationToMainApp;

How do I fix this issue ?

Upvotes: 6

Views: 22123

Answers (1)

Dhaval Chaudhary
Dhaval Chaudhary

Reputation: 5815

As per the documents here

When you are creating new job You can pass job options. In which there's attempts and backoff option.

In your case while creating job you can just pass

Queue.add('<You-job-name>', <Your-Data>, {
   attempts: 5, // If job fails it will retry till 5 times
   backoff: 5000 // static 5 sec delay between retry
});

Backoff can be number in ms or you can pass separate backoff option like:

interface BackoffOpts{
   type: string; // Backoff type, which can be either `fixed` or `exponential`. 
   //A custom backoff strategy can also be specified in `backoffStrategies` on the queue settings.
   delay: number; // Backoff delay, in milliseconds.
}

Upvotes: 23

Related Questions