kue icon indicating copy to clipboard operation
kue copied to clipboard

trouble getting job id immediately after a save

Open tmoens opened this issue 6 years ago • 2 comments

I have a simple node.js server. It supports the ocassional HTTP request to myserver/goDoSomethingThatTakesAWhile

I want to kue the job and pass back the job Id in the HTTP response and then let the client go wild checking for progress.

My problem is that the function that does the save and and wants to return the ID completes before the save completes and I do not get the job Id to send to the client.

let job = this.queue.create(name,dj)
      .save(function (err) {
        if (!err) {
          console.log(job.id); // ==> works as expected but the function has already completed
        } else {
          //TODO
        }
      });
    //console.log(job.id);  ==> undefined at this point ergo the problem;
 //would like to return job.id at this point, but it is not defined.

I suspect I am missing something terribly obvious here.

tmoens avatar Apr 25 '18 13:04 tmoens

You're not missing anything obvious, the problem is that the save callback is never called on success but the job is created asynchronously. There's no way to know when the job has actually been created. Which seems like a major oversight in the API design.

https://github.com/Automattic/kue/blob/87d61503d3d9cc024633efc7611bd25551f0f87d/lib/queue/job.js#L789

DylanRJohnston avatar Jun 11 '18 19:06 DylanRJohnston

Hi,

Any changes on this? I need to keep track of my job ids in case i need to cancel them (programatically) in the future, and this complicates a lot

Thank you very much

marcelothomaz avatar Jan 14 '19 21:01 marcelothomaz