gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:50 26 4
gpt4 key购买 nike

我想使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com