Expose .kill method from programmatic calls + return right process array when starting JSON

This commit is contained in:
Unitech 2015-04-21 18:17:46 -04:00
parent ee0d8a8a2f
commit cd91aa3b8a
5 changed files with 213 additions and 8 deletions

View File

@ -339,7 +339,9 @@ CLI.actionFromJson = function(action, file, jsonVia, cb) {
* @param {function} cb
*/
CLI._startJson = function(cmd, opts, jsonVia, cb) {
var appConf, deployConf = null;
var appConf;
var deployConf = null;
var apps_info = [];
if (typeof(cb) === 'undefined' && typeof(jsonVia) === 'function')
cb = jsonVia;
@ -396,12 +398,13 @@ CLI._startJson = function(cmd, opts, jsonVia, cb) {
if (opts.watch)
app_paths.watch = true;
Satan.executeRemote('prepare', app_paths, function(err) {
Satan.executeRemote('prepare', app_paths, function(err, apps) {
printOut(cst.PREFIX_MSG + 'Process launched');
apps_info = apps_info.concat(apps);
next();
});
}, function(err) {
return cb ? cb(err || null, appConf) : speedList();
return cb ? cb(err || null, apps_info) : speedList();
});
};
@ -1450,7 +1453,7 @@ CLI.ilogs = function() {
* @param {} cb
* @return
*/
CLI.killDaemon = function(cb) {
CLI.killDaemon = CLI.kill = function(cb) {
printOut(cst.PREFIX_MSG + 'Stopping PM2...');
Satan.executeRemote('notifyKillPM2', {}, function() {});

View File

@ -349,14 +349,14 @@ God.prepare = function prepare(env, cb) {
(function ex(i) {
if (i <= 0) {
if (cb != null) return cb(null, arr);
if (cb) return cb(null, arr);
return false;
}
env.NODE_APP_INSTANCE = instance_id++;
return God.executeApp(Common.serialize(env), function(err, clu) {
if (err) return ex(i - 1);
arr.push(clu);
arr.push(Common.serialize(clu));
God.notify('start', clu, true);
return ex(i - 1);
});

137
test/programmatic/logs.js Normal file
View File

@ -0,0 +1,137 @@
var pm2 = require('../..');
var should = require('should');
var fs = require('fs');
var assert = require('better-assert');
var path = require('path');
describe('Max memory restart programmatic', function() {
var proc1 = null;
var procs = [];
after(pm2.disconnect);
afterEach(function(done) {
pm2.delete('all', done);
});
before(function(done) {
pm2.connect(function() {
pm2.kill(function() {
pm2.connect(function() {
done();
});
});
});
});
describe('Log merging', function() {
it('should process HAS post fixed logs with id (merge_logs: false)', function(done) {
pm2.start({
script: 'test/fixtures/echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log'
}, function(err, procs) {
should(err).be.null;
var out_file = procs[0].pm2_env.pm_out_log_path;
var err_file = procs[0].pm2_env.pm_err_log_path;
out_file.should.containEql('out-echo-0.log');
err_file.should.containEql('error-echo-0.log');
setTimeout(function() {
fs.readFileSync(out_file).toString().should.containEql('ok');
fs.readFileSync(err_file).toString().should.containEql('thisnok');
done();
}, 500);
});
});
it('should process HAS NOT post fixed logs with id (merge_logs: true)', function(done) {
pm2.start({
script: 'test/fixtures/echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log',
merge_logs : true
}, function(err, procs) {
should(err).be.null;
var out_file = procs[0].pm2_env.pm_out_log_path;
var err_file = procs[0].pm2_env.pm_err_log_path;
out_file.should.containEql('out-echo.log');
err_file.should.containEql('error-echo.log');
setTimeout(function() {
fs.readFileSync(out_file).toString().should.containEql('ok');
fs.readFileSync(err_file).toString().should.containEql('thisnok');
done();
}, 500);
});
});
it('should process HAS NOT post fixed logs with id and MERGED FILE (merge_logs: true)', function(done) {
pm2.start({
script: 'test/fixtures/echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log',
log_file : 'merged.log',
merge_logs : true
}, function(err, procs) {
should(err).be.null;
var out_file = procs[0].pm2_env.pm_out_log_path;
var err_file = procs[0].pm2_env.pm_err_log_path;
var log_file = procs[0].pm2_env.pm_log_path;
out_file.should.containEql('out-echo.log');
err_file.should.containEql('error-echo.log');
log_file.should.containEql('merged.log');
setTimeout(function() {
fs.readFileSync(out_file).toString().should.containEql('ok');
fs.readFileSync(err_file).toString().should.containEql('thisnok');
fs.readFileSync(log_file).toString().should.containEql('thisnok');
fs.readFileSync(log_file).toString().should.containEql('ok');
done();
}, 500);
});
});
});
describe('Log timestamp', function() {
it('should every file be timestamped', function(done) {
pm2.start({
script : 'test/fixtures/echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log',
log_file : 'merged.log',
merge_logs : true,
log_date_format : 'YYYY-MM-DD HH:mm Z'
}, function(err, procs) {
should(err).be.null;
var out_file = procs[0].pm2_env.pm_out_log_path;
var err_file = procs[0].pm2_env.pm_err_log_path;
var log_file = procs[0].pm2_env.pm_log_path;
out_file.should.containEql('out-echo.log');
err_file.should.containEql('error-echo.log');
log_file.should.containEql('merged.log');
setTimeout(function() {
fs.readFileSync(out_file).toString().should.containEql('20');
fs.readFileSync(err_file).toString().should.containEql('20');
fs.readFileSync(log_file).toString().should.containEql('20');
done();
}, 500);
});
});
});
});

View File

@ -0,0 +1,67 @@
var pm2 = require('../..');
var should = require('should');
var assert = require('better-assert');
var path = require('path');
describe('Max memory restart programmatic', function() {
var proc1 = null;
var procs = [];
after(pm2.disconnect);
afterEach(function(done) {
pm2.delete('all', done);
});
before(function(done) {
process.env.PM2_WORKER_INTERVAL = 1000;
pm2.connect(function() {
pm2.kill(function() {
pm2.connect(function() {
done();
});
});
});
});
describe('Max memory limit', function() {
it('should restart process based on memory limit (UGLY WAY)', function(done) {
pm2.start(process.cwd() + '/test/fixtures/big-array.js', {
maxMemoryRestart : '10M'
}, function(err, data) {
should(err).be.null;
setTimeout(function() {
pm2.list(function(err, ret) {
should(err).be.null;
ret[0].pm2_env.restart_time.should.not.eql(0);
done();
});
}, 3000);
});
});
it('should restart process based on memory limit (JSON WAY)', function(done) {
pm2.start({
script : process.cwd() + '/test/fixtures/big-array.js',
max_memory_restart : '10M'
}, function(err, data) {
should(err).be.null;
setTimeout(function() {
pm2.list(function(err, ret) {
should(err).be.null;
console.log(ret[0]);
ret[0].pm2_env.restart_time.should.not.eql(0);
done();
});
}, 3000);
});
});
});
});

View File

@ -392,8 +392,6 @@ describe('PM2 programmatic calls', function() {
done();
});
});
});
});