繁体   English   中英

NodeJS如何在该作业失败后的某个时间内使用bull重试队列作业

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

我正在尝试创建一个作业,在该作业使用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));

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;

我该如何解决这个问题?

根据这里的文件

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

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

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

退避可以是以毫秒为单位的数字,或者您可以通过单独的退避选项,如:

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.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM