Node.js:除非重新启动服务器,否则后台作业不会重新启动

时间:2020-05-04 14:50:58

标签: javascript node.js

我是node.js的新手,请原谅我,以防万一。

我正在尝试将Bull集成到应用程序中进行后台处理,并且注意到在处理过程中发生错误时,作业不会在重启动服务器之前重试。即使发生错误,作业仍保持active状态,并且不会移至failed

下面是我代码的相关部分

queue.js
const Queue = require('bull');
const { setQueues } = require('bull-board');
const csvExportsQueue = new Queue('csv exports');
setQueues([csvExportsQueue]);
module.exports = csvExportsQueue;

我正在侦听失败和错误事件,但是发生异常时似乎都没有触发

worker.js
const csvExportProcessing = require('./queue');
const processExport = require('./process_export');

csvExportProcessing.process(function(job, done){
  processExport.performExport(job.data.params, done).catch(err => {
    console.log('in error handling'); // I see this outputted on the console
  });
});


csvExportProcessing.on('completed', function(job, result){
  console.log('job is now completed');
});


csvExportProcessing.on('failed', function(job, err){
  console.log('failed');
});


csvExportProcessing.on('error', function(job, err){
  console.log('error');
});

因此,在端点/exports上收到了发布请求,该请求将作业添加到队列,下面是代码

const express = require('express');
const router = express.Router();
const exportCsv = require('./process_export');
const exportQueue = require('./queue');

router.post('/', (req, resp, next) => {
  console.log('received request...adding to the queue');
  exportQueue.add({params: req.body}, {attempts:5, backoff: 5000 });
  next();
});

module.exports = router;

然后我使用npm start启动服务器,该服务器显示以下输出

> nodemon server.js

[nodemon] 2.0.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`

任何有关如何解决此问题的帮助都将非常有用。谢谢。

0 个答案:

没有答案