fix offline - add updatePM2 command to update in-memory pm2 - doc - test

This commit is contained in:
tknew2 2014-05-16 03:52:34 +02:00
parent fc5df8eafa
commit 9e6fcb1bed
6 changed files with 69 additions and 19 deletions

View File

@ -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:
<a name="update-pm2"/>
## 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
```
<a name="builtin-remote-monitoring"/>

11
bin/pm2
View File

@ -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);
});

View File

@ -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);
});
});

View File

@ -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() {

View File

@ -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');
});
});
}

View File

@ -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