[英]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.