node.js - 使用 bull 进行作业处理微服务

标签 node.js job-queue bull.js

我想使用 node.js 处理计划作业 bull 。基本上我有两个处理器可以处理两种类型的作业。有一个配置器可以配置将使用 cron 添加到 bull 队列的作业。

调度程序将位于一个微服务中,每个处理器将是一个单独的微服务。所以我将拥有 3 个微服务。

我的问题是我对 bull 使用了正确的模式吗?

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

const Queue=require('bull');

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

fetcher-configurator.js

const Queue=require('bull');

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

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

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()
};

将有三个微服务 -

  1. Node 索引.js
  2. Node fetcher-configurator.js
  3. Node 处理器配置器.js

我看到 bull 的行为不一致。有时我会收到错误缺少作业类型的进程处理程序

最佳答案

引用我自己的话,希望这对其他人有帮助

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". Bull doesn't allow you to take only compatible jobs from queue, both workers should be able to process all types of jobs. 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

关于node.js - 使用 bull 进行作业处理微服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58049541/

相关文章:

node.js - 单击页面上的按钮,直到全部被删除

javascript - 一对多关系袋实例是否始终需要在两端进行更新?

json - 在nodejs中从json设置模型值

node.js - 如何在提供静态内容时使用 res.sendFile() 发送参数并在 UI Controller 中访问它们?

javascript - 什么是适合 node.js 的类似 Sidekiq 的好作业系统?

python - Dask作业队列设计模式?

具有延迟事件通知的 C# 作业队列实现

bull.js - 服务器 1 如何可靠地知道服务器 2 何时完成了特定工作? (bull.js)

node.js - 尽管设置了超时,Bull.js 作业仍停滞不前