Hmm, on my OSX machine, it seems the behavior is roughly the same as I
recall from Windows - hitting Ctrl+C delivers SIGINT to all of the
processes, not just the master, so the cluster children are exiting
immediately because of that. If you kill just the master, then the
children presumably exit because they notice the IPC connection being
closed. If you want to change that behavior, catch the appropriate signals
and disconnect your children appropriately. Here's a modified version of
your code using the logic we use for hot-restarting our servers (master
exits immediately, to be replaced with a new one, children hang around
until they're done servicing all of their requests, children ignore SIGINT
in this case).
var cluster = require('cluster');
if(cluster.isMaster){
console.log('In Master');
for(var i = 0; i < 3 ; i++) {
cluster.fork();
}
cluster.on('online',function(worker){
console.log('Worker Live:',worker.id);
});
cluster.workers[1].send('Bazingaa');
process.on('SIGINT', function () {
for (var id in cluster.workers) {
cluster.workers[id].send('mySIGINT');
}
cluster.disconnect();
process.stdout.write('Received SIGINT, forwarded to workers', function
() {
process.exit(0);
});
});
} else {
console.log('In Worker',cluster.worker.id);
var interval = setInterval(function () {
console.log('worker ' + cluster.worker.id + ' alive');
}, 1000);
process.on('message',function(msg){
if (msg === 'mySIGINT') {
console.log('Master wants me to exit');
setTimeout(function () {
// will cause process to exit, no outstanding operations
clearInterval(interval);
}, 2500);
} else {
console.log('Message from server:', msg, cluster.worker.id);
}
});
process.on('exit', function () {
console.log('worker ' + cluster.worker.id + ' exiting');
});
process.on('SIGINT', function () {
console.log('Received SIGINT as child, ignoring');
});
}
On Thursday, August 14, 2014 8:30:43 PM UTC-7, Sam Roberts wrote:
>
> On Wed, Aug 13, 2014 at 8:53 PM, Tushar Jain <[email protected]
> <javascript:>> wrote:
> >> sorry for the ambiguity..
> >
> > I mean node example.js
> > (ctrl-c node)
>
> master is killed (assuming default SIGINT behaviour)
> ipc connections to all workers closed by OS
> all workers notice IPC connection closed
> all workers exit
>
> > node example.js
>
> master starts
> worker starts
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/273609b0-4600-4401-9a1b-921d8f001db0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.