Skip to content

Commit

Permalink
feature: #3485 add cleardump command
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Mar 2, 2018
1 parent f2523f6 commit ced2835
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
9 changes: 9 additions & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@ commander.command('dump')
pm2.dump();
}));

//
// Delete dump file
//
commander.command('cleardump')
.description('Create empty dump file')
.action(failOnUnknown(function() {
pm2.clearDump();
}));

//
// Save processes to file
//
Expand Down
19 changes: 18 additions & 1 deletion lib/API/Startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ module.exports = function(CLI) {
console.error(e.stack || e);
try {
// try to backup file
fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH);
if(fs.existsSync(cst.DUMP_BACKUP_FILE_PATH)) {
fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH);
}
} catch (e) {
// don't keep broken file
fs.unlinkSync(cst.DUMP_FILE_PATH);
Expand All @@ -431,6 +433,21 @@ module.exports = function(CLI) {
});
};

/**
* Remove DUMP_FILE_PATH file and DUMP_BACKUP_FILE_PATH file
* @method dump
* @param {} cb
* @return
*/
CLI.prototype.clearDump = function(cb) {
fs.writeFileSync(cst.DUMP_FILE_PATH, JSON.stringify([]));

if(cb && typeof cb === 'function') return cb();

Common.printOut(cst.PREFIX_MSG + 'Successfully created %s', cst.DUMP_FILE_PATH);
return this.exitCli(cst.SUCCESS_EXIT);
};

/**
* Resurrect processes
* @method resurrect
Expand Down
4 changes: 3 additions & 1 deletion lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ module.exports = function(God) {
console.error(e.stack || e);
try {
// try to backup file
fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH);
if(fs.existsSync(cst.DUMP_BACKUP_FILE_PATH)) {
fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH);
}
} catch (e) {
// don't keep broken file
fs.unlinkSync(cst.DUMP_FILE_PATH);
Expand Down
7 changes: 6 additions & 1 deletion test/programmatic/programmatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ describe('PM2 programmatic calls', function() {
});

after(function(done) {
pm2.kill(done);
pm2.delete('all', function(err, ret) {
// clean dump file
pm2.clearDump(function(err) {
pm2.kill(done);
});
});
});

before(function(done) {
Expand Down

0 comments on commit ced2835

Please sign in to comment.