From c79b62f86945f32266e936c5a6179e2d05c7daa9 Mon Sep 17 00:00:00 2001 From: Catrope Date: Sat, 14 Jul 2012 16:07:43 -0700 Subject: [PATCH] When the main server process is killed, kill the children too Change-Id: If07a8c290d5c1d4ac977222fe8cd25ef345c4132 --- api/server.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/api/server.js b/api/server.js index 54c9609324..09de457953 100644 --- a/api/server.js +++ b/api/server.js @@ -17,10 +17,27 @@ if (cluster.isMaster) { } cluster.on('death', function(worker) { - console.log('worker ' + worker.pid + ' died, restarting.'); - // restart worker - cluster.fork(); + if(!worker.suicide) { + console.log('worker ' + worker.pid + ' died, restarting.'); + // restart worker + cluster.fork(); + } }); + process.on('SIGTERM', function() { + console.log('master shutting down, killing workers'); + for(var i = 0; i < workers.length; i++) { + console.log('Killing worker ' + i + ' with PID ' + workers[i].pid); + // disconnect() doesn't work for some reason + //workers[i].disconnect(); + workers[i].kill('SIGTERM'); + } + console.log('Done killing workers, bye'); + process.exit(1); + } ); } else { + process.on('SIGTERM', function() { + console.log('Worker shutting down'); + process.exit(1); + }); app.listen(8000); }