我已经在我们的项目中广泛使用Bull库。几天前,我们在处理来自Bull Queue的作业时发现了问题。我们能够在Bull Queue中添加作业,但无法处理该作业。当我签入Taskforce时,我们注意到该作业因空错误响应而延迟/失败。我认为发生这种情况是因为工作进程未与Queue关联。如果我尝试在一段时间后重新启动过程,则相同的代码可以正常工作。我不确定这是Redis或Bull的问题,还是我的使用方式。问题是我能够添加作业,但作业未处理。 (仅在某些情况下如此)
示例代码段:
const BullQueue = require('bull');
class Queue {
constructor(name, connectionName = DBConstant.localRedisConnectionName) {
const options = {};
options.redis = DBConnectionUseCase.getRedisConnectionOptions(connectionName);
this._queue = new BullQueue(name, options);
this._connectionName = connectionName;
this.queueName = name;
}
async initProcessor() {
try {
//TODO: We have noticed that in some cases process does not get attached to Queue.
//We tried adding await here and check result but did not get anything. This needs to be debug.
this._queue.process((job, done) => {
this.process(job, done);
});
}catch(error) {
console.log(`Worker :: ${this.queueName} :: Exception in processor initialisation :: connectionName :: ${this._connectionName} :: Error :: ${error.message} :: ${JSON.stringify(error)}`);
}
console.log(`Worker :: ${this.queueName} :: processor initialised :: connectionName :: ${this._connectionName}`);
}
getQueueName() {
return this.queueName;
}
addJob(data, options) {
console.log(`Worker :: ${this.queueName} :: Job added in Queue :: ${this._connectionName}`);
return this._queue.add(data, options);
}
}
class FetchCustomerData extends Queue{
constructor() {
super(QUEUE_NAME);
}
/**
*
*/
static getInstance() {
if(!queueInstance) {
queueInstance = new FetchCustomerData();
}
return queueInstance;
}
/**
*
*/
initDefaultJob() {
const data = {};
const options = {
// This cron will run in everyday at 12:30AM ISE
repeat: {
cron: '0 20 * * *'
}
};
this.addJob(data, Object.assign({},options, constant.BULL_JOB_OPTIONS));
console.log(`Worker :: ${QUEUE_NAME} :: initial job added :: options :: ${JSON.stringify(Object.assign({},options, constant.BULL_JOB_OPTIONS))}`);
}
/**
*
*/
async process(job, done) {
try {
...
return done(nu;;);
}catch(error) {
console.error(`FetchActivationalUCC.process :: ${uuid} :: Exception :: ${JSON.stringify(error)}`);
return done(error);
}
}
}
我如何使用它:
FetchCustomerData.initProcessor();
const res = await FetchCustomerData.addJob({"key": 123});
包装清单:
"bull": "3.7.0"
"ioredis": "4.9.0"
"node": v10.15.0