indexzero
10/9/2011 - 12:59 AM

A simple hook to restart a process if it is hung (i.e. non-responsive)

A simple hook to restart a process if it is hung (i.e. non-responsive)

var forever = require('forever'),
    request = require('request');

function checkIsHung (monitor) {
  //
  // Do something to check if your process is hung
  // e.g. Make an http request
  //
  request({ uri: 'http://yourapp.com' }, function (err, res, body) {
    if (err) {
      monitor.restart();
    }
  });
};

function hungHook (monitor) {
  monitor.on('start', function () {
    //
    // Check if your process is hung every 30 seconds
    //
    setInterval(checkIsHung.bind(monitor), 30000);
  });
}

forever.start('your-script.js', {
  //
  // Include other forever options here
  // see README.md for details of all
  // options
  //
  hooks: [hungHook]
});