From 9e6fcb1bed18ece1ff49c536c2111650c5934801 Mon Sep 17 00:00:00 2001 From: tknew2 Date: Fri, 16 May 2014 03:52:34 +0200 Subject: [PATCH] fix offline - add updatePM2 command to update in-memory pm2 - doc - test --- README.md | 15 +++++++++----- bin/pm2 | 11 ++++++++++- lib/CLI.js | 49 ++++++++++++++++++++++++++++++++++------------ lib/WatchDog.js | 7 ++++++- scripts/install.js | 1 + test/cli.sh | 5 +++++ 6 files changed, 69 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index aa277c67..98af4308 100644 --- a/README.md +++ b/README.md @@ -174,9 +174,11 @@ $ pm2 delete all # Will remove all processes from pm2 list # Misc +$ pm2 updatePM2 # Update in memory pm2 $ pm2 ping # Ensure pm2 dameon has been launched $ pm2 sendSignal SIGUSR2 my-app # Send system signal to script $ pm2 start app.js --no-daemon + ``` ## Different ways to launch a process @@ -251,13 +253,16 @@ Options: ## How to update PM2 -As PM2 is backgrounded, when you do an update you have to replace the old PM2 daemon by the new one: +Install the latest pm2 version : -``` +```bash $ npm install pm2@latest -g -$ pm2 dump # Save the current process list -$ pm2 kill # Kill current pm2 -$ pm2 resurrect # Resurrect previously saved processes +``` + +Then update the in-memory pm2 : + +```bash +$ pm2 updatePM2 ``` diff --git a/bin/pm2 b/bin/pm2 index f8d7b6e0..2ce68c77 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -64,6 +64,9 @@ commander.on('--help', function() { console.log(' Kill daemon pm2 :'); console.log(' $ pm2 kill'); console.log(''); + console.log(' Update pm2 :'); + console.log(' $ npm install pm2@latest -g ; pm2 updatePM2'); + console.log(''); console.log(' More examples in https://github.com/Unitech/pm2#usagefeatures'); console.log(''); }); @@ -252,6 +255,12 @@ commander.command('ping') .description(' ping pm2 daemon - if not up it will launch it') .action(failOnUnknown(CLI.ping)); +commander.command('updatePM2') + .description(' updatePM2 after a npm update') + .action(function() { + CLI.updatePM2() + }); + // // Interact // @@ -444,7 +453,7 @@ if (process.argv.length == 2) { // This message is triggered once the RPC Client is connected to the Daemon // in file Satan.js, method Satan.launchRPC // -process.on('satan:client:ready', function() { +process.once('satan:client:ready', function() { commander.parse(process.argv); }); diff --git a/lib/CLI.js b/lib/CLI.js index 70979c46..2d1c9304 100644 --- a/lib/CLI.js +++ b/lib/CLI.js @@ -345,16 +345,17 @@ CLI.ping = function() { }); }; -CLI.resurrect = function() { +CLI.resurrect = function(cb) { try { var apps = fs.readFileSync(cst.DUMP_FILE_PATH); } catch(e) { console.error(cst.PREFIX_MSG + 'No processes saved; DUMP file doesn\'t exist'); - return exitCli(cst.ERROR_EXIT); + if (cb) return cb(e); + else return exitCli(cst.ERROR_EXIT); } (function ex(apps) { - if (!apps[0]) return speedList(); + if (!apps[0]) return cb ? cb() : speedList(); Satan.executeRemote('prepare', apps[0], function(err) { if (err) console.error(cst.PREFIX_MSG_ERR + ' Process %s not launched - (script missing)', apps[0].pm_exec_path); @@ -367,10 +368,32 @@ CLI.resurrect = function() { })(JSON.parse(apps)); }; +CLI.updatePM2 = function(cb) { + console.log('Be sure to haave the latest version by doing `npm install pm2@latest -g` before doing this procedure.'); + + CLI.dump(function(err) { + console.log(cst.PREFIX_MSG + '--- dumped'); + CLI.killDaemon(function(err) { + console.log(cst.PREFIX_MSG + '--- killed'); + Satan.launchDaemon(function(err, child) { + + console.log(cst.PREFIX_MSG + '--- resurrected'); + if (err) { + console.error(err); + } + CLI.resurrect(function() { + console.log('>>>>>>>>>> PM2 updated'.blue.bold); + cb ? cb() : speedList(); + }); + + }); + }); + }); +}; /** * Dump current processes managed by pm2 into DUMP_FILE_PATH file */ -CLI.dump = function() { +CLI.dump = function(cb) { var env_arr = []; Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { @@ -381,7 +404,8 @@ CLI.dump = function() { function fin(err) { fs.writeFileSync(cst.DUMP_FILE_PATH, JSON.stringify(env_arr)); UX.processing.stop(); - exitCli(cst.SUCCESS_EXIT); + if (cb) return cb(null); + else return exitCli(cst.SUCCESS_EXIT); } (function ex(apps) { @@ -778,32 +802,33 @@ CLI.streamLogs = function(id) { }); }; -CLI.killDaemon = function() { - printOut('Killing pm2...'); +CLI.killDaemon = function(cb) { + printOut(cst.PREFIX_MSG + 'Killing pm2...'); Satan.executeRemote('getMonitorData', {}, function(err, list) { if (err) { console.error('Error retrieving process list: ' + err); - exitCli(cst.ERROR_EXIT); + cb ? cb() : exitCli(cst.ERROR_EXIT); } async.eachLimit(list, 2, function(proc, next) { Satan.executeRemote('deleteProcessId', proc.pm2_env.pm_id, function(err, res) { if (err) printError('Error : ' + err); - printOut('Process %s stopped', proc.pm2_env.name); + printOut(cst.PREFIX_MSG + 'Process %s stopped', proc.pm2_env.name); return next(); }); return false; }, function(err) { - printOut('All processes stopped'); + printOut(cst.PREFIX_MSG + 'All processes stopped'); Satan.killDaemon(function(err, res) { if (err) { printError(err); - exitCli(cst.ERROR_EXIT); + if (cb) return cb(err); + else exitCli(cst.ERROR_EXIT); } console.info('PM2 stopped'); - exitCli(cst.SUCCESS_EXIT); + cb ? cb(null, res) : exitCli(cst.SUCCESS_EXIT); }); }); diff --git a/lib/WatchDog.js b/lib/WatchDog.js index 81e283ad..7751ca31 100644 --- a/lib/WatchDog.js +++ b/lib/WatchDog.js @@ -118,7 +118,7 @@ WatchDog.interaction = function() { }, 10000); }; -WatchDog.connect = function() { +WatchDog.connect = function(cb) { var self = this; debug('Initializing watchdog'); @@ -139,8 +139,13 @@ WatchDog.connect = function() { else vs_socket = sock.connect(cst.WATCHDOG_PORT, cst.WATCHDOG_URL); + vs_socket.on('error', function(e) { + console.error(e); + }); + vs_socket.on('connect', function() { WatchDog.interaction(); + if (cb) cb(); }); vs_socket.on('reconnect attempt', function() { diff --git a/scripts/install.js b/scripts/install.js index ee194751..262a690d 100644 --- a/scripts/install.js +++ b/scripts/install.js @@ -31,6 +31,7 @@ q.askOne({ info: 'Would you like to receive an email when pm2 or your server goe q.askOne({ info: 'Email' }, function(email){ WatchDog.createConfFile(email, function() { console.log('Thanks for your subscription, if pm2 goes offline for more that 1min, you will be notified.'); + console.log('\nTo update current pm2 please do :\npm2 updatePM2\n'); }); }); } diff --git a/test/cli.sh b/test/cli.sh index 8a99ffe1..47efd253 100755 --- a/test/cli.sh +++ b/test/cli.sh @@ -60,6 +60,11 @@ spec "should describe online process" $pm2 describe asdsa ispec "should exit with right exit code when no process found" +# +# Update pm2 +# +$pm2 updatePM2 +spec "should update pm2" # # Main tests