Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the recommended way to retrieve results from completed job? #1930

Open
sfdcale opened this issue Dec 9, 2020 · 4 comments
Open

What is the recommended way to retrieve results from completed job? #1930

sfdcale opened this issue Dec 9, 2020 · 4 comments

Comments

@sfdcale
Copy link

sfdcale commented Dec 9, 2020

I am using BullMQ with express server to process jobs asynchronously but confused as how to retrieve the results from completed jobs.

What I am doing currently is to listen for job completed status event and store those results in an object with job Id as key and retrieving the results from that object whenever I need it. Is there a recommended way of doing this?

I looked at documentation but couldn't find anything about how to retrieve results.

Here is the sample code:

// 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`));
@manast
Copy link
Member

manast commented Dec 15, 2020

Please let me know if the information here is enough or we need to improve the documentation https://docs.bullmq.io/guide/returning-job-data

@sfdcale
Copy link
Author

sfdcale commented Dec 15, 2020

@manast Thank you for pointing to me that article. It gave me some pointers on how to get result from processed job.

I have one more question as how to get job status efficiently when server receives request from client about job status.

Here in the below pseudo code, I am using job.getState() method but the documentation says, don't use this method as it is not efficient.

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: job.returnvalue });
  }
});

What is the recommended approach to get job status in this situation?

Also, if you can point me to some example code on how to use results queue approach, that would be awesome.

@TemaSM
Copy link

TemaSM commented Jan 25, 2021

@manast how about to add more detailed info (like code examples) to this section?
https://docs.bullmq.io/guide/returning-job-data#using-a-results-queue
Thanks in advance 😉

@stale
Copy link

stale bot commented Jul 12, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 12, 2021
@manast manast added BETTER DOC and removed wontfix labels Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants