Skip to content

Commit

Permalink
(god)(stopProcessId) refactor: now it only kill process without disco…
Browse files Browse the repository at this point in the history
…nnecting in cluster mode
  • Loading branch information
Unitech committed Oct 29, 2016
1 parent 81cc08f commit d0a3f49
Showing 1 changed file with 27 additions and 101 deletions.
128 changes: 27 additions & 101 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d0a3f49

Please sign in to comment.