recluster

recluster (GitHub: doxout/recluster, License: MIT) is a
clustering library with support for zero-downtime reloading.

1
npm install recluster

Delivering that 100% uptime for web apps can be a tricky business. Errors and new code rollouts are the things we have to deal with on a daily basis and if you run continuous integration, your app is probably restarting many times a day. What kind of experience this might be creating for your users?

recluster is among a few other
libraries that try to address this issue. It is:

  • Cluster aware.
  • Zero downtime errors and deploys.
  • Does not run as daemon.
  • Log agnostic.
  • Simple, relatively easy to reason about.

Example

If server.js is your regular http server (e.g. express), create cluster.js and add:

1
2
3
4
5
6
7
8
9
10
11
12
var recluster = require('recluster'),
    path = require('path');
var cluster = recluster(path.join(__dirname, 'server.js'));
cluster.run();
process.on('SIGUSR2', function() {
    console.log('Got SIGUSR2, reloading cluster...');
    cluster.reload();
});
console.log("spawned cluster, kill -s SIGUSR2", process.pid, "to reload");

then run it

1
node cluster.js

To hot-reload the server, simply run

1
kill -s SIGUSR2 <cluster_pid>

A server worker can gracefully exit by cleaning up in the ‘close’ event of its server:

1
2
3
server.on('close', function() {
    // cleanup
});

Non-server workers can listen for the disconnect command and shut down gracefully before the kill timeout:

1
2
3
4
5
process.on('message', function(m) {
    if (m.cmd == 'disconnect') {
        // cleanup
    }
})

For some fantastic insights on the subject, checkout Towards 100% Uptime with Node.js slides by William Bert @williamjohnbert and 10 steps to nodejs nirvana in production by Qasim Zaidi @kernelhacker.

Curious, how do you manage your node process?

Post navigation

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *