node.js - 在 Node.js 中使用 Bull 有条件地重试失败的作业
问题描述
我知道我们可以使用下面的backoff
属性在Bull 中重试失败的作业Bull
var Queue = require('bull');
var myQueue = new Queue('myQueue');
runQueryQueue
.add(data, {
jobId: uid,
attempts: 10,
backoff: {
type: 'exponential',
delay: 2000,
},
stackTraceLimit: 100,
})
.then((result) => {})
.catch(console.log);
但我只想在失败原因值得重试时重试 - 例如超时、速率限制等,而不是在错误与用户输入等相关时重试。
如何检查失败错误消息并决定重试?
解决方案
您可以将作业标记为完成,例如不抛出错误,而是将错误数据写入作业完成的结果。如果有任何错误迹象,可以以不同的方式处理结果。
async function jobProcess(job) {
if (doNotRetryError) {
return doNotRetryError
} else if (anyOtherError) {
throw new Error('retry')
} else {
return {success: true}
}
}
async function jobCompleted(job, result) {
if (result instanceof Error) {
// some error happened, but job shouldn't be retried
} else {
// job completed
}
}
推荐阅读
- android - 如果 handler.post(runnable) 非常快会发生什么
- ios - generateCGImages根据实际时间异步产生重复图像
- python - While循环用户输入?
- mysql-5.6 - 检查是否安装了 percona-xtrabackup?
- java - 如何从我的 java bean 中获取名称和 ID?
- sql-server - SQL Server CPU 时间大于已用时间 (MAXDOP 1)
- rxjs - 为什么我需要将管道与 shareReplay 一起使用?
- r - 如何为两个不同的图形创建两个不同的图例
- amazon-web-services - 如果您使用 AWS 自动扩展应用程序的 Spot 实例,您如何处理日志记录?
- android - NDK 构建问题:aarch64-linux-android-ar.exe xxx.cpp.o:没有这样的文件或目录