Automattic / kue

Kue is a priority job queue backed by redis, built for node.js.
http://automattic.github.io/kue
MIT License
9.46k stars 867 forks source link

trouble getting job id immediately after a save #1177

Open tmoens opened 6 years ago

tmoens commented 6 years ago

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.

DylanRJohnston commented 6 years ago

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

marcelothomaz commented 5 years ago

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