我正在尝试创建一个作业,该作业将在使用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;
如何解决此问题?
答案 0 :(得分:0)
根据文档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.
}