Skip to content

Instantly share code, notes, and snippets.

@ritch
Created December 28, 2011 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/1525799 to your computer and use it in GitHub Desktop.
Save ritch/1525799 to your computer and use it in GitHub Desktop.
Ring
// start node.js processes on a distributed network
// makes sure the process is always available, even if
// it has to restart the process on another server
// essentially a distributed version of forever
// no client/server relationship, all ring nodes are peers
var ring = require('ring').join('network guid')
, opts = {
distribution: {
min: 3,
max: 10
}
};
// - tell ring you want a process up no matter what
// - nodes on the ring keep eachother up to date
ring.start('my guid', opts, function(err, proc) {
// stdout is available over
// the network if needed
proc.stdout.on('data', function (data) {
process.stdout.write(data);
});
ring.list(function (err, processes) {
// list all processes in
// the entire ring
console.log(processes);
});
});
// - all events come across the network
// - death can be due to a fault or failure
// to trigger a heartbeat event
ring.on('death', function (err, proc) {
// by default ring will try to restart on the same machine,
// if the proc cannot be brought up it will try another machine
// until this callback gets an 'err' that the entire ring is down
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment