Skip to content

Commit

Permalink
fix: reinforce pm2-runtime auto exit strategy #3567 #3206
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed Mar 28, 2018
1 parent 438e303 commit e09cdba
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/binaries/Runtime4Docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var Log = require('../../lib/API/Log');
var cst = require('../../constants.js');
var pkg = require('../../package.json');
var path = require('path');
var DEFAULT_FAIL_COUNT = 3;

process.env.PM2_DISCRETE_MODE = true;

Expand Down Expand Up @@ -121,8 +122,11 @@ var Runtime = {
Runtime.pm2.web(port);
}

if (commander.autoExit)
Runtime.autoExitWorker();
if (commander.autoExit) {
setTimeout(function() {
Runtime.autoExitWorker();
}, 4000);
}

// For Testing purpose (allow to auto exit CLI)
if (process.env.PM2_RUNTIME_DEBUG)
Expand All @@ -138,20 +142,23 @@ var Runtime = {
/**
* Exit runtime mgmt
*/
exit : function() {
exit : function(code) {
if (!this.pm2) return process.exit(1);

this.pm2.kill(function() {
process.exit(0);
process.exit(code || 0);
});
},

/**
* Exit current PM2 instance if 0 app is online
* function activated via --auto-exit
*/
autoExitWorker : function() {
var interval = 1000;
autoExitWorker : function(fail_count) {
var interval = 2000;

if (typeof(fail_count) =='undefined')
fail_count = DEFAULT_FAIL_COUNT;

var timer = setTimeout(function () {
Runtime.pm2.list(function (err, apps) {
Expand All @@ -170,8 +177,10 @@ var Runtime = {
});

if (appOnline === 0) {
console.log('0 application online, exiting');
return Runtime.exit();
console.log('0 application online, retry =', fail_count);
if (fail_count <= 0)
return Runtime.exit(2);
return Runtime.autoExitWorker(--fail_count);
}

Runtime.autoExitWorker();
Expand Down

0 comments on commit e09cdba

Please sign in to comment.