Skip to content

Instantly share code, notes, and snippets.

@nlacasse
Created February 24, 2012 21:00
Show Gist options
  • Select an option

  • Save nlacasse/1903711 to your computer and use it in GitHub Desktop.

Select an option

Save nlacasse/1903711 to your computer and use it in GitHub Desktop.
Forever A Node

Forever A Node

Say you've just had a great idea for a new web app. An app that's never been done before. An app that will improve the quality of life of millions of people. An app that will make you rich and famous.

You spend the weekend coding like crazy, and at 4:30am Monday morning, you have something that runs, barely. Most of the bad crashes are fixed, but you still see some weird bugs every once in a while. You're too sleepy to debug these issues now, but every second that your app isn't online is an opportunity for somebody else to snatch up your great idea.

So you quickly find a witty name for the app, register the domain, throw your code up on some server, fire off a tweet announcing the new breakthrough app, and fall asleep.

Two hours later, your new app is the top story on Hacker News. You're getting lots of traffic, and lots of positive feedback.

Three hours later, one of those weird bugs shows up and crashes the Node process. Traffic is still pouring in to your site, but getting "web page unavailable" errors. The comments on Hacker News go negative fast, and the reputation of your kick-ass app is sullied.

Luckily, there are tools to help avoid just this type of scenario.

Enter Forever

Forever is a simple tool that will automatically restart your application when it crashes.

Forever has some nice features that set it apart from similar tools:

  • In addition to command-line usage, Forever can be used as a NodeJS module in larger applications. This allows you to pragmatically start and stop different processes, and inspect their logs.

  • Forever is built on Node and works great with Node applications, but it can be used to restart any process. So you don't need to learn a new tool when you decide to use another language for your next project.

  • Forever can run your process as a daemon, or as a "long-running process" that outputs to the console. Forever provides an easy way to list all running applications, and to stop and restart them. I like to use daemons for production deployments, but long-running processes when I am developing so that I can see the console easily.

  • Forever manages the logs from all the application it runs, and makes it easy to tail the logs from any running application.

A Broken Clock Example

Here is a simple Node "clock server" that responds to every request with the current time.

var http = require('http');

var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end((new Date()).toString());

  if (Math.random() >= 0.99) throw "The clock is broken!";
});

server.listen(8080);

Unfortunately, this code has a nasty bug where the clock crashes after some seemingly random number of requests. (Careful readers can probably spot the bug in the above code, and may even be able to fix it, but pretend it's not quite this easy.) Broken clocks are useless, and we don't have time to debug the clock now. There are people depending on us!

So we run clock.js with forever. Here we are running it as a daemon:

forever start clock.js

Now that the app is running, we can check on its status with forever list:

info:   Forever processes running
data:       uid  command script   forever pid   logfile                          uptime
data:   [0] 2ugG node    clock.js 11001   11002 /home/nlacasse/.forever/2ugG.log 0:0:2:25.567

If we had more apps running with forever, they would be listed as well. The columns output by forever list can be configured with forever column add and forever column rm.

We can see the locations of the log files for running apps with forever logs:

info:   Logs for running Forever processes
data:       script   logfile
data:   [0] clock.js /home/nlacasse/.forever/2ugG.log

Apparently the log file is being kept in "/home/nlacasse/.forever/2ugG.log" (this location is also configurable from the command line). We could start tailing that file ourselves, but Forever makes it even easier for us. Running forever log clock.js (or equivalently forever log 0) will start tailing the log from clock.js.

info:   Showing logs for clock.js
data:
data:   /home/nlacasse/spire/blogs/forever/clock.js:14
data:     if (Math.random() >= 0.99) throw "The clock is broken!";
data:                                ^
data:   The clock is broken!
data:   warn: Forever detected script exited with code: 1
data:   warn: Forever restarting script for 1 time
data:
data:   /home/nlacasse/spire/blogs/forever/clock.js:14
data:     if (Math.random() >= 0.99) throw "The clock is broken!";
data:                                ^
data:   The clock is broken!
data:   warn: Forever detected script exited with code: 1
data:   warn: Forever restarting script for 2 time

Here we can see the bug that is causing our clock to crash, and we can also see Forever doing its job and restarting the clock. We get to see the bugs and the restarting process, but to the outside world it appears as though our clock never stopped ticking!

With Forever, you never need to worry about your killer app crashing at 4am. You can run it on any server and be confidant it will stay available. (Of course, you could also just use a PaaS provider that restarts your app for you.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment