Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS with forever on Heroku

Tags:

node.js

heroku

So, I need to run my node.js app on heroku, it works very well, but when my app crashes, i need something to restart it, so i added forever to package.json, and created a file named forever.js with this:

var forever = require('forever');

var child = new (forever.Monitor)('web.js', {
  max: 3,
  silent: false,
  options: []
});

//child.on('exit', this.callback);
child.start();

forever.startServer(child);

on my Procfile (that heroku uses to know what to start) i put:

web: node forever.js

alright! Now everytime my app crashes it auto restarts, but, from time to time (almost every 1 hour), heroku starts throwing H99 - Platform error, and about this error, they say:

Unlike all of the other errors which will require action from you to correct, this one does not require action from you. Try again in a minute, or check the status site.

But I just manually restart my app and the error goes away, if I don't do that, it may take hours to go away by itself.

Can anyone help me here? Maybe this is a forever problem? A heroku issue?

like image 377
Rogerio Chaves Avatar asked Dec 28 '11 14:12

Rogerio Chaves


People also ask

How do I use Heroku forever?

There are two ways to use forever: through the command line or by using forever in your code. Note: If you are using forever programatically you should install forever-monitor.

Does Heroku support node js?

Heroku Node. js support will only be applied when the application has a package. json file in the root directory.

Why do you use forever with node js?

The purpose of Forever is to keep a child process (such as your node. js web server) running continuously and automatically restart it when it exits unexpectedly.

Does Heroku support node 16?

js release schedule below, Heroku's currently supported Node. js versions are 14. x , 16. x , and 18.


1 Answers

This is an issue with free Heroku accounts: Heroku automatically kills unpaid apps after 1 hour of inactivity, and then spins them back up the next time a request comes in. (As mentioned below, this does not apply to paid accounts. If you scale up to two servers and pay for the second one, you get two always-on servers.) - https://devcenter.heroku.com/articles/dynos#dyno-sleeping

This behavior is probably not playing nicely with forever. To confirm this, run heroku logs and look for the lines "Idling" and " Stopping process with SIGTERM" and then see what comes next.

Instead of using forever, you might want to try the using the Cluster API and automatically create a new child each time one dies. http://nodejs.org/api/cluster.html#cluster_cluster is a good example, you'd just put your code into the else block.

The upshot is that your app is now much more stable, plus it gets to use all of the available CPU cores (4 in my experience).

The downside is that you cannot store any state in memory. If you need to store sessions or something along those lines, try out the free Redis To Go addon (heroku addons:add redistogo).

Here's an example that's currently running on heroku using cluster and Redis To Go: https://github.com/nfriedly/node-unblocker

UPDATE: Heroku has recently made some major changes to how free apps work, and the big one is they can only be online for a maximum of 18 hours per day, making it effectively unusable as a "real" web server. Details at https://blog.heroku.com/archives/2015/5/7/heroku-free-dynos

UPDATE 2: They changed it again. Now, if you verify your ID, you can run 1 free dyno constantly: https://blog.heroku.com/announcing_heroku_free_ssl_beta_and_flexible_dyno_hours#flexible-free-dyno-hours

like image 52
Nathan Friedly Avatar answered Nov 16 '22 02:11

Nathan Friedly