(docker) better auto exit trigger (#2861)

This commit is contained in:
vmarchaud 2017-05-21 00:49:09 +02:00
parent 64fe775ce3
commit da7f7698b9

View File

@ -118,23 +118,42 @@ function exitPM2() {
* function activated via --auto-exit
*/
function autoExit() {
setTimeout(function() {
pm2.list(function(err, apps) {
if (err) console.error(err.stack || err);
var interval = 3000;
var aliveInterval = interval * 1.5;
var online_count = 0;
setTimeout(function () {
var alive = false
var aliveTimer = setTimeout(function () {
if (!alive) {
console.error('PM2 Daemon is dead');
process.exit(1);
}
}, aliveInterval);
apps.forEach(function(app) {
if (app.pm2_env.status == cst.ONLINE_STATUS ||
app.pm2_env.status == cst.LAUNCHING_STATUS)
online_count++;
pm2.list(function (err, apps) {
if (err) {
console.log('pm2.list got error')
console.error(err);
exitPM2();
}
clearTimeout(aliveTimer);
alive = true;
var appOnline = 0;
apps.forEach(function (app) {
if (app.pm2_env.status === cst.ONLINE_STATUS ||
app.pm2_env.status === cst.LAUNCHING_STATUS) {
appOnline++;
}
});
if (online_count == 0) {
if (appOnline === 0) {
console.log('0 application online, exiting');
exitPM2();
}
autoExit();
});
}, 3000);
}, interval);
}