简体   繁体   English

使用公牛的作业处理微服务

[英]Job processing microservices using bull

I would like to process scheduled jobs using node.js bull .我想使用 node.js bull处理预定的作业。 Basically I have two processors that handle 2 types of jobs.基本上我有两个处理器来处理两种类型的工作。 There is one configurator that configures the jobs which will be added to the bull queue using cron.有一个配置器可以配置将使用 cron 添加到公牛队列的作业。

The scheduler will be in one microservice and the each of the processor will be a separate microservice.调度程序将位于一个微服务中,每个处理器将是一个单独的微服务。 So I will be having 3 micro services.所以我将拥有 3 个微服务。

My question is am I using the correct pattern with bull?我的问题是我在公牛上使用正确的模式吗?

index.js index.js

const Queue = require('bull');

const fetchQueue = new Queue('MyScheduler');
fetchQueue.add("fetcher", {name: "earthQuakeAlert"}, {repeat: {cron: '1-59/2 * * * *'}, removeOnComplete: true});
fetchQueue.add("fetcher", {name: "weatherAlert"}, {repeat: {cron: '3-59/3 * * * *'}, removeOnComplete: true});

processor-configurator.js处理器配置器.js

const Queue=require('bull');

const scheduler = new Queue("MyScheduler");
scheduler.process("processor", __dirname + "/alert-processor");

fetcher-configurator.js fetcher-configurator.js

const Queue=require('bull');

const scheduler = new Queue("MyScheduler");
scheduler.process("fetcher", __dirname+"/fetcher");

fetcher.js fetcher.js

const Queue = require('bull');
const moment = require('moment');

module.exports = function (job) {
    const scheduler = new Queue('MyScheduler');
    console.log("Insider processor ", job.data, moment().format("YYYY-MM-DD hh:mm:ss"));
    scheduler.add('processor', {'name': 'Email needs to be sent'}, {removeOnComplete: true});
    return Promise.resolve()
};

alert-processor.js警报处理器.js

const Queue = require('bull');
const moment = require('moment');

module.exports = function (job) {
    const scheduler = new Queue('MyScheduler');
    console.log("Insider processor ", job.data, moment().format("YYYY-MM-DD hh:mm:ss"));
    scheduler.add('processor', {'name': 'Email needs to be sent'}, {removeOnComplete: true});
    return Promise.resolve()
};

There will be three microservices -将有三个微服务 -

  1. node index.js节点索引.js
  2. node fetcher-configurator.js节点 fetcher-configurator.js
  3. node processor-configurator.js节点处理器配置器.js

I see inconsistent behavior from bull.我看到公牛的行为不一致。 Sometimes I am getting the error Missing process handler for job type有时我收到错误缺少作业类型的流程处理程序

Quoting myself with a hope this will be helpful for someone else引用自己希望这对其他人有帮助

This is because both workers use the same queue.这是因为两个工作人员使用相同的队列。 Worker tries to get next job from queue, receives a job with wrong type (eg "fetcher" instead of "processor") and fails because it knows how to handle "processor" and doesn't know what to do with "fetcher". Worker 尝试从队列中获取下一个作业,接收到错误类型的作业(例如,“fetcher”而不是“processor”)并失败,因为它知道如何处理“processor”并且不知道如何处理“fetcher”。 Bull doesn't allow you to take only compatible jobs from queue, both workers should be able to process all types of jobs. Bull 不允许您只从队列中获取兼容的工作,两个工作人员都应该能够处理所有类型的工作。 The simplest solution would be to use two different queues, one for processors and one for fetchers.最简单的解决方案是使用两个不同的队列,一个用于处理器,一个用于提取器。 Then you can remove names from jobs and processors, it won't be needed anymore since name is defined by the queue.然后您可以从作业和处理器中删除名称,因为名称由队列定义,因此不再需要它。

https://github.com/OptimalBits/bull/issues/1481 https://github.com/OptimalBits/bull/issues/1481

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

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