Skip to content

Commit

Permalink
Merge pull request #3822 from imarakho/flush_parameter
Browse files Browse the repository at this point in the history
added parameter to flush command(added tests)
  • Loading branch information
wallet77 committed Aug 17, 2018
2 parents 7e92855 + 31789a9 commit bbcc85a
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 8 deletions.
10 changes: 9 additions & 1 deletion bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,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
//
Expand Down
27 changes: 20 additions & 7 deletions lib/API/LogManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand All @@ -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);
Expand Down
81 changes: 81 additions & 0 deletions test/programmatic/flush.mocha.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
});
});

0 comments on commit bbcc85a

Please sign in to comment.