From 49aed09a8ee9563558141a3ce8f35b1fd1ef2a2e Mon Sep 17 00:00:00 2001 From: tknew2 Date: Sun, 13 Apr 2014 16:01:40 +0800 Subject: [PATCH] Add command pm2 describe to get all informations about a process - tests --- bin/pm2 | 8 ++++++++ lib/CLI.js | 29 +++++++++++++++++++++++++-- lib/CliUx.js | 51 +++++++++++++++++++++++++++++++++++++++++------ test/cli.sh | 13 ++++++++++++ test/god.mocha.js | 1 + 5 files changed, 94 insertions(+), 8 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index 614edb48..b4d3534c 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -312,6 +312,14 @@ commander.command('generate ') CLI.generateSample(name); }); +commander.command('describe ') + .description('describe all parameters of a process id') + .action(CLI.describeProcess); + +commander.command('desc ') + .description('describe all parameters of a process id') + .action(CLI.describeProcess); + // // List command // diff --git a/lib/CLI.js b/lib/CLI.js index 1522d3f0..e80e8d65 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -639,6 +639,28 @@ CLI.flush = function() { }); }; +CLI.describeProcess = function(pm2_id) { + var found = false; + Satan.executeRemote('getMonitorData', {}, function(err, list) { + if (err) { + console.error('Error retrieving process list: ' + err); + exitCli(cst.ERROR_EXIT); + } + list.forEach(function(proc) { + if (proc.pm_id == pm2_id) { + found = true; + UX.describeTable(proc); + } + }); + + if (found === false) { + printError(pm2_id + ' id doesn\'t exists'); + return exitCli(cst.ERROR_EXIT); + } + return exitCli(cst.SUCCESS_EXIT); + }); +}; + CLI.reloadLogs = function() { printOut('Reloading all logs...'); Satan.executeRemote('reloadLogs', {}, function(err, logs) { @@ -743,7 +765,7 @@ CLI.killDaemon = function() { exitCli(cst.ERROR_EXIT); } - async.eachLimit(list, 1, function(proc, next) { + async.eachLimit(list, 2, function(proc, next) { Satan.executeRemote('deleteProcessId', proc.pm2_env.pm_id, function(err, res) { if (err) printError('Error : ' + err); @@ -788,8 +810,11 @@ function speedList() { } if (commander.miniList && !commander.silent) UX.miniDisplay(list); - else if (!commander.silent) + else if (!commander.silent) { UX.dispAsTable(list); + printOut(' Use pm2 describe to get more informations'); + } + if (commander.daemon) { diff --git a/lib/CliUx.js b/lib/CliUx.js index 6c76b545..58d70c94 100644 --- a/lib/CliUx.js +++ b/lib/CliUx.js @@ -31,10 +31,38 @@ UX.miniDisplay = function(list) { }; +UX.describeTable = function(process) { + var table = new Table({ + style : {'padding-left' : 1, head : ['cyan', 'bold'], border : ['white'], compact : true} + }); + var pm2_env = process.pm2_env; + + console.log('Describing process with pid %d - name %s', pm2_env.pm_id, pm2_env.name); + table.push( + { 'status' : colorStatus(pm2_env.status) }, + { 'name': pm2_env.name }, + { 'id' : pm2_env.pm_id }, + { 'path' : pm2_env.pm_exec_path }, + { 'exec cwd' : pm2_env.pm_cwd }, + { 'error log path' : pm2_env.pm_err_log_path }, + { 'out log path' : pm2_env.pm_out_log_path }, + { 'pid path' : pm2_env.pm_pid_path }, + { 'mode' : pm2_env.exec_mode }, + { 'watch & reload' : pm2_env.watch ? '✔'.green.bold : '✘' }, + { 'interpreter' : pm2_env.exec_interpreter }, + { 'restarts' : pm2_env.restart_time }, + { 'unstable restarts' : pm2_env.unstable_restarts }, + { 'uptime' : (pm2_env.pm_uptime && pm2_env.status == 'online') ? timeSince(pm2_env.pm_uptime) : 0 }, + { 'created at' : new Date(pm2_env.created_at).toISOString() } + ); + + console.log(table.toString()); +}; + UX.dispAsTable = function(list) { var table = new Table({ - head: ['App Name', 'id', 'mode', 'PID', 'status', 'port', 'Restarted', 'Uptime', 'memory', 'watching', 'err logs'], - colAligns : ['left', 'left', 'left', 'left', 'left', 'left', 'right', 'left', 'right', 'right', 'left'], + head: ['App Name', 'id', 'mode', 'PID', 'status', 'port', 'Restarted', 'Uptime', 'memory', 'watching'], + colAligns : ['left', 'left', 'left', 'left', 'left', 'left', 'right', 'left', 'right', 'right'], style : {'padding-left' : 1, head : ['cyan', 'bold'], border : ['white'], compact : true} }); @@ -50,13 +78,12 @@ UX.dispAsTable = function(list) { l.pm2_env.pm_id, mode == 'fork' ? 'fork'.inverse.bold : 'cluster'.blue.bold, l.pid, - status == 'online' ? 'online'.bold.green : status.bold.red, + colorStatus(status), port ? port : '', l.pm2_env.restart_time ? l.pm2_env.restart_time : 0, (l.pm2_env.pm_uptime && status == 'online') ? timeSince(l.pm2_env.pm_uptime) : 0, l.monit ? UX.bytesToSize(l.monit.memory, 3) : '', - l.pm2_env.watch ? '✔' : '✘', - l.pm2_env.pm_err_log_path + l.pm2_env.watch ? 'activated'.green.bold : 'unactivated'.grey ]; table.push(obj); @@ -96,6 +123,19 @@ UX.bytesToSize = function(bytes, precision) { } }; +function colorStatus(status) { + switch (status) { + case 'online': + return 'online'.green.bold; + break; + case 'launching': + return 'launching'.blue.bold; + break; + default: + return status.bold.red; + } +}; + function timeSince(date) { var seconds = Math.floor((new Date() - date) / 1000); @@ -123,4 +163,3 @@ function timeSince(date) { } return Math.floor(seconds) + 's'; } - diff --git a/test/cli.sh b/test/cli.sh index d534497f..8a99ffe1 100755 --- a/test/cli.sh +++ b/test/cli.sh @@ -46,6 +46,19 @@ OUT=`$pm2 prettylist | grep -o "stopped" | wc -l` [ $OUT -eq 3 ] || fail "$1" success "$1" +# +# Describe process +# +$pm2 describe 0 +spec "should describe stopped process" + +$pm2 restart 1 + +$pm2 describe 1 +spec "should describe online process" + +$pm2 describe asdsa +ispec "should exit with right exit code when no process found" # diff --git a/test/god.mocha.js b/test/god.mocha.js index de44c8f5..c2b9d993 100644 --- a/test/god.mocha.js +++ b/test/god.mocha.js @@ -31,6 +31,7 @@ describe('God', function() { 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('reload'); God.should.have.property('reloadProcessName');