const { Queue } = require('bullmq');
const queue = new Queue('myQueue', {
defaultJobOptions: {
attempts: 5,
backoff: {
type: 'exponential',
delay: 1000,
},
},
});
async function main() {
try {
await queue.add('myJob', { foo: 'bar' });
} catch (err) {
if (err.name === 'BullMQRedisError') {
console.log('Redis connection error, retrying...');
await queue.add('myJob', { foo: 'bar' });
} else {
console.error(err);
}
}
}
main();
在代码中添加异常捕获后,当 Redis 连接错误时将进行重试,直到连接成功为止。