Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Unitech/pm2 into development
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Mar 5, 2018
2 parents aae1d55 + 9436f11 commit 48f81a8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 40 deletions.
18 changes: 9 additions & 9 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,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 Expand Up @@ -882,15 +891,6 @@ commander.command('backward <name>')
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
//
Expand Down
49 changes: 44 additions & 5 deletions lib/API/Startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,32 @@ module.exports = function(CLI) {
* @return
*/
function fin(err) {

// 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) {
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);
Expand All @@ -390,8 +409,13 @@ module.exports = function(CLI) {
} catch (e) {
console.error(e.stack || e);
try {
fs.unlinkSync(cst.DUMP_FILE_PATH);
// try to backup file
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);
console.error(e.stack || e);
}
Common.printOut(cst.PREFIX_MSG_ERR + 'Failed to save dump file in %s', cst.DUMP_FILE_PATH);
Expand All @@ -415,6 +439,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
3 changes: 1 addition & 2 deletions lib/Daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -231,7 +231,6 @@ Daemon.prototype.innerStart = function(cb) {
notifyByProcessId : God.notifyByProcessId,

notifyKillPM2 : God.notifyKillPM2,
forceGc : God.forceGc,
monitor : God.monitor,
unmonitor : God.unmonitor,

Expand Down
27 changes: 22 additions & 5 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,25 @@ module.exports = function(God) {
}

function fin(err) {

// 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});
}

// 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);
Expand All @@ -155,8 +167,13 @@ module.exports = function(God) {
} catch (e) {
console.error(e.stack || e);
try {
fs.unlinkSync(cst.DUMP_FILE_PATH);
// try to backup file
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);
console.error(e.stack || e);
}
}
Expand Down
17 changes: 0 additions & 17 deletions lib/God/Methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
};

};
1 change: 0 additions & 1 deletion lib/Satan.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ Satan.remoteWrapper = function() {

killMe : God.killMe,
notifyKillPM2 : God.notifyKillPM2,
forceGc : God.forceGc,

findByFullPath : God.findByFullPath,

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 48f81a8

Please sign in to comment.