Skip to content

Commit

Permalink
Add new feature with unstable resatrt
Browse files Browse the repository at this point in the history
(cherry picked from commit 592c65a)
  • Loading branch information
Andru Cherny authored and vmarchaud committed Aug 17, 2018
1 parent a668f57 commit c433f5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
45 changes: 30 additions & 15 deletions lib/God.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,30 +324,45 @@ God.handleExit = function handleExit(clu, exit_code, kill_signal) {
var min_uptime = typeof(proc.pm2_env.min_uptime) !== 'undefined' ? proc.pm2_env.min_uptime : 1000;
var max_restarts = typeof(proc.pm2_env.max_restarts) !== 'undefined' ? proc.pm2_env.max_restarts : 16;


//Only if process restart in repiting restart count ++ if process not run minimal uptime
if ((Date.now() - proc.pm2_env.pm_uptime) < min_uptime) {
// Increment unstable restart
proc.pm2_env.repiting_reset += 1;
} else {
proc.pm2_env.repiting_reset = 0;
}



//On boot
if ((Date.now() - proc.pm2_env.created_at) < (min_uptime * max_restarts)) {
if ((Date.now() - proc.pm2_env.pm_uptime) < min_uptime) {
// Increment unstable restart
proc.pm2_env.unstable_restarts += 1;
} else {
proc.pm2_env.repiting_reset = 0;
}
}

if (proc.pm2_env.unstable_restarts >= max_restarts) {
// Too many unstable restart in less than 15 seconds
// Set the process as 'ERRORED'
// And stop restarting it
proc.pm2_env.status = cst.ERRORED_STATUS;
proc.process.pid = 0;

console.log('Script %s had too many unstable restarts (%d). Stopped. %j',
proc.pm2_env.pm_exec_path,
proc.pm2_env.unstable_restarts,
proc.pm2_env.status);
if (proc.pm2_env.unstable_restarts >= max_restarts || proc.pm2_env.repiting_reset>= max_restarts) {
// Too many unstable restart in less than 15 seconds
// Set the process as 'ERRORED'
// And stop restarting it
proc.pm2_env.status = cst.ERRORED_STATUS;
proc.process.pid = 0;

God.notify('restart overlimit', proc);
console.log('Script %s had too many unstable restarts (%d). Stopped. %j',
proc.pm2_env.pm_exec_path,
proc.pm2_env.unstable_restarts,
proc.pm2_env.status);

proc.pm2_env.unstable_restarts = 0;
proc.pm2_env.created_at = null;
overlimit = true;
}
God.notify('restart overlimit', proc);

proc.pm2_env.unstable_restarts = 0;
proc.pm2_env.created_at = null;
overlimit = true;
}

if (typeof(exit_code) !== 'undefined') proc.pm2_env.exit_code = exit_code;
Expand Down
1 change: 1 addition & 0 deletions lib/God/ActionMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ module.exports = function(God) {
God.clusters_db[id].pm2_env.created_at = Utility.getDate();
God.clusters_db[id].pm2_env.unstable_restarts = 0;
God.clusters_db[id].pm2_env.restart_time = 0;
God.clusters_db[id].pm2_env.repiting_reset = 0;

return cb(null, God.getFormatedProcesses());
};
Expand Down

0 comments on commit c433f5f

Please sign in to comment.