Skip to content

Instantly share code, notes, and snippets.

@AndreasMadsen
Created April 12, 2012 12:18
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 AndreasMadsen/2366873 to your computer and use it in GitHub Desktop.
Save AndreasMadsen/2366873 to your computer and use it in GitHub Desktop.
immortal example
var net = require('net');
var immortal = require('immortal');
var spawn = function(id, callback) {
var socket = net.connect('/tmp/monitor.sock');
console.log(id, 'start');
socket.on('error', function(error) {
if (error.code !== 'ENOENT') throw error;
immortal.start(__dirname+'/worker.js', {
strategy: 'unattached',
auto:false,
monitor: null
}, function(error) {
if (error) throw error;
console.log('immortal process spawned');
// error === null
// Der er derfor ingen grund til at lave et respawn
// Desuden er der kun når forkerte options er givet i immortal.start(name, options) at
// error ikke vil være null.
//setTimeout(spawn.bind(null, id, callback), 100);
});
});
socket.on('connect', function() {
console.log(id, 'connect');
callback(null, socket);
});
};
for (var i = 0 ; i < 5; i++) {
spawn(i, function() {
console.log('spawned...');
});
}
var net = require('net');
var server = net.createServer(function(socket) {
// do some stuff
});
server.on('error', function() {
process.exit(0); // only listen in one of the daemons
});
server.listen('/tmp/monitor.sock');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment