KGo
KGo

Reputation: 20084

Background jobs that run on every request on Heroku and node.js

I have an app that needs to run a very long process (takes 30-60 seconds for each request). After the processing, the result is then returned to the request as a response. This works fine locally, but it crashes my Heroku instance.

What I'd like to happen instead is:

I am not sure what all I need for this. Based on an hour-long research, it seems like I can use Redis as a queue and a worker can poll it every x minutes. But what I can't understand is how to figure out which request to send the response to after processing ends.

Is there a sample Express/node.js for this? Any pointers are helpful.

Upvotes: 5

Views: 2654

Answers (2)

Jai Pandya
Jai Pandya

Reputation: 2329

Kue (from @mattetre's answer) is not maintained anymore. Kue's GitHub page suggests Bull as a good alternative. It is a fast and reliable Redis based queue for Node.js.

Upvotes: 1

mattetre
mattetre

Reputation: 390

Like you found in your research, setting up a worker queue using Redis is a good approach for long running processes. A nice library for this is kue (https://github.com/learnboost/kue).

When it comes to responding to a request with the results of the job, having an outanding requesting hanging waiting for a response is not a good way to go about it (and may not work, heroku kills requests that have been idle for a certain period of time).

What you could do is when the request is made start the background job and respond to the request right away with job ID. The client can then poll the server for the status of the job, when the job is complete it can then fetch the needed result.

Upvotes: 2

Related Questions