简体   繁体   English

使用 NodeJS Bull Queue 独立处理器

[英]Use NodeJS Bull Queue seperate processor

I'm trying to run the process in different processor file itsef as its in bull documentation , I've added the file as below.我正在尝试在不同的processor文件 itef 中运行该process ,就像它在bull文档中一样,我添加了如下文件。

// -------- Queue.js ----------

formatQueue.process(__dirname + "/processors/format-worker.js");


// On Cmplete Handler

formatQueue.on('completed', function(job, result){
    console.log(result, "Format-Complete-job"); // result is undefined
    if(job.data.is_well_format){
        existenceQueue.add(job.data, { attempts: 3, backoff: 1000 });
    }else QueueModel.lastStep(job.data)
});

// -------- Queue.js ends ---------

//format-worker.js
Validator = require("../../validators");
module.exports = (job) => {
    Validator.Format.validate(job.data, (data) => {
        job.data = data;
        return Promise.resolve(data);
    });
}

Now On Job complete which I was using before, I used to get job data with updated job parameters.现在在我之前使用的作业完成时,我曾经使用更新的作业参数获取作业数据。 Now I'm not getting updated job data.现在我没有得到更新的工作数据。 and the second parameter which is there in the documentation ie result is undefined .文档中的第二个参数,即resultundefined Now how can I get the updated job data in this case.现在在这种情况下如何获取更新的作业数据。

The job and the processors are working fine it I run the process as below. jobprocessors工作正常,我运行如下process

formatQueue.process(function(job, done){
    Validator.Format.validate(job.data, (data) => {
        job.data = data;
        done();
    });
});

In this case, The job data itself is getting updated and that works aswell.在这种情况下,作业数据本身正在更新,并且效果也很好。

I have multiple queues doing the different jobs, Once the previous job is finished with success, then only I need the other job to start the work.我有多个队列在做不同的工作,一旦前一个工作成功完成,那么只有我需要另一个工作来开始工作。 I have another question and I've mentioned the usecase there.我还有另一个问题,我已经提到了那里的用例。 please check it here . 请在这里查看

Let me help you and also give you some hints.让我帮助你,也给你一些提示。 First of all there are some errors in your code.首先,您的代码中有一些错误。 The process function is not returning the promise that you create inside the validate callback.进程 function 没有返回您在验证回调中创建的 promise。 Now, I do not know what Validator.Format.validate returns, so to be on the safe side I will write it like this:现在,我不知道Validator.Format.validate返回什么,所以为了安全起见,我会这样写:

module.exports = job => {
  return new Promise(resolve => {
    Validator.Format.validate(job.data, data => {
      resolve(data);
    });
  });
};

Secondly, in general it is more robust to add the next job inside the process handler itself instead of on the "completed" event callback, the reason is that by doing so you will get a much more robust solution, in the case adding the next job fails for some reason, the job will fail and you will be able to retry or examine why it failed and so on.其次,一般来说,在流程处理程序本身而不是在“完成”事件回调中添加下一个作业更健壮,原因是这样做您将获得一个更健壮的解决方案,在添加下一个的情况下作业由于某种原因失败,作业将失败,您将能够重试或检查失败的原因等等。

module.exports = (job) => {
  return new Promise(resolve => {
    Validator.Format.validate(job.data, (data) => {
      if(data.is_well_format){
        resolve(existenceQueue.add(data, { attempts: 3, backoff: 1000 }));
      }else {
        resolve(QueueModel.lastStep(data))
      }
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM