0

Im running a heroku app with a background worker processing jobs with Kue. Because heroku terminates and restarts any request which takes more than 30 seconds, I ended up sending a response back to client on jobs.create() (before it is finished).

Since some of these jobs take up to a few minutes to complete, what would be the best way to check the progress from the client?

So far the best solution I can see is to send the job id back to the client and then check the job progress every x seconds:

var job = jobs.create(type, data).save(function () {
   res.send(200, job.id);
});
...
kue.Job.get(id, function (err, job) {
   res.send({ progress: job._progress, state: job._state })
}
lifwanian
  • 644
  • 1
  • 7
  • 19

1 Answers1

0

Anytime you have a job that might take > 30 seconds to complete, you should probably poll instead of relying on a really long request to not be canceled or broken somewhere. Polling is also more resilient to clients who refresh (likely, if your job takes so long):

https://devcenter.heroku.com/articles/asynchronous-web-worker-model-using-rabbitmq-in-node#4-poll-for-changes

this.pollInterval = setInterval(poll.bind(this), 2000);
hunterloftis
  • 13,386
  • 5
  • 48
  • 50