我创建了一个队列作业系统,以使用BullQueue
来生成我的报告系统。但是,当我的工作失败时,我的返回响应出现问题。
我的代码:
处理器
@Process('generate_report')
async generateReport(job: Job<{options: OptionsDto, reportType: Type}>): Promise<any> {
try {
switch (job.data.reportType) {
case NORMAL:
return this.generateNormalReport(job.data.options);
case EXTERNAL:
return this.generateExternalReport(job.data.options);
default:
break;
}
} catch (error) {
this.logger.error(`[Queue] Failed to generate report.`)
}
}
@OnQueueFailed()
onError(job: Job<any>, error: BadRequestException): void {
this.logger.error(`[Queue] Failed job ${job.id}`, error.stack) // I want to return this response with 500 code when job is failed
}
服务
async addReportGenerationToQueue(options: OptionsDto, reportType: Type): Promise<void> {
try {
await this.reportQueue.add('generate_report', {
options,
reportType
})
this.logger.log(`[Queue] Report was added to queue.`)
} catch (error) {
this.logger.error(`[Queue] The report was not added to the queue.`);
}
}
有人可以告诉我队列中的作业失败时如何返回带错误代码的回购吗?