added parameter to flush command(tests added)

This commit is contained in:
Igor Marakhov 2018-07-24 15:35:32 +03:00
parent 992a045227
commit 31789a9a6d
3 changed files with 110 additions and 8 deletions

10
bin/pm2
View File

@ -803,12 +803,20 @@ commander.command('dashboard')
//
// Flushing command
//
commander.command('flush [api]')
.description('flush logs')
.action(function(api) {
pm2.flush(api);
});
/* old version
commander.command('flush')
.description('flush logs')
.action(failOnUnknown(function() {
pm2.flush();
}));
*/
//
// Reload all logs
//

View File

@ -15,9 +15,8 @@ module.exports = function(CLI) {
* @method flush
* @return
*/
CLI.prototype.flush = function(cb) {
CLI.prototype.flush = function(api, cb) {
var that = this;
Common.printOut(cst.PREFIX_MSG + 'Flushing ' + cst.PM2_LOG_FILE_PATH);
fs.closeSync(fs.openSync(cst.PM2_LOG_FILE_PATH, 'w'));
@ -31,13 +30,27 @@ module.exports = function(CLI) {
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path);
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path);
if (l.pm2_env.pm_log_path) {
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_log_path);
fs.closeSync(fs.openSync(l.pm2_env.pm_log_path, 'w'));
if(typeof api == 'undefined')
{
if (l.pm2_env.pm_log_path) {
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_log_path);
fs.closeSync(fs.openSync(l.pm2_env.pm_log_path, 'w'));
}
fs.closeSync(fs.openSync(l.pm2_env.pm_out_log_path, 'w'));
fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w'));
}
else
{
if (l.pm2_env.pm_log_path && l.pm2_env.pm_log_path.lastIndexOf('/') < l.pm2_env.pm_log_path.lastIndexOf(api)) {
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_log_path);
fs.closeSync(fs.openSync(l.pm2_env.pm_log_path, 'w'));
}
fs.closeSync(fs.openSync(l.pm2_env.pm_out_log_path, 'w'));
fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w'));
if(l.pm2_env.pm_out_log_path.lastIndexOf('/') < l.pm2_env.pm_out_log_path.lastIndexOf(api))
fs.closeSync(fs.openSync(l.pm2_env.pm_out_log_path, 'w'));
if(l.pm2_env.pm_err_log_path.lastIndexOf('/') < l.pm2_env.pm_err_log_path.lastIndexOf(api))
fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w'));
}
});
Common.printOut(cst.PREFIX_MSG + 'Logs flushed');
return cb ? cb(null, list) : that.exitCli(cst.SUCCESS_EXIT);

View File

@ -0,0 +1,81 @@
process.env.NODE_ENV = 'test';
var PM2 = require('../..');
var should = require('should');
var fs = require('fs');
var path = require('path');
describe('Programmatic flush feature test', function() {
var proc1 = null;
var procs = [];
var pm2 = new PM2.custom({
cwd : __dirname + '/../fixtures'
});
before(function(done) {
pm2.delete('all', function() {
done();
});
});
after(function(done) {
pm2.disconnect(done);
});
afterEach(function(done) {
pm2.delete('all', done);
});
describe('Flush test', function() {
it('flush all logs', function(done) {
pm2.start({
script: './echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log',
merge_logs: false
}, 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');
pm2.flush(undefined, function(){
fs.readFileSync(out_file, "utf8").should.be.empty();
fs.readFileSync(err_file, "utf8").should.be.empty();
done();
});
});
});
it('flush only echo logs', function(done) {
pm2.start({
script: './echo.js',
error_file : 'error-echo.log',
out_file : 'out-echo.log',
merge_logs: false
}, 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;
pm2.start({
script: './001-test.js',
error_file : 'error-001-test.log',
out_file : 'out-001-test.log',
merge_logs: false
}, function(err, procs, $out_file, $err_file) {
should(err).be.null();
var out_file1 = procs[0].pm2_env.pm_out_log_path;
var err_file1 = procs[0].pm2_env.pm_err_log_path;
pm2.flush('echo', function(){
fs.readFileSync(out_file, "utf8").toString().should.be.empty();
fs.readFileSync(err_file, "utf8").toString().should.be.empty();
fs.readFileSync(out_file1, "utf8").toString().should.not.be.empty();
fs.readFileSync(err_file1, "utf8").toString().should.not.be.empty();
done();
});
});
});
});
});
});