0

I'm trying to pass next() function to Bull job so I can handle asynchronous error handling with Express.

For some reason, it won't get forwarded.

This is is the route. data includes domain and next() which get passed to the Queue

crawler.post("/sitemap",async (req,res,next)=>{
        const {domain} = req.body
        const data = {domain,next}
        const response = await generateSitemapQueue(data)
        return res.json({message:`task has been added to the queue successfully...`,...response})
}) 

Queue simply adds data to the worker

sitemapQueue.process(createSitemapForDomain)

export const generateSitemapQueue = async (data) => {
  sitemapQueue.add(data)
}
// Here next can still be accessible in data

Not at worker, next() is undefined and cannot be called but domain is accessible. Why is that?

export const createSitemapForDomain = async (job,done)=>{
    const {domain,next} = job.data
    // next is undefined and cannot be called
}
Tajs
  • 521
  • 7
  • 18
  • 1
    You can't store functions in Redis. – robertklep Sep 11 '22 at 10:03
  • That makes so much sense. Thank you. I need to figure out how to leverage express custom error handling for asynchronous bull tasks. There is `done(new Error())` but this will only cause worker to fail. I guess this is the only option going forward. Have one try catch block in worker and throw errors via `done()` – Tajs Sep 12 '22 at 07:53
  • You might want to [edit] your question to add that clarification. – Yunnosch Sep 12 '22 at 07:58
  • @MatijaŽiberna if the job would fail and you would have been able to pass the error back to Express, you'd get an Express error anyway because you already sent back a response. Perhaps it's possible to pass back a job id to the frontend and periodically check the status of that job (and notify the user if it failed). – robertklep Sep 12 '22 at 10:17

0 Answers0