From 256da4822c994c4c7d8c1fc09d5a5e4333df2a76 Mon Sep 17 00:00:00 2001 From: Unitech Date: Mon, 7 Dec 2015 16:23:26 +0100 Subject: [PATCH] Fix cluster kill signal + avoid writing on std output when proc disconnected #1819 --- CHANGELOG.md | 2 ++ examples/beforeExit.js | 32 ++++++++++++++------------------ examples/child.js | 12 +----------- lib/God.js | 10 +++++----- lib/God/Methods.js | 7 ++++--- lib/ProcessContainer.js | 6 ++---- 6 files changed, 28 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3131ce6..d364cfb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### 0.15.11 +- Send SIGINT signal to process instead of SIGTERM [#1819] +- Avoid writing on std err/out when process is disconnected [#1819] - Upgrade pm2-axon, cron, should, mocha, coffee-script, chokidar, semver NPM packages - Show process configuration option when describing process - Add --no-automation flag diff --git a/examples/beforeExit.js b/examples/beforeExit.js index 5749aa77..3d1fe786 100644 --- a/examples/beforeExit.js +++ b/examples/beforeExit.js @@ -1,25 +1,21 @@ -'use strict'; -let stopped = false; +var stopped = false; function work() { - console.log('working'); - !stopped && setTimeout(work, 200); - } - -function stop() { - stopped = true; - console.log('shutting down', stopped); + console.log('working'); + !stopped && setTimeout(work, 1000); } -process.once('SIGINT', function() { - console.log('SIGINT'); - stop(); -}); // CTRL-C -process.once('SIGTERM', function() { - console.log('SIGTERM'); - stop(); -}); // pm2 stop -process.on('beforeExit', () => console.log('exited cleanly :)')); +function stop() { + console.log('shutting down'); + stopped = true; +} + +process.once('SIGINT', stop); // CTRL-C + +process.on('beforeExit', function() { + console.log('exited cleanly :)'); + process.exit(0); +}); work(); diff --git a/examples/child.js b/examples/child.js index 56840d4a..3fe0657b 100644 --- a/examples/child.js +++ b/examples/child.js @@ -4,17 +4,7 @@ require('pmx').init({ var http = require('http'); -http.createServer(function(req, res) { +var server = http.createServer(function(req, res) { res.writeHead(200); res.end('hey'); }).listen(8000); - -process.on('message', function(msg) { - if (msg == 'shutdown') { - console.log('Closing all connections...'); - setTimeout(function() { - console.log('Finished closing connections'); - process.exit(0); - }, 500); - } -}); diff --git a/lib/God.js b/lib/God.js index 39fb349e..9339c5a6 100644 --- a/lib/God.js +++ b/lib/God.js @@ -175,7 +175,7 @@ God.executeApp = function executeApp(env, cb) { }); clu.once('exit', function cluExit(code, signal) { - God.handleExit(clu, signal || code); + God.handleExit(clu, code || 0, signal || 'SIGINT'); }); clu.once('online', function cluOnline() { @@ -219,11 +219,11 @@ God.executeApp = function executeApp(env, cb) { if (clu.connected === true) clu.disconnect && clu.disconnect(); clu._reloadLogs = null; - return God.handleExit(proc, signal || code); + return God.handleExit(proc, code || 0, signal); }); if (proc.pm2_env.vizion !== false && proc.pm2_env.vizion !== "false") - God.finalizeProcedure(proc) + God.finalizeProcedure(proc); else God.notify('online', proc); @@ -242,8 +242,8 @@ God.executeApp = function executeApp(env, cb) { * @param {} exit_code * @return */ -God.handleExit = function handleExit(clu, exit_code) { - console.log('App name:%s id:%s exited with code %s', clu.pm2_env.name, clu.pm2_env.pm_id, exit_code); +God.handleExit = function handleExit(clu, exit_code, kill_signal) { + console.log('App [%s] with id [%s] and pid [%s], exited with code [%s] via signal [%s]', clu.pm2_env.name, clu.pm2_env.pm_id, clu.process.pid, exit_code, kill_signal); var proc = this.clusters_db[clu.pm2_env.pm_id]; diff --git a/lib/God/Methods.js b/lib/God/Methods.js index 2bada3a8..e8865e84 100644 --- a/lib/God/Methods.js +++ b/lib/God/Methods.js @@ -211,7 +211,7 @@ module.exports = function(God) { var timer = setInterval(function() { if (God.checkProcess(pid) === false) { - console.log('Process with pid %d killed', pid); + debug('Process with pid %d killed', pid); clearTimeout(timeout); clearInterval(timer); return cb(null, true); @@ -254,8 +254,9 @@ module.exports = function(God) { if(pm2_env.treekill !== true) process.kill(parseInt(pid)); else { - if (mode.indexOf('cluster') === 0) - treekill(parseInt(pid)); + if (mode.indexOf('cluster') === 0) { + treekill(parseInt(pid), 'SIGINT'); + } else crossPlatformGroupKill(parseInt(pid), 'SIGINT'); } diff --git a/lib/ProcessContainer.js b/lib/ProcessContainer.js index 8d280ba4..0ce88ee4 100644 --- a/lib/ProcessContainer.js +++ b/lib/ProcessContainer.js @@ -36,11 +36,9 @@ delete process.env.pm2_env; var cronRestart = pm2_env.cron_restart; var original_send = process.send; + process.send = function() { - if (process.connected) - original_send.apply(this, arguments); - else - console.error('IPC channel with master closed'); + if (process.connected) original_send.apply(this, arguments); }; if (cst.MODIFY_REQUIRE)