首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从BullMQ已完成作业中检索结果的推荐方法是什么?

从BullMQ已完成作业中检索结果的推荐方法是什么?
EN

Stack Overflow用户
提问于 2020-12-09 22:08:46
回答 1查看 2.5K关注 0票数 2

我正在使用BullMQ和快递服务器异步处理作业,但不知道如何从已完成的作业中检索结果。

我目前要做的是侦听作业完成状态事件,并将这些结果存储在一个以作业Id作为键的对象中,并在需要时从该对象检索结果。有推荐的方法吗?

我查看了BullMQ文档,但没有找到任何关于如何检索结果的信息。

下面是示例代码:

server.js

代码语言:javascript
运行
复制
// Kick off a new job by adding it to the work queue
app.post("/api/submitjob", async (req, res) => {
  let job = await workQueue.add();
  res.json({ id: job.id });
});

app.get("/api/jobstatus/:id", async (req, res) => {
  let id = req.params.id;
  let job = await workQueue.getJob(id);

  if (job === null) {
    res.status(404).end();
  } else {
    let state = await job.getState();
    let reason = job.failedReason;
    res.json({ id, state, progress, reason, result: jobIdResultMap[id] });
  }
});

// You can listen to global events to get notified when jobs are processed
workQueue.on('global:completed', (jobId, result) => {
  logger.log('info', `${jobId} succesfully completed`);
  jobIdResultMap[jobId] = JSON.parse(result);
});

app.listen(PORT, () => console.log(`✅  API Server started: http://${HOST}:${PORT}/api/v1/endpoint`));

worker.js:

代码语言:javascript
运行
复制
let throng = require("throng");
let Queue = require("bull");

// Connect to a local redis instance locally, and the Heroku-provided URL in production
let REDIS_URL = process.env.REDIS_URL || "redis://127.0.0.1:6379";

// Spin up multiple processes to handle jobs to take advantage of more CPU cores
// See: https://devcenter.heroku.com/articles/node-concurrency for more info
let workers = process.env.WEB_CONCURRENCY || 2;

// The maximum number of jobs each worker should process at once. This will need
// to be tuned for your application. If each job is mostly waiting on network
// responses it can be much higher. If each job is CPU-intensive, it might need
// to be much lower.
let maxJobsPerWorker = 50;

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

function start() {
  // Connect to the named work queue
  let workQueue = new Queue("work", REDIS_URL);

  workQueue.process(maxJobsPerWorker, async (job) => {
    // This is an example job that just slowly reports on progress
    // while doing no work. Replace this with your own job logic.
    let progress = 0;

    await sleep(50);

    // A job can return values that will be stored in Redis as JSON
    // This return value is unused in this demo application.
    return { value: "This will be stored" };
  });
}

// Initialize the clustered worker process
// See: https://devcenter.heroku.com/articles/node-concurrency for more info
throng({ workers, start });
EN

回答 1

Stack Overflow用户

发布于 2021-01-14 12:50:06

建议的方法是使用作业队列和消息队列。

在撰写本文时,BullMQ的文档是不完整的,因此您应该查看Bull中的文档。

从文件上看-

返回作业完成情况

一种常见的模式是,您有一组队列处理器,它们可以尽可能快地处理作业,还有一些其他服务需要获取这些处理器的结果并对其进行处理,可能会将结果存储在数据库中。

实现这一目标的最健壮和可伸缩的方法是将标准作业队列与消息队列模式相结合:服务只通过打开作业队列并向集群添加作业就将作业发送到集群,集群将尽快开始处理。每次在集群中完成作业时,都会向带有结果数据的结果消息队列发送消息,而将结果存储在数据库中的其他服务会侦听此队列。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65225648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档