superdaemon for hot-deploying net.Servers


angel.js is a simple library to gracefully restart multi process net.Servers

  • SIGHUP to graceful restart
  • SIGTERM to graceful shutdown
  • refresh modules, and graceful restart only if refresh succeeds
  • fork a new worker on accidental death
  • max_requests_per_child


var http = require('http');
var server = http.createServer(function(reqres) {
    var tick = 0;
    setInterval( function() {
        res.write( "worker["""+ (++tick) +"\n" );
    }, 1000 );
module.exports = server;


var angel = require('../angel')
, app     = require('./app');
angel( app, {
    port: 3000,                              // or unix domain socket. path: "/tmp/socket" 
    workers: 4,
    pidfile: '',
    refresh_modules_regexp: 'eg/app\\.js$',  // match against require.cache keys 
    interval: 1,                             // between new workers' start and old workers' close, in seconds 
    max_requests_per_child: 1000             // worker dies and a new worker spawns after processing x number of requests 

will output something like:

% node eg/server.js
master[20363] created pid_file:
master[20363] master will fork 4 workers
master[20363] forked worker[20364]
master[20363] forked worker[20365]
master[20363] forked worker[20366]
master[20363] forked worker[20367]
worker[20365] launched
worker[20365] listening on 3000
worker[20364] launched
worker[20364] listening on 3000
worker[20366] launched
worker[20366] listening on 3000
worker[20367] launched
worker[20367] listening on 3000

to graceful restart:

kill -HUP `cat`

after HUP you'll have stdout:

master[20363] SIGHUP
master[20363] reloaded /path/to/eg/app.js
master[20363] forked worker[20370]
master[20363] forked worker[20371]
master[20363] forked worker[20372]
master[20363] forked worker[20373]
worker[20372] launched
worker[20370] launched
worker[20372] listening on 3000
worker[20370] listening on 3000
worker[20371] launched
worker[20371] listening on 3000
worker[20373] launched
worker[20373] listening on 3000
worker[20364] closes
worker[20365] closes
master[20363] worker 20364 died
master[20363] worker 20365 died
worker[20366] closes
master[20363] worker 20366 died
worker[20367] closes
master[20363] worker 20367 died
  • merge pull requests :-)

MIT License