Node.js Bull JS和在Heroku上运行不同的工作人员
在NodeJS、Heroku应用程序上实现不同的worker时,我正在寻找如何构建我的应用程序的建议 我的路由器打开了两个端点。一个接收Node.js Bull JS和在Heroku上运行不同的工作人员,node.js,heroku,service-worker,Node.js,Heroku,Service Worker,在NodeJS、Heroku应用程序上实现不同的worker时,我正在寻找如何构建我的应用程序的建议 我的路由器打开了两个端点。一个接收GET请求,另一个接收带有某些正文内容的POST请求 const Queue = require("bull"); // Create / Connect to a naned worked queue let workQueue = new Queue("work", client); router.post(&qu
GET
请求,另一个接收带有某些正文内容的POST
请求
const Queue = require("bull");
// Create / Connect to a naned worked queue
let workQueue = new Queue("work", client);
router.post("/job/get-data/:id", ensureAuth, async (req, res) => {
let job = await workQueue.add({
filename: req.params.id,
arguments: req.body.data,
});
// Does not redirect if the POST request is made using Javascript
res.json({ id: job.id });
});
router.post("/job/perform-action/:id", ensureAuth, async (req, res) => {
let job = await workQueue.add({ id: req.params.id, type: req.body.type });
res.json({ id: job.id });
});
现在,我对两个请求使用相同的队列。这是我的worker.js
:
const client = require("../config/redis");
let throng = require("throng");
let Queue = require("bull");
let workers = process.env.WEB_CONCURRENCY || 1;
let maxJobsPerWorker = 50;
let workQueue = new Queue("work", client);
function start() {
workQueue.process(maxJobsPerWorker, __dirname + "/processor.js");
}
throng({ workers, start })
最后是processor.js文件:
const db = require("../config/db");
const scrapperController = require("../controller/scrapperController");
const awsController = require("../controller/awsController");
module.exports = async function (job) {
// We need to restart the Mongoose process here so it knows what database to search
db();
try {
let scrappedData = null;
if (job.data.arguments) {
// With argument, wants to get data
scrappedData = await scrapperController.getData(
job.data.filename,
job.data.arguments
);
await awsController.uploadFile(job.data.filename, scrappedData);
return { value: "Success" };
} else {
// With no arguments, whats to perform action
scrappedData = await scrapperController.performAction(job.data.id);
return { value: csvFileName };
}
} catch (error) {
return Promise.reject("Unable to scrape data", error);
}
};
有谁知道如何将这两个端点分给不同的工人吗?也许一个工人会被称为“获取信息”,另一个工人会被称为“执行操作”
我的想法是创建不同的文件,这些文件将引用不同的工作者并链接到不同的处理器文件
我的另一个想法是完全丢失processor.js
文件,创建执行不同操作的sperate worker文件
我对工人以及他们是如何“工作”的都很陌生,所以我很难将我的思想集中在他们身上
对上述代码的任何其他改进都是非常受欢迎的
谢谢大家!