What is the recommended way to retrieve results from completed job?
Created by: sfdcale
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`));