From d0a3f4902e3c0808de6c05a20ad32c27fd2abea7 Mon Sep 17 00:00:00 2001 From: Unitech Date: Sat, 29 Oct 2016 17:27:24 +0200 Subject: [PATCH] (god)(stopProcessId) refactor: now it only kill process without disconnecting in cluster mode --- lib/God/ActionMethods.js | 128 +++++++++------------------------------ 1 file changed, 27 insertions(+), 101 deletions(-) diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index fd3abe1b0..0281cf1bd 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -236,129 +236,55 @@ module.exports = function(God) { * @return Literal */ God.stopProcessId = function(id, cb) { - if(typeof id == 'object' && 'id' in id) + if (typeof id == 'object' && 'id' in id) id = id.id; if (!(id in God.clusters_db)) return cb(God.logAndGenerateError(id + ' : id unknown'), {}); if (God.clusters_db[id].pm2_env.status == cst.STOPPED_STATUS) return cb(null, God.getFormatedProcess(id)); - - if (God.clusters_db[id].state && God.clusters_db[id].state === 'none') { - setTimeout(function() { - God.stopProcessId(id, cb); - }, 250); - return false; - } + // state == 'none' means that the process is not online yet + if (God.clusters_db[id].state && God.clusters_db[id].state === 'none') + return setTimeout(function() { God.stopProcessId(id, cb); }, 250); var proc = God.clusters_db[id]; - var timeout = null; - var timeout2 = null; console.log('Stopping app:%s id:%s', proc.pm2_env.name, proc.pm2_env.pm_id); proc.pm2_env.status = cst.STOPPING_STATUS; - proc.pm2_env.vizion_running = false; if (!proc.process.pid) { + console.error('app=%s id=%d does not have a pid', proc.pm2_env.name, proc.pm2_env.pm_id); proc.pm2_env.status = cst.STOPPED_STATUS; - return cb(null, []); + return cb(null, { error : true, message : 'could not kill process w/o pid'}); } - var kill_anyway = function(proc) { - if (proc && proc.process && proc.process.pid) { - God.killProcess(proc.process.pid, proc.pm2_env, function(err) { - proc.pm2_env.status = cst.STOPPED_STATUS; - - if (err && err.type && err.type === 'timeout') { - proc.removeAllListeners && proc.removeAllListeners(); - proc.pm2_env.status = cst.ERRORED_STATUS; - } else { - pidusage.unmonitor(proc.process.pid); - proc.process.pid = 0; - if (proc.pm2_env.axm_actions) proc.pm2_env.axm_actions = []; - if (proc.pm2_env.axm_monitor) proc.pm2_env.axm_monitor = {}; - if (proc.pm2_env.pm_id.toString().indexOf('_old_') !== 0) { - try { - fs.unlinkSync(proc.pm2_env.pm_pid_path); - } catch (e) {} - } - - God.notify('exit', proc); - } + God.killProcess(proc.process.pid, proc.pm2_env, function(err) { + proc.pm2_env.status = cst.STOPPED_STATUS; + pidusage.unmonitor(proc.process.pid); - return cb(null, God.getFormatedProcess(id)); - }); - } - else { - console.error('[stopProcessId] Could not kill process with pid 0'); + God.notify('exit', proc); + + if (err && err.type && err.type === 'timeout') { + console.error('app=%s id=%d pid=%s could not be stopped', + proc.pm2_env.name, + proc.pm2_env.pm_id, + proc.process.pid); + proc.pm2_env.status = cst.ERRORED_STATUS; return cb(null, God.getFormatedProcess(id)); } - }; - var exitedAfterDisconnect = false; + if (proc.pm2_env.pm_id.toString().indexOf('_old_') !== 0) { + try { + fs.unlinkSync(proc.pm2_env.pm_pid_path); + } catch (e) {} + } - if (semver.lt(process.version, '7.0.0') === true) { - exitedAfterDisconnect = proc.suicide; - } else { - exitedAfterDisconnect = proc.exitedAfterDisconnect; - } + if (proc.pm2_env.axm_actions) proc.pm2_env.axm_actions = []; + if (proc.pm2_env.axm_monitor) proc.pm2_env.axm_monitor = {}; - /** - * Procedure to stop a process in cluster mode - */ - if (proc.pm2_env.exec_mode == 'cluster_mode' && - proc.state != 'disconnected' && - proc.state != 'dead' && - exitedAfterDisconnect != true) { - - var onDisconnect = function() { - clearTimeout(timeout2); - clearTimeout(timeout); - kill_anyway(proc); - }; - - proc.once('disconnect', onDisconnect); - - timeout = setTimeout(function() { - // Fallback 2 because disconnect didnt happened - proc.removeListener('disconnect', onDisconnect); - kill_anyway(proc); - }, 800); - - timeout2 = setTimeout(function() { - try { - if (proc.process.connected === true && proc.state !== 'disconnected' - && proc.process.signalCode == null && proc.process.exitCode == null) { - proc.disconnect && proc.disconnect(); - } - else { - console.log('Process %s has already been disconnected', proc.pm2_env.pm_id); - kill_anyway(proc); - } - } catch (e) { - // Fallback on disconnect method fail - clearTimeout(timeout); - console.log('Could not disconnect process', e.stack || e); - proc.removeListener('disconnect', onDisconnect); - if (proc && proc.process && proc.process.pid) - kill_anyway(proc); - else { - process.nextTick(function() { - proc.pm2_env.status = cst.STOPPED_STATUS; - return cb(null, God.getFormatedProcess(id)); - }); - } - } - }, 50); - return false; - } - else { - /** - * Procedure to stop a process in fork mode - */ - kill_anyway(proc); - } - return false; + proc.process.pid = 0; + return cb(null, God.getFormatedProcess(id)); + }); }; God.resetMetaProcessId = function(id, cb) {