【问题标题】:NodeJS how to retry queue job using bull in certain time after that job is failedNodeJS如何在该作业失败后的特定时间内使用公牛重试队列作业
【发布时间】:2019-09-06 03:24:48
【问题描述】:

我正在尝试使用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;

我该如何解决这个问题?

【问题讨论】:

    标签: node.js queue jobs


    【解决方案1】:

    根据文件here

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      相关资源
      最近更新 更多