Skip to content

Commit 64a9e88

Browse files
committed
fix: report error when pm2 flush targets a nonexistent process
When pm2 flush is called with an app name or id that does not match any running process, it previously printed "Logs flushed" even though nothing was actually flushed. This is misleading. Now it prints an error and returns an error callback when the specified process is not found. Also adds tests for pm_id-based flushing and the not-found error case. Closes #4553
1 parent ff1ca97 commit 64a9e88

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

lib/API/LogManagement.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ module.exports = function(CLI) {
2828
Common.printError(err);
2929
return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
3030
}
31+
32+
var found = false;
33+
3134
list.forEach(function(l) {
3235
if (typeof api == 'undefined') {
36+
found = true;
3337
Common.printOut(cst.PREFIX_MSG + 'Flushing:');
3438
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_out_log_path);
3539
Common.printOut(cst.PREFIX_MSG + l.pm2_env.pm_err_log_path);
@@ -42,6 +46,7 @@ module.exports = function(CLI) {
4246
fs.closeSync(fs.openSync(l.pm2_env.pm_err_log_path, 'w'));
4347
}
4448
else if (l.pm2_env.pm_id == api || l.pm2_env.name === api) {
49+
found = true;
4550
Common.printOut(cst.PREFIX_MSG + 'Flushing:');
4651

4752
if (l.pm2_env.pm_log_path && fs.existsSync(l.pm2_env.pm_log_path)) {
@@ -61,7 +66,12 @@ module.exports = function(CLI) {
6166
}
6267
});
6368

64-
Common.printOut(cst.PREFIX_MSG + 'Logs flushed');
69+
if (found) {
70+
Common.printOut(cst.PREFIX_MSG + 'Logs flushed');
71+
} else if (typeof api !== 'undefined') {
72+
Common.printError(cst.PREFIX_MSG_ERR + 'No process found with name or id \'' + api + '\'');
73+
return cb ? cb(Common.retErr('process not found')) : that.exitCli(cst.ERROR_EXIT);
74+
}
6575
return cb ? cb(null, list) : that.exitCli(cst.SUCCESS_EXIT);
6676
});
6777
};

test/programmatic/flush.mocha.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,50 @@ describe('Programmatic flush feature test', function() {
7777
});
7878
});
7979
});
80+
it('flush logs by pm_id', function(done) {
81+
pm2.start({
82+
script: './echo.js',
83+
name: 'first',
84+
error_file : 'error-first.log',
85+
out_file : 'out-first.log',
86+
merge_logs: false
87+
}, function(err, procs) {
88+
should(err).be.null();
89+
var out_file = procs[0].pm2_env.pm_out_log_path;
90+
var err_file = procs[0].pm2_env.pm_err_log_path;
91+
var pm_id = String(procs[0].pm2_env.pm_id);
92+
pm2.start({
93+
script: './001-test.js',
94+
name: 'second',
95+
error_file : 'error-second.log',
96+
out_file : 'out-second.log',
97+
merge_logs: false
98+
}, function(err, procs) {
99+
should(err).be.null();
100+
var out_file1 = procs[0].pm2_env.pm_out_log_path;
101+
var err_file1 = procs[0].pm2_env.pm_err_log_path;
102+
pm2.flush(pm_id, function(){
103+
fs.readFileSync(out_file, "utf8").toString().should.be.empty();
104+
fs.readFileSync(err_file, "utf8").toString().should.be.empty();
105+
fs.readFileSync(out_file1, "utf8").toString().should.not.be.empty();
106+
fs.readFileSync(err_file1, "utf8").toString().should.not.be.empty();
107+
done();
108+
});
109+
});
110+
});
111+
});
112+
it('should return error when process not found', function(done) {
113+
pm2.start({
114+
script: './echo.js',
115+
name: 'myapp',
116+
merge_logs: false
117+
}, function(err) {
118+
should(err).be.null();
119+
pm2.flush('nonexistent', function(err) {
120+
should(err).not.be.null();
121+
done();
122+
});
123+
});
124+
});
80125
});
81126
});

0 commit comments

Comments
 (0)