Skip to content

Commit 9f9d55a

Browse files
committed
fix Unitech#23 Unitech#22 - add data for the list feature
1 parent 13e37c1 commit 9f9d55a

File tree

7 files changed

+58
-18
lines changed

7 files changed

+58
-18
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ The next generation daemon process manager for Node.js with native clusterizatio
88

99
PM2 is mainly a production module for Linux servers.
1010

11+
Tested with Node v0.8, v0.10, v0.11
12+
1113
# Installation
1214

1315
```
@@ -47,6 +49,15 @@ $ pm2 start app.js -i max -- -a 23 // Pass arguments after -- to app.js
4749
$ pm2 start app.js -i max -e err.log -o out.log -w // Will start and generate a configuration file
4850
```
4951

52+
## Updating pm2 and keeping processes alive
53+
54+
```
55+
$ pm2 dump
56+
$ npm install -g pm2@latest
57+
$ pm2 kill
58+
$ pm2 resurect
59+
```
60+
5061
## pm2 list
5162

5263
List infos about all processes managed by pm2. It shows also how many times a process has been restarted because of an unhandled exception.

bin/pm2

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var util = require('util');
1313
var watch = require('watch');
1414

1515
const PREFIX_MSG = '\x1B[32m⌬ PM2 \x1B[39m';
16+
const PREFIX_MSG_ERR = '\x1B[31m⌬ PM2 [ERROR] \x1B[39m';
1617
const SUCCESS_EXIT = 0;
1718
const ERROR_EXIT = 1;
1819
const SAMPLE_FILE_PATH = '../lib/sample.json';
@@ -33,7 +34,7 @@ commander.version(pkg.version)
3334
//
3435
// Start command
3536
//
36-
commander.command('start <part>')
37+
commander.command('start <script>')
3738
.description('start specific part')
3839
.action(function(cmd) {
3940
if (cmd.indexOf('.json') > 0)
@@ -59,7 +60,7 @@ commander.command('stopAll')
5960
commander.command('stop <pm2_id>')
6061
.description('stop specific process pm2 id')
6162
.action(function(pm2_id) {
62-
console.log(PREFIX_MSG + 'Stopping process' + pm2_id);
63+
console.log(PREFIX_MSG + 'Stopping process ' + pm2_id);
6364
UX.processing.start();
6465
CLI.stopId(pm2_id);
6566
});
@@ -284,7 +285,7 @@ CLI.startFile = function(script) {
284285

285286
Satan.executeRemote('findByScript', {script : appConf.script}, function(err, exec) {
286287
if (exec && !commander.force) {
287-
console.log(PREFIX_MSG + 'Script already launched, add -f option to force re execution');
288+
console.log(PREFIX_MSG_ERR + 'Script already launched, add -f option to force re execution');
288289
process.exit(ERROR_EXIT);
289290
}
290291

@@ -445,11 +446,14 @@ CLI.web = function() {
445446
CLI.restart = function(pm2_id) {
446447
Satan.executeRemote('list', {}, function(err, list) {
447448
list.forEach(function(l) {
448-
if (l.pm_id == pm2_id) {
449+
if (l.pm_id == pm2_id && l.status != 'stopped') {
449450
try {
450451
process.kill(l.pid);
451452
} catch(e) { }
452453
}
454+
else if (l.pm_id == pm2_id && l.status == 'stopped') {
455+
Satan.executeRemote('startId', {id : l.pm_id}, function(err, list) {});
456+
}
453457
});
454458
setTimeout(function() {
455459
console.log('\n' + PREFIX_MSG + 'Process ' + pm2_id + ' restarted');
@@ -486,7 +490,11 @@ CLI.stopId = function(pm2_id) {
486490
Satan.executeRemote('stopId', {
487491
id : pm2_id
488492
}, function(err, list) {
489-
if (err) process.exit(ERROR_EXIT);
493+
if (err) {
494+
console.log('\n' + PREFIX_MSG_ERR + pm2_id + ' : pm2 id not found');
495+
process.exit(ERROR_EXIT);
496+
}
497+
490498
console.log('\n');
491499
console.log(PREFIX_MSG + 'Process stopped');
492500
UX.processing.stop();
@@ -606,7 +614,10 @@ function speedList() {
606614
function resolvePaths(app) {
607615
app["pm_exec_path"] = path.resolve(process.cwd(), app.script);
608616

609-
fs.existsSync(app.pm_exec_path);
617+
if (fs.existsSync(app.pm_exec_path) == false) {
618+
console.log(PREFIX_MSG_ERR + 'script not found : ' + app.pm_exec_path);
619+
process.exit(ERROR_EXIT);
620+
}
610621

611622
// Set current env
612623
util._extend(app, process.env);
@@ -615,7 +626,7 @@ function resolvePaths(app) {
615626
app["pm_out_log_path"] = path.resolve(process.cwd(), app.fileOutput);
616627
else {
617628
if (!app.name) {
618-
console.log(PREFIX_MSG + 'You havent specified log path, please specify at least a "name" field in the JSON');
629+
console.log(PREFIX_MSG_ERR + 'You havent specified log path, please specify at least a "name" field in the JSON');
619630
process.exit(ERROR_EXIT);
620631
}
621632
app["pm_out_log_path"] = path.resolve(cst.DEFAULT_LOG_PATH, [app.name, '-out.log'].join(''));
@@ -636,8 +647,9 @@ function resolvePaths(app) {
636647
app.pidFile = app["pm_pid_path"];
637648
}
638649

639-
fs.existsSync(app.pm_out_log_path);
640-
fs.existsSync(app.pm_err_log_path);
641-
650+
if (!fs.existsSync(app.pm_out_log_path) || !fs.existsSync(app.pm_err_log_path)) {
651+
console.log(PREFIX_MSG_ERR + 'logs file not accessible : ' + app.pm_out_log_path + ' | ' + app.pm_err_log_path);
652+
process.exit(ERROR_EXIT);
653+
}
642654
return app;
643655
}

lib/CliUx.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var p = require('path');
66
var UX = module.exports = {};
77

88
UX.dispAsTable = function(list) {
9-
var table = new Table({ head: ["Script", "id", "PID","status", "Restarted", "memory", "err logs"] });
9+
var table = new Table({ head: ["Script", "id", "PID","status", "Restarted", "Last restart", "memory", "err logs"] });
1010
list.forEach(function(l) {
1111
var u = l.opts.script;
1212
var obj = {};
@@ -16,8 +16,9 @@ UX.dispAsTable = function(list) {
1616
l.pid,
1717
l.status,
1818
l.opts.restart_time ? l.opts.restart_time : 0,
19+
l.opts.pm_uptime ? new Date(l.opts.pm_uptime).toISOString().replace(/T/, ' ').replace(/\..+/, '') : 0,
1920
l.monit ? UX.bytesToSize(l.monit.memory, 3) : '',
20-
l.opts.fileError,
21+
l.opts.fileError
2122
];
2223

2324
table.push(obj);

lib/God.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,14 @@ God.stopProcess = function(clu, cb) {
172172
};
173173

174174
God.stopProcessId = function(id, cb) {
175-
console.log(id);
175+
if (!(id in God.clusters_db))
176+
return cb({msg : 'Process not found'});
176177
God.clusters_db[id].opts.max = 0;
177178
process.kill(God.clusters_db[id].process.pid);
178179
God.clusters_db[id].process.pid = 0;
179-
setTimeout(cb, 200);
180+
setTimeout(function() {
181+
cb(null, null);
182+
}, 200);
180183
};
181184

182185
//
@@ -210,6 +213,9 @@ God.prepare = function(opts, cb) {
210213
function execute(env, cb) {
211214
var id;
212215

216+
if (env.pm_id && env.opts && env.opts.status == 'stopped') {
217+
delete God.clusters_db[env.pm_id];
218+
}
213219
id = God.next_id;
214220
God.next_id += 1;
215221

lib/Satan.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var bind = (function(addr) {
2424

2525
return {
2626
HOST: hostport[0],
27-
PORT: Number(hostport[1]),
28-
}
27+
PORT: Number(hostport[1])
28+
};
2929
})(cst.DAEMON_BIND_ADDR);
3030

3131
var Satan = module.exports = {};
@@ -104,7 +104,10 @@ Satan.remoteWrapper = function() {
104104
},
105105
stopId : function(opts, fn) {
106106
God.stopProcessId(opts.id, function(err, clu) {
107-
fn(err, stringifyOnce(clu, undefined, 0));
107+
if (err)
108+
fn(new Error('Process not found'));
109+
else
110+
fn(err, stringifyOnce(clu, undefined, 0));
108111
});
109112
},
110113
stopAll : function(opts, fn) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"description": "The next generation process manager for Node.js with native clusterization ",
1919
"main": "index.js",
2020
"scripts": {
21-
"test": "bash ./test/cli.sh ; NODE_ENV=test ./node_modules/mocha/bin/mocha test",
21+
"test": "bash ./test/cli.sh && NODE_ENV=test ./node_modules/mocha/bin/mocha test",
2222
"froze": "npm shrinkwrap"
2323
},
2424
"keywords": [

test/cli.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ cd $file_path
4646
$pm2 kill
4747
spec "kill daemon"
4848

49+
$pm2 start eyayimfake
50+
ispec "should fail if script doesnt exist"
51+
52+
$pm2 start $file_path/child.js -o /sadadsax
53+
ispec "should fail if output log unreachable"
54+
4955
$pm2
5056
ispec "No argument"
5157

@@ -114,6 +120,7 @@ spec "Should restart all processes"
114120
sleep 0.3
115121
wget -q http://localhost:9615/ -O $JSON_FILE
116122
OUT=`cat $JSON_FILE | grep -o "restart_time\":1" | wc -l`
123+
117124
[ $OUT -eq 7 ] || fail "$1"
118125
success "$1"
119126

0 commit comments

Comments
 (0)