enhacement: remove deprecated calls / adapt tests / return acted process deleteProcessId/stopProcessId

This commit is contained in:
Unitech 2016-03-21 18:02:51 +01:00
parent 98c4e6e012
commit c3ea256c2e
8 changed files with 87 additions and 250 deletions

View File

@ -1,3 +1,4 @@
var pmx = require('pmx').init({
http : true
});

View File

@ -1176,13 +1176,8 @@ CLI.delete = function(process_name, jsonVia, cb) {
return CLI.actionFromJson('deleteProcessId', process_name, commander, 'pipe', cb);
if (process_name.indexOf('.json') > 0)
return CLI.actionFromJson('deleteProcessId', process_name, commander, 'file', cb);
else {
CLI._delete(process_name, cb);
}
}
CLI._delete = function(process_name, cb) {
CLI._operate('deleteProcessId', process_name, cb);
else
CLI._operate('deleteProcessId', process_name, cb);
}
CLI.stop = function(process_name, cb) {
@ -1199,13 +1194,8 @@ CLI.stop = function(process_name, cb) {
}
else if (process_name.indexOf('.json') > 0)
CLI.actionFromJson('stopProcessId', process_name, commander, 'file', cb);
else {
CLI._stop(process_name, cb);
}
}
CLI._stop = function(process_name, cb) {
CLI._operate('stopProcessId', process_name, cb);
else
CLI._operate('stopProcessId', process_name, cb);
}
CLI.getProcessIdByName = function(name, cb) {

View File

@ -45,7 +45,7 @@ require('./God/ForkMode.js')(God);
require('./God/ClusterMode.js')(God);
require('./God/Reload')(God);
require('./God/ActionMethods')(God);
require('./God/DeprecatedCalls')(God);
//require('./God/DeprecatedCalls')(God);
require('./Watcher')(God);
/**

View File

@ -230,22 +230,22 @@ module.exports = function(God) {
*/
God.stopProcessId = function(id, cb) {
if(typeof id == 'object' && 'id' in id)
id = id.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.getFormatedProcesses());
return cb(null, God.getFormatedProcess(id));
if (God.clusters_db[id].pm2_env.status === cst.LAUNCHING_STATUS
|| (God.clusters_db[id].state && God.clusters_db[id].state === 'none')) {
setTimeout(function() {
God.stopProcessId(id, cb);
}, 250);
return;
return false;
}
var proc = God.clusters_db[id];
var proc = God.clusters_db[id];
var timeout = null;
var timeout2 = null;
@ -255,7 +255,7 @@ module.exports = function(God) {
if (!proc.process.pid) {
proc.pm2_env.status = cst.STOPPED_STATUS;
return cb(null);
return cb(null, []);
}
var kill_anyway = function(proc) {
@ -280,12 +280,12 @@ module.exports = function(God) {
God.notify('exit', proc);
}
return cb(null, God.getFormatedProcesses());
return cb(null, God.getFormatedProcess(id));
});
}
else {
console.error('[stopProcessId] Could not kill process with pid 0');
return cb(null, God.getFormatedProcesses());
return cb(null, God.getFormatedProcess(id));
}
};
@ -331,7 +331,7 @@ module.exports = function(God) {
else {
process.nextTick(function() {
proc.pm2_env.status = cst.STOPPED_STATUS;
return cb(null, God.getFormatedProcesses());
return cb(null, God.getFormatedProcess(id));
});
}
}
@ -370,15 +370,14 @@ module.exports = function(God) {
* @return Literal
*/
God.deleteProcessId = function(id, cb) {
God.stopProcessId(id, function(err, dt) {
God.stopProcessId(id, function(err, proc) {
if (err) return cb(God.logAndGenerateError(err), {});
// ! transform to slow object
delete God.clusters_db[id];
var processes = God.getFormatedProcesses();
if (processes.length === 0)
if (Object.keys(God.clusters_db).length == 0)
God.next_id = 0;
return cb(null, processes);
return cb(null, proc);
});
return false;
};
@ -427,6 +426,38 @@ module.exports = function(God) {
return false;
};
/**
* Restart all process by name
* @method restartProcessName
* @param {} name
* @param {} cb
* @return Literal
*/
God.restartProcessName = function(name, cb) {
var processes = God.findByName(name);
if (processes && processes.length === 0)
return cb(God.logAndGenerateError('Unknown process'), {});
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
if (God.pm2_being_killed)
return next('[Watch] PM2 is being killed, stopping restart procedure...');
if (proc.pm2_env.status === cst.ONLINE_STATUS)
return God.restartProcessId({id:proc.pm2_env.pm_id}, next);
else if (proc.pm2_env.status !== cst.STOPPING_STATUS
&& proc.pm2_env.status !== cst.LAUNCHING_STATUS)
return God.startProcessId(proc.pm2_env.pm_id, next);
else
return next("[Watch] Process name %s is being stopped so I won't restart it", name);
}, function(err) {
if (err) return cb(God.logAndGenerateError(err));
return cb(null, God.getFormatedProcesses());
});
return false;
};
/**
* Send system signal to process id
* @method sendSignalToProcessId

View File

@ -1,152 +0,0 @@
var cluster = require('cluster');
var path = require('path');
var async = require('async');
var os = require('os');
var p = path;
var cst = require('../../constants.js');
var pkg = require('../../package.json');
var pidusage = require('pidusage');
var Common = require('../Common');
var util = require('util');
var debug = require('debug')('pm2:deprecated');
module.exports = function(God) {
/**
* Delete a process by name
* It will stop it and remove it from the database
* @method deleteProcessName
* @param {} name
* @param {} cb
* @return
*/
God.deleteProcessName = function(name, cb) {
var processes = God.findByName(name);
if (processes && processes.length === 0)
return cb(God.logAndGenerateError('Unknown process name'), {});
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
God.stopProcessId(proc.pm2_env.pm_id, function() {
// Slow object
delete God.clusters_db[proc.pm2_env.pm_id];
return next();
});
return false;
}, function(err) {
if (err) return cb(God.logAndGenerateError(err), {});
return cb(null, God.getFormatedProcesses());
});
};
/**
* Delete all processes
* It will stop them and remove them from the database
* @method deleteAll
* @param {} opts
* @param {} cb
* @return
*/
God.deleteAll = function(opts, cb) {
var processes = God.getFormatedProcesses();
if (processes && processes.length === 0)
return cb(God.logAndGenerateError('No processes launched'), {});
debug('Deleting all processes');
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
debug('Deleting process %s', proc.pm2_env.pm_id);
God.deleteProcessId(proc.pm2_env.pm_id, function() {
return next();
});
return false;
}, function(err) {
if (err) return cb(God.logAndGenerateError(err), {});
God.clusters_db = null;
God.clusters_db = {};
return cb(null, []);
});
};
/**
* Description
* @method stopAll
* @param {} env
* @param {} cb
* @return
*/
God.stopAll = function(env, cb) {
var processes = God.getFormatedProcesses();
if (processes && processes.length === 0) {
return cb(God.logAndGenerateError('No process launched'), {});
}
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
if (proc.state == cst.STOPPED_STATUS ||
proc.state == cst.STOPPING_STATUS) return next();
return God.stopProcessId(proc.pm2_env.pm_id, next);
}, function(err) {
if (err) return cb(new Error(err));
return cb(null, processes);
});
};
/**
* Restart all process by name
* @method restartProcessName
* @param {} name
* @param {} cb
* @return Literal
*/
God.restartProcessName = function(name, cb) {
var processes = God.findByName(name);
if (processes && processes.length === 0)
return cb(God.logAndGenerateError('Unknown process'), {});
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
if (God.pm2_being_killed)
return next('[Watch] PM2 is being killed, stopping restart procedure...');
if (proc.pm2_env.status === cst.ONLINE_STATUS)
return God.restartProcessId({id:proc.pm2_env.pm_id}, next);
else if (proc.pm2_env.status !== cst.STOPPING_STATUS
&& proc.pm2_env.status !== cst.LAUNCHING_STATUS)
return God.startProcessId(proc.pm2_env.pm_id, next);
else
return next("[Watch] Process name %s is being stopped so I won't restart it", name);
}, function(err) {
if (err) return cb(God.logAndGenerateError(err));
return cb(null, God.getFormatedProcesses());
});
return false;
};
/**
* Stop all process by name
* @method stopProcessName
* @param {} name
* @param {} cb
* @return
*/
God.stopProcessName = function(name, cb) {
var processes = God.findByName(name);
if (processes && processes.length === 0)
return cb(God.logAndGenerateError('Unknown process name'), {});
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
return God.stopProcessId(proc.pm2_env.pm_id, next);
}, function(err) {
if (err) return cb(God.logAndGenerateError(err));
return cb(null, God.getFormatedProcesses());
});
};
};

View File

@ -59,6 +59,17 @@ module.exports = function(God) {
return God.clusters_db;
};
God.getFormatedProcess = function getFormatedProcesses(id) {
if (God.clusters_db[id])
return {
pid : God.clusters_db[id].process.pid,
name : God.clusters_db[id].pm2_env.name,
pm2_env : God.clusters_db[id].pm2_env,
pm_id : God.clusters_db[id].pm2_env.pm_id
};
return {};
};
/**
* Description
* @method getFormatedProcesses

View File

@ -49,10 +49,30 @@ function getConf4() {
});
}
var async = require('async');
function deleteAll(data, cb) {
var processes = God.getFormatedProcesses();
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
console.log('Deleting process %s', proc.pm2_env.pm_id);
God.deleteProcessId(proc.pm2_env.pm_id, function() {
return next();
});
return false;
}, function(err) {
if (err) return cb(God.logAndGenerateError(err), {});
God.clusters_db = null;
God.clusters_db = {};
return cb(null, []);
});
}
describe('God', function() {
before(function(done) {
God.deleteAll({}, function(err, dt) {
deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
@ -66,43 +86,15 @@ describe('God', function() {
God.should.have.property('getSystemData');
God.should.have.property('getFormatedProcesses');
God.should.have.property('checkProcess');
God.should.have.property('stopAll');
God.should.have.property('reloadLogs');
God.should.have.property('stopProcessId');
God.should.have.property('sendSignalToProcessId');
God.should.have.property('sendSignalToProcessName');
});
describe('Special functions for God', function() {
before(function(done) {
God.deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
it('should kill a process by name', function(done) {
God.prepare(getConf(), function(err, procs) {
God.getFormatedProcesses().length.should.equal(2);
God.stopProcessName('echo', function() {
God.getFormatedProcesses().length.should.equal(2);
God.deleteAll({}, function() {
setTimeout(done, 1000);
});
});
});
});
});
describe('One process', function() {
var proc, pid;
before(function(done) {
God.deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
it('should fork one process', function(done) {
God.prepare(getConf(), function(err, procs) {
should(err).be.null;
@ -118,7 +110,7 @@ describe('God', function() {
var clu, pid;
before(function(done) {
God.deleteAll({}, function(err, dt) {
deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
@ -133,10 +125,9 @@ describe('God', function() {
});
it('should stop a process and keep in database on state stopped', function(done) {
God.stopProcessId(clu.pm2_env.pm_id, function(err, dt) {
var proc = God.findProcessById(clu.pm2_env.pm_id);
God.stopProcessId(clu.pm2_env.pm_id, function(err, proc) {
proc.pm2_env.status.should.be.equal('stopped');
God.checkProcess(proc.process.pid).should.be.equal(false);
God.checkProcess(proc.pid).should.be.equal(false);
done();
});
});
@ -150,24 +141,6 @@ describe('God', function() {
});
});
it('should stop this process by name and keep in db on state stopped', function(done) {
God.stopProcessName(clu.pm2_env.name, function(err, dt) {
var proc = God.findProcessById(clu.pm2_env.pm_id);
proc.pm2_env.status.should.be.equal('stopped');
God.checkProcess(proc.process.pid).should.be.equal(false);
done();
});
});
it('should restart the same process by NAME and set it as state online and be up', function(done) {
God.restartProcessName(clu.pm2_env.name, function(err, dt) {
var proc = God.findProcessById(clu.pm2_env.pm_id);
proc.pm2_env.status.should.be.equal('online');
God.checkProcess(proc.process.pid).should.be.equal(true);
done();
});
});
it('should stop and delete a process id', function(done) {
var old_pid = clu.pid;
God.deleteProcessId(clu.pm2_env.pm_id, function(err, dt) {
@ -178,29 +151,13 @@ describe('God', function() {
});
});
it('should start stop and delete the process name from database', function(done) {
God.prepare(getConf(), function(err, _clu) {
pid = _clu[0].pid;
_clu[0].pm2_env.status.should.be.equal('online');
var old_pid = _clu[0].pid;
God.deleteProcessName(_clu[0].pm2_env.name, function(err, dt) {
setTimeout(function() {
var proc = God.findProcessById(clu.pm2_env.pm_id);
should(proc == null);
God.checkProcess(old_pid).should.be.equal(false);
done();
}, 100);
});
});
});
});
describe('Reload - cluster', function() {
before(function(done) {
God.deleteAll({}, function(err, dt) {
deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
@ -224,14 +181,14 @@ describe('God', function() {
describe('Multi launching', function() {
before(function(done) {
God.deleteAll({}, function(err, dt) {
deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});
afterEach(function(done) {
God.deleteAll({}, function(err, dt) {
deleteAll({}, function(err, dt) {
setTimeout(done, 1000);
});
});

View File

@ -53,7 +53,6 @@ describe('Satan', function() {
methods.should.have.property('getSystemData');
methods.should.have.property('stopProcessId');
methods.should.have.property('stopAll');
methods.should.have.property('stopProcessName');
methods.should.have.property('killMe');
done();
});