Thanks for the suggestion, I'm running this code now. 

var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
    var worker,
        workers = {};

    var killWorkers = function(reason){
        return function(reason) {
            console.log('Killing because we received ' + reason);
            _.each(workers, function(w){
                w.kill();
                console.log('Killed worker ' + w.pid);
            });
            console.log('Shutting down master process');
            process.exit(1);
        };
    };

    // Fork workers.
    console.log(numCPUs + ' CPUs detected.');
    for(var i = 0; i < numCPUs; i++) {
        worker = cluster.fork();
        workers[worker.pid] = worker;
    }

    process.on('uncaughtException', killWorkers('uncaughtException'));
    process.on('exit', killWorkers('exit'));

    cluster.on('death', function(worker) {
        cluster.fork();
        console.log('Worker ' + worker.pid + ' died.');
    });
} else {
    var port = process.argv[2] || process.env.NODE_PORT || config.port;
    console.log( 'Worker process listening on ' + port );
    app.listen( port );
}


On Thursday, May 10, 2012 12:12:15 AM UTC-7, Kevin Liu wrote:
>
> I'm using core cluster in node 0.6.17. Is there a way to get worker 
> processes to die if the master process dies?
>
> The specific problem: my production server is daemonized using forever. It 
> appears that the master process occasionally crashes. Forever tries to 
> bring the server up and now I have orphaned worker processes, a new master, 
> and new associated worker processes that presumably cannot bind to the port 
> in use.
>
> I saw a pull request to patch this in 0.7+, but I was hoping there was a 
> way to accomodate this case in the meantime.
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to