NodeJS如何在该作业失败后的特定时间内使用公牛重试队列作业

Bil*_*hia 3 queue jobs node.js

我正在尝试创建一个作业,该作业将在使用bull queue. 但工作从不拖延,总是紧随其后。这是我当前的代码:

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));

Run Code Online (Sandbox Code Playgroud)

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;

Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Dha*_*ary 12

根据这里的文件

创建新作业时您可以传递作业选项。其中有尝试和退避选项。

在您创建工作的情况下,您可以通过

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
});
Run Code Online (Sandbox Code Playgroud)

退避可以是毫秒数,或者您可以传递单独的退避选项,例如:

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.
}
Run Code Online (Sandbox Code Playgroud)