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

Handle results from promises from seprate processors #2220

Closed
shamonshan opened this issue Nov 25, 2021 · 10 comments
Closed

Handle results from promises from seprate processors #2220

shamonshan opened this issue Nov 25, 2021 · 10 comments

Comments

@shamonshan
Copy link

Description

I am trying to implement a queue as a separate processor

imageProcessQueue.process(__dirname+'/process-queue.js');

In process-queue.js i m handling

module.exports = async function (job) {

  //some jobs 
  return Promise.resolve({status:"completed"});
}

But on the queue completed event I can't read the results from the promise which I return from the processor

imageProcessQueue.on('completed', function (job, result) {
  // can see this output
  console.log('completed')
  //never logs anything always empty 
 //should log  {status:"completed"}
  console.log(result)
})

Bull version :4.1.1

@manast
Copy link
Member

manast commented Nov 25, 2021

Strange, we have a test case that cover this: https://github.com/OptimalBits/bull/blob/develop/test/test_sandboxed_process.js#L38

@shamonshan
Copy link
Author

But if i return a normal statement ie return {status:"completed"} that i can see in the completed event

@manast
Copy link
Member

manast commented Nov 25, 2021

Since you are using "async" already, then what you are returning is a promise not the value of the promise.

@shamonshan
Copy link
Author

Ahh shit, I forgot about the async keyword, now it getting the expected result.
Thanks for the point, One more question is it necessary to do a resolve or reject to mark the job as complete??

@manast
Copy link
Member

manast commented Nov 25, 2021

Not needed.

@manast
Copy link
Member

manast commented Nov 25, 2021

I mean, you need to return a promise, either using async or a promise from a promise chain or whatever.

@shamonshan
Copy link
Author

module.exports = function (job) {
    ProcessImage(job.data,(err,result)=>{
                 if(result){
                    return Promise.resolve({ status:"success" });
                 }
            });
    }
}

But the above implementation the completed event is not giving any data

@manast
Copy link
Member

manast commented Nov 25, 2021

Because you are not returning any promise. The promise that you are returning is for the callback in ProcessImage...

@manast
Copy link
Member

manast commented Nov 25, 2021

The code should be like this:

module.exports = function (job) {
  return new Promise((resolve, reject) => {
    ProcessImage(job.data, (err, result) => {
      if (err) {
        reject(err);
      } else {
        resolve({ status: "success" });
      }
    });
  });
};

@shamonshan
Copy link
Author

Thanks now its working

@manast manast closed this as completed Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants