HTML5 Zone is brought to you in partnership with:

I'm software developer and microISV, with a lot of passion to technologies.. having a fun writing code, writing blog posts and giving public speeches. For about 10 years of total experince I've been working in many branches of software development, including management and QA. I try to follow and adopt agile practices and signed up for software craftsmenship manifesto. Alexander is a DZone MVB and is not an employee of DZone and has posted 67 posts at DZone. You can read more from them at their website. View Full User Profile

How to Keep Node.js Processes Running

01.31.2014
| 11667 views |
  • submit to reddit

Node.js/Express.js is great for Web API’s and applications. In contrast to known enterprise technologies, Node.js is very special. It’s a single process/threaded environment. If an unhanded exception occurs, the Node.js virtual machine simply stops, leaving the application in an unresponsive state.

Due to the async nature of Node.js, try/catch doesn't always work, even with domains and stuff you have a chance that the application crashes on production while you sleep.

To mitigate the issue, there are few known solutions. The common idea is that there is a watchdog keeping an eye on node processes and if crashed, it restarts the application again.

Recently I’ve used a great library by @mafintosh called respawn. I liked its minimalistic style and decided to try it out.

The bare-bones code is very simple. Without modification of your application, just create file monitor.js with the following code:

var respawn = require('respawn');

var monitor = respawn(['node', 'server.js'], {
    env: {ENV_VAR:'test'}, // set env vars
    cwd: '.',              // set cwd
    maxRestarts:10,        // how many restarts are allowed within 60s
    sleep:1000,            // time to sleep between restarts
});

monitor.start(); // spawn and watch

monitor will spawn new node process and in case of crash it will be restarted. You can also specify maxRestars (I recommend to do that, if something is really bad it won’t be restarted infinitely) and sleep time.

I’ve tried that, by implementing /fail end-point in my app, to see that respawn really works.

app.get('/fail', function (req, res, next) {
  setTimeout(function () {
      var nu = null;
      nu.access();

      res.send('Hello World');
  }, 1000);
});

if I try to hit /fail I’ll see no results in browser, but if I go back to / the application is running in normal state.

But simple respawning of the application is not a complete solution. You need to know what exactly happened to be able to fix issue. Proper logging of your application is essential. I’ll show my small setup around respawn that sends critical message to Logentries, so all crashes are logged.

var respawn = require('respawn');
var util = require('util');
var logger = require('./source/utils/logger');

var proc = respawn(['node', 'app.js'], {
  cwd: '.',
  maxRestarts: 10,
  sleep: 1000,
});

proc.on('spawn', function () {
  util.print('application monitor started...');
});

proc.on('exit', function (code, signal) {
  logger.fatal({msg: 'process exited, code: ' + code + ' signal: ' + signal});
});

proc.on('stdout', function (data) {
  util.print(data.toString());
});

proc.on('stderr', function (data) {
  logger.error({msg: 'process error', data: data.toString()});
});

proc.start();

(Details of logger you can find in this post).

All process output goes to stdout, which is convinient for development, but in case of stderr or exit everything is logged to cloud and notification to dev-team sent.

It worked really nice, now I’m not worried even if something bad happens on production. respawn will make sure that the rest of users are not affected. As a developer, you can much quicker find bugs and push hotfixes.

Published at DZone with permission of Alexander Beletsky, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

darryl west replied on Fri, 2014/01/31 - 12:05pm

node clusters, when fully available will probably be the best choice for keeping services alive and healthy.    Until then, respawn with multiple instances seem like a safe solution.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.