11

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?

Rogerio Chaves
  • 2,719
  • 2
  • 25
  • 24
  • Did you happen to understand what is the reason? – pars Feb 25 '12 at 12:38
  • 1
    No, but i removed forever, its no need on heroku, you should avoid the errors so your app continues to run – Rogerio Chaves Mar 05 '12 at 01:25
  • 2
    so does heroku automatically restart your app when it crashes now? – hellatan Mar 10 '12 at 22:03
  • 1
    from my understanding of heroku the processes should restart. – Piotr Tomasik May 25 '12 at 10:48
  • 3
    From their docs (https://devcenter.heroku.com/articles/ps#process-restarts): In the normal case of a long-running web or worker process getting an occasional crash, it will be restarted instantly without any intervention on your part. If your process crashes twice in a row, it will stay down for ten minutes before the system retries. – Matthew F. Robben Jan 14 '13 at 16:41
  • You may incur additional charges if you start multiple processes on Heroku. – Jacob Groundwater Feb 02 '13 at 07:23

1 Answers1

10

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

Nathan Friedly
  • 7,837
  • 3
  • 42
  • 59
  • 1
    You're right that Heroku does this, only for free single-dyno apps though. The paid tiers always stay up. – Jed Watson Nov 12 '12 at 14:13
  • 1
    you can use monitoring service which will keep pinging your server forever and thus keep it aliveeee – Muhammad Umer Mar 27 '15 at 03:39
  • @MuhammadUmer: keep free apps alive in this way and Heroku will end up removing free apps altogether: if you want it up always, it's got to be worth $7 a month – ChrisV Jul 16 '15 at 20:38
  • @ChrisV: they basically did remove free apps altogether: you can now only have a free server online for a maximum of 18 hours per day - so your can't have a free app stay online for any reasonable amount of traffic. https://blog.heroku.com/archives/2015/5/7/new-dyno-types-public-beta – Nathan Friedly Jul 17 '15 at 00:24