Prevents hung Node.js processes
Tools like forever and PM2 are great for Node.js stability in production, but they're only effective at recovering processes that crash. They can't ensure the application is actually doing what it's supposed to do in a healthy state.
Hangman monitors a process from the inside and deliberately kills it when an unhealthy state is detected, letting the process manager kick in and restart it. Hangman is effective even in cases of a blocked event loop.
Healthy state can be monitored automatically as regular writes to stdout (which is typically how log files are written), or it can be specified in any context-specific way that would be appropriate for a particular app.
To kill a process if it is silent for over 60 seconds, all you need is:
You can specify a different timer interval:
// this process is expected to log something at least every 10srequire'hangman'10000;
To reset the timer based on actions other than stdout writes, call the function that is returned when hangman is initiated:
var resetHangman = require'hangman';// execute this function whenever the process does what it's supposed toserveron'request'// respond to the request, thenresetHangman;;
To specify what happens when the timer expires, pass in a callback. Note that this will not be effective against starved event loop situations. Hangman does not continue monitoring after the callback has been called; it is up to you to initiate new instances.
require'hangman'console.log'All quiet on the western front.';;
You can create multiple instances to monitor different things.
var hangman = require'hangman';var stdoutMonitor = hangman;var appMonitor = hangman30000// note that using console.log here would actually trip the stdoutMonitorconsole.log'The app has not checked in for 30 seconds.';
A hangman monitor instance can be canceled and resumed.
var stdoutMonitor = require'hangman';stdoutMonitorcancel;stdoutMonitorresume;