From f2523f6a6b9d8b61ba6ace7b89a0353bee76360b Mon Sep 17 00:00:00 2001 From: vince Date: Fri, 2 Mar 2018 10:22:23 +0100 Subject: [PATCH 1/4] fix: #3485 fix issue when there is empty dump file --- lib/API/Startup.js | 26 +++++++++++++++++++++----- lib/God/ActionMethods.js | 19 ++++++++++++++----- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/API/Startup.js b/lib/API/Startup.js index 5249acc5d..5be3e3d63 100644 --- a/lib/API/Startup.js +++ b/lib/API/Startup.js @@ -371,13 +371,26 @@ module.exports = function(CLI) { * @return */ function fin(err) { + + // try to fix issues with empty dump file + // like #3485 + if (env_arr.length === 0) { + // if no process in list don't modify dump file + // process list should not be empty + if(cb) { + return cb(null, {success: true}); + } else { + Common.printOut(cst.PREFIX_MSG + 'Nothing to save !!!'); + Common.printOut(cst.PREFIX_MSG + 'In this case we keep old dump file. To clear dump file you can delete it manually !'); + that.exitCli(cst.SUCCESS_EXIT); + return; + } + } + // Back up dump file try { if (fs.existsSync(cst.DUMP_FILE_PATH)) { - if (fs.existsSync(cst.DUMP_BACKUP_FILE_PATH)) { - fs.unlinkSync(cst.DUMP_BACKUP_FILE_PATH); - } - fs.renameSync(cst.DUMP_FILE_PATH, cst.DUMP_BACKUP_FILE_PATH); + fs.copyFileSync(cst.DUMP_FILE_PATH, cst.DUMP_BACKUP_FILE_PATH); } } catch (e) { console.error(e.stack || e); @@ -390,8 +403,11 @@ module.exports = function(CLI) { } catch (e) { console.error(e.stack || e); try { - fs.unlinkSync(cst.DUMP_FILE_PATH); + // try to backup file + fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH); } catch (e) { + // don't keep broken file + fs.unlinkSync(cst.DUMP_FILE_PATH); console.error(e.stack || e); } Common.printOut(cst.PREFIX_MSG_ERR + 'Failed to save dump file in %s', cst.DUMP_FILE_PATH); diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index 65cb64157..6b577d69a 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -137,13 +137,19 @@ module.exports = function(God) { } function fin(err) { + + // try to fix issues with empty dump file + // like #3485 + if (process_list.length === 0) { + // if no process in list don't modify dump file + // process list should not be empty + return cb(null, {success:true, process_list: process_list}); + } + // Back up dump file try { if (fs.existsSync(cst.DUMP_FILE_PATH)) { - if (fs.existsSync(cst.DUMP_BACKUP_FILE_PATH)) { - fs.unlinkSync(cst.DUMP_BACKUP_FILE_PATH); - } - fs.renameSync(cst.DUMP_FILE_PATH, cst.DUMP_BACKUP_FILE_PATH); + fs.copyFileSync(cst.DUMP_FILE_PATH, cst.DUMP_BACKUP_FILE_PATH); } } catch (e) { console.error(e.stack || e); @@ -155,8 +161,11 @@ module.exports = function(God) { } catch (e) { console.error(e.stack || e); try { - fs.unlinkSync(cst.DUMP_FILE_PATH); + // try to backup file + fs.copyFileSync(cst.DUMP_BACKUP_FILE_PATH, cst.DUMP_FILE_PATH); } catch (e) { + // don't keep broken file + fs.unlinkSync(cst.DUMP_FILE_PATH); console.error(e.stack || e); } } From ced2835d2100dc7f5162295f7b51335374855495 Mon Sep 17 00:00:00 2001 From: vince Date: Fri, 2 Mar 2018 12:18:54 +0100 Subject: [PATCH 2/4] feature: #3485 add cleardump command --- bin/pm2 | 9 +++++++++ lib/API/Startup.js | 19 ++++++++++++++++++- lib/God/ActionMethods.js | 4 +++- test/programmatic/programmatic.js | 7 ++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index 05072f4bc..3099bba4d 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -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 // diff --git a/lib/API/Startup.js b/lib/API/Startup.js index 5be3e3d63..3064e6a62 100644 --- a/lib/API/Startup.js +++ b/lib/API/Startup.js @@ -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); @@ -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 diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index 6b577d69a..37a6255cf 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -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); diff --git a/test/programmatic/programmatic.js b/test/programmatic/programmatic.js index e71f0aecf..478e13618 100644 --- a/test/programmatic/programmatic.js +++ b/test/programmatic/programmatic.js @@ -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) { From 8951184688c720ded5b4b46bd5b393c3793f9b03 Mon Sep 17 00:00:00 2001 From: vince Date: Fri, 2 Mar 2018 15:25:15 +0100 Subject: [PATCH 3/4] fix: solve empty list when no process and try to update pm2 --- lib/API/Startup.js | 6 ++++++ lib/God/ActionMethods.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/API/Startup.js b/lib/API/Startup.js index 3064e6a62..f77ca5167 100644 --- a/lib/API/Startup.js +++ b/lib/API/Startup.js @@ -375,6 +375,12 @@ module.exports = function(CLI) { // try to fix issues with empty dump file // like #3485 if (env_arr.length === 0) { + + // fix : if no dump file, no process, only module and after pm2 update + if (!fs.existsSync(cst.DUMP_FILE_PATH)) { + that.clearDump(function(){}); + } + // if no process in list don't modify dump file // process list should not be empty if(cb) { diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index 37a6255cf..1df6b159c 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -141,6 +141,12 @@ module.exports = function(God) { // try to fix issues with empty dump file // like #3485 if (process_list.length === 0) { + + // fix : if no dump file, no process, only module and after pm2 update + if (!fs.existsSync(cst.DUMP_FILE_PATH)) { + that.clearDump(function(){}); + } + // if no process in list don't modify dump file // process list should not be empty return cb(null, {success:true, process_list: process_list}); From 97fd1010d005e59f2411042fa95891f9717fa8b7 Mon Sep 17 00:00:00 2001 From: vince Date: Mon, 5 Mar 2018 13:38:29 +0100 Subject: [PATCH 4/4] chore: fix issue with snapshot command + remove command forceGc --- bin/pm2 | 9 --------- lib/Daemon.js | 3 +-- lib/God/Methods.js | 17 ----------------- lib/Satan.js | 1 - 4 files changed, 1 insertion(+), 29 deletions(-) diff --git a/bin/pm2 b/bin/pm2 index 809177b03..55aeda641 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -882,15 +882,6 @@ commander.command('backward ') pm2.backward(pm2_name); }); -// -// Force PM2 to trigger garbage collection -// -commander.command('gc') - .description('force PM2 to trigger garbage collection') - .action(function() { - pm2.forceGc(); - }); - // // Perform a deep update of PM2 // diff --git a/lib/Daemon.js b/lib/Daemon.js index 26c9c98ef..c1d7019ff 100644 --- a/lib/Daemon.js +++ b/lib/Daemon.js @@ -153,7 +153,7 @@ Daemon.prototype.innerStart = function(cb) { var profiler; try { - profiler = require('v8-profiler'); + profiler = require('v8-profiler-node8'); } catch(e) { profiler = null; } @@ -231,7 +231,6 @@ Daemon.prototype.innerStart = function(cb) { notifyByProcessId : God.notifyByProcessId, notifyKillPM2 : God.notifyKillPM2, - forceGc : God.forceGc, monitor : God.monitor, unmonitor : God.unmonitor, diff --git a/lib/God/Methods.js b/lib/God/Methods.js index e7b8e9fa3..ea6753a2c 100644 --- a/lib/God/Methods.js +++ b/lib/God/Methods.js @@ -247,21 +247,4 @@ module.exports = function(God) { pm2_env.unstable_restarts = 0; }; - /** - * Description - * @method forcegc - * @return - */ - God.forceGc = function(opts, cb) { - if (global.gc) { - global.gc(); - debug('Garbage collection triggered successfully'); - if (cb) cb(null, {success: true}); - } - else { - debug('Garbage collection failed'); - if (cb) cb(null, {success: false}); - } - }; - }; diff --git a/lib/Satan.js b/lib/Satan.js index e6df06ba9..1fc6651e7 100644 --- a/lib/Satan.js +++ b/lib/Satan.js @@ -215,7 +215,6 @@ Satan.remoteWrapper = function() { killMe : God.killMe, notifyKillPM2 : God.notifyKillPM2, - forceGc : God.forceGc, findByFullPath : God.findByFullPath,