简体   繁体   English

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

[英]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 . 我正在尝试创建一个作业,在该作业使用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 ? 我该如何解决这个问题?

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

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

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