我正在尝试创建一个工作,在使用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;
我该如何解决这个问题?
根据文件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.
}