mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
God now use an unified method to log and send back error messages - CLI display error messages - add test - fix test
This commit is contained in:
parent
0a2a80662f
commit
d59200f2c4
69
lib/CLI.js
69
lib/CLI.js
@ -456,8 +456,8 @@ CLI.reloadProcessName = function (process_name, reload_method) {
|
||||
CLI.restartProcessByName = function(pm2_name) {
|
||||
Satan.executeRemote('restartProcessName', pm2_name, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error : ' + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
console.log(cst.PREFIX_MSG + 'Process ' + pm2_name + ' restarted');
|
||||
@ -468,8 +468,8 @@ CLI.restartProcessByName = function(pm2_name) {
|
||||
CLI.restartProcessById = function(pm2_id) {
|
||||
Satan.executeRemote('restartProcessId', pm2_id, function(err, res) {
|
||||
if (err) {
|
||||
console.error('Error : ' + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
console.log(cst.PREFIX_MSG + 'Process ' + pm2_id + ' restarted');
|
||||
@ -480,9 +480,14 @@ CLI.restartProcessById = function(pm2_id) {
|
||||
CLI.restartAll = function() {
|
||||
Satan.executeRemote('getMonitorData', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error retrieving process list: ' + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
if (list && list.length === 0) {
|
||||
printError('No process launched');
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
|
||||
|
||||
(function rec(processes) {
|
||||
var proc = processes[0];
|
||||
@ -493,12 +498,12 @@ CLI.restartAll = function() {
|
||||
}
|
||||
Satan.executeRemote('restartProcessId', proc.pm2_env.pm_id, function(err, res) {
|
||||
if (err) {
|
||||
console.error('Error : ' + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.log(cst.PREFIX_MSG + 'Process ' + proc.pm2_env.name + ' restarted');
|
||||
processes.shift();
|
||||
rec(processes);
|
||||
return rec(processes);
|
||||
});
|
||||
return false;
|
||||
})(list);
|
||||
@ -508,7 +513,7 @@ CLI.restartAll = function() {
|
||||
CLI.stopAll = function() {
|
||||
Satan.executeRemote('stopAll', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error(cst.PREFIX_MSG_ERR + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
@ -525,8 +530,8 @@ CLI.deleteProcess = function(process_name, jsonVia) {
|
||||
console.log(cst.PREFIX_MSG + 'Stopping and deleting all processes');
|
||||
Satan.executeRemote('deleteAll', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error(cst.PREFIX_MSG_ERR + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
speedList();
|
||||
@ -536,8 +541,8 @@ CLI.deleteProcess = function(process_name, jsonVia) {
|
||||
console.log('Stopping and deleting process by id : %s', process_name);
|
||||
Satan.executeRemote('deleteProcessId', process_name, function(err, list) {
|
||||
if (err) {
|
||||
console.error(cst.PREFIX_MSG_ERR + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
speedList();
|
||||
@ -547,8 +552,8 @@ CLI.deleteProcess = function(process_name, jsonVia) {
|
||||
console.log(cst.PREFIX_MSG + 'Stopping and deleting process by name %s', process_name);
|
||||
Satan.executeRemote('deleteProcessName', process_name, function(err, list) {
|
||||
if (err) {
|
||||
console.error('\n' + cst.PREFIX_MSG_ERR + err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
UX.processing.stop();
|
||||
speedList();
|
||||
@ -559,8 +564,8 @@ CLI.deleteProcess = function(process_name, jsonVia) {
|
||||
CLI.stopProcessName = function(name) {
|
||||
Satan.executeRemote('stopProcessName', name, function(err, list) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.log(cst.PREFIX_MSG + 'Stopping process by name ' + name);
|
||||
UX.processing.stop();
|
||||
@ -571,8 +576,8 @@ CLI.stopProcessName = function(name) {
|
||||
CLI.stopId = function(pm2_id) {
|
||||
Satan.executeRemote('stopProcessId', pm2_id, function(err, list) {
|
||||
if (err) {
|
||||
console.error(cst.PREFIX_MSG_ERR + pm2_id + ' : pm2 id not found');
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
printError(err);
|
||||
return exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.log(cst.PREFIX_MSG + ' Process stopped');
|
||||
UX.processing.stop();
|
||||
@ -598,7 +603,7 @@ CLI.list = function() {
|
||||
CLI.jlist = function(debug) {
|
||||
Satan.executeRemote('getMonitorData', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error retrieving process list: ' + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
if (debug)
|
||||
@ -615,7 +620,7 @@ CLI.flush = function() {
|
||||
|
||||
Satan.executeRemote('getMonitorData', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error retrieving process list: ' + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
list.forEach(function(l) {
|
||||
@ -636,7 +641,7 @@ CLI.sendSignalToProcessName = function(signal, process_name) {
|
||||
process_name : process_name
|
||||
}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error : ' + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.log(cst.PREFIX_MSG + 'Succesfully sent signal %s to process name %s', signal, process_name);
|
||||
@ -651,7 +656,7 @@ CLI.sendSignalToProcessId = function(signal, process_id) {
|
||||
process_id : process_id
|
||||
}, function(err, list) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.log(cst.PREFIX_MSG + 'Succesfully sent signal %s to process id %s', signal, process_id);
|
||||
@ -663,7 +668,7 @@ CLI.sendSignalToProcessId = function(signal, process_id) {
|
||||
CLI.monit = function() {
|
||||
Satan.executeRemote('getMonitorData', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error retrieving process list: ' + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
if (Object.keys(list).length == 0) {
|
||||
@ -694,7 +699,7 @@ CLI.streamLogs = function(id) {
|
||||
|
||||
Satan.executeRemote('getMonitorData', {}, function(err, list) {
|
||||
if (err) {
|
||||
console.error('Error retrieving process list: ' + err);
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
|
||||
@ -719,7 +724,7 @@ CLI.streamLogs = function(id) {
|
||||
CLI.killDaemon = function() {
|
||||
Satan.killDaemon(function(err, res) {
|
||||
if (err) {
|
||||
console.error('Error when killing daemon');
|
||||
printError(err);
|
||||
exitCli(cst.ERROR_EXIT);
|
||||
}
|
||||
console.info('Daemon killed');
|
||||
@ -763,6 +768,16 @@ function speedList() {
|
||||
});
|
||||
}
|
||||
|
||||
function printError(msg) {
|
||||
if (msg instanceof Error)
|
||||
return console.error(msg.message);
|
||||
return console.error(msg);
|
||||
};
|
||||
|
||||
function printOut(msg) {
|
||||
console.log(msg);
|
||||
};
|
||||
|
||||
function exitCli(code) {
|
||||
Satan.client.sock.close();
|
||||
return process.exit(code);
|
||||
|
||||
@ -92,6 +92,10 @@ module.exports = function(God) {
|
||||
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();
|
||||
@ -226,6 +230,9 @@ module.exports = function(God) {
|
||||
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 (proc.pm2_env.status == cst.ONLINE_STATUS)
|
||||
return God.restartProcessId(proc.pm2_env.pm_id, next);
|
||||
@ -235,6 +242,8 @@ module.exports = function(God) {
|
||||
if (err) return cb(God.logAndGenerateError(err));
|
||||
return cb(null, God.getFormatedProcesses());
|
||||
});
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -244,6 +253,9 @@ module.exports = function(God) {
|
||||
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) {
|
||||
@ -284,6 +296,9 @@ module.exports = function(God) {
|
||||
var processes = God.findByName(opts.process_name);
|
||||
var signal = opts.signal;
|
||||
|
||||
if (processes && processes.length === 0)
|
||||
return cb(God.logAndGenerateError('Unknown process name'), {});
|
||||
|
||||
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
|
||||
if (proc.pm2_env.status == cst.ONLINE_STATUS) {
|
||||
try {
|
||||
@ -308,6 +323,9 @@ module.exports = function(God) {
|
||||
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() {
|
||||
delete God.clusters_db[proc.pm2_env.pm_id];
|
||||
@ -328,6 +346,9 @@ module.exports = function(God) {
|
||||
God.deleteAll = function(opts, cb) {
|
||||
var processes = God.getFormatedProcesses();
|
||||
|
||||
if (processes && processes.length === 0)
|
||||
return cb(God.logAndGenerateError('No processes launched'), {});
|
||||
|
||||
async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(proc, next) {
|
||||
God.stopProcessId(proc.pm2_env.pm_id, function() {
|
||||
delete God.clusters_db[proc.pm2_env.pm_id];
|
||||
|
||||
@ -30,7 +30,7 @@ module.exports = function(God) {
|
||||
}
|
||||
// Is a JSON or simple string
|
||||
console.error(err);
|
||||
return new Error({msg : err});
|
||||
return new Error(err);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -39,7 +39,7 @@
|
||||
"description": "Modern CLI process manager for Node apps with a builtin load-balancer",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "bash ./test/cli.sh && bash ./test/json_file.sh && bash ./test/harmony.sh && bash ./test/reload.sh && bash ./test/gracefulReload.sh && bash ./test/cli2.sh && bash ./test/misc.sh && bash ./test/fork.sh && bash ./test/infinite_loop.sh && NODE_ENV=test ./node_modules/mocha/bin/mocha test"
|
||||
"test": "bash ./test/cli.sh && bash ./test/json_file.sh && bash ./test/harmony.sh && bash ./test/reload.sh && bash ./test/right-exit-code.sh && bash ./test/gracefulReload.sh && bash ./test/cli2.sh && bash ./test/misc.sh && bash ./test/fork.sh && bash ./test/infinite_loop.sh && NODE_ENV=test ./node_modules/mocha/bin/mocha test"
|
||||
},
|
||||
"keywords": [
|
||||
"cli",
|
||||
|
||||
@ -16,9 +16,10 @@ function getConf() {
|
||||
}
|
||||
|
||||
describe('God', function() {
|
||||
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should have right properties', function() {
|
||||
@ -39,7 +40,9 @@ describe('God', function() {
|
||||
|
||||
describe('Special functions for God', function() {
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should kill a process by name', function(done) {
|
||||
@ -64,7 +67,9 @@ describe('God', function() {
|
||||
var proc, pid;
|
||||
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should fork one process', function(done) {
|
||||
@ -82,9 +87,10 @@ describe('God', function() {
|
||||
var clu, pid;
|
||||
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should start a process', function(done) {
|
||||
God.prepare(getConf(), function(err, proce) {
|
||||
clu = proce;
|
||||
@ -178,7 +184,9 @@ describe('God', function() {
|
||||
describe('Reload - cluster', function() {
|
||||
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should launch app', function(done) {
|
||||
@ -220,11 +228,16 @@ describe('God', function() {
|
||||
describe('Multi launching', function() {
|
||||
|
||||
before(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
afterEach(function(done) {
|
||||
God.deleteAll({}, done);
|
||||
God.deleteAll({}, function(err, dt) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should launch multiple processes depending on CPUs available', function(done) {
|
||||
|
||||
29
test/right-exit-code.sh
Normal file
29
test/right-exit-code.sh
Normal file
@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
SRC=$(cd $(dirname "$0"); pwd)
|
||||
source "${SRC}/include.sh"
|
||||
|
||||
cd $file_path
|
||||
|
||||
echo -e "\033[1mRIGHT EXIT CODES:\033[0m"
|
||||
|
||||
|
||||
$pm2 kill
|
||||
|
||||
$pm2 restart BULLSHIT
|
||||
ispec "Unknown process = error exit"
|
||||
|
||||
$pm2 restart 666
|
||||
ispec "Unknown process = error exit"
|
||||
|
||||
$pm2 restart all
|
||||
ispec "No process = error exit"
|
||||
|
||||
$pm2 stop all
|
||||
ispec "No process = error exit"
|
||||
|
||||
$pm2 delete 10
|
||||
ispec "No process = error exit"
|
||||
|
||||
$pm2 delete toto
|
||||
ispec "No process = error exit"
|
||||
Loading…
x
Reference in New Issue
Block a user