Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using --auto-exit, pm2-docker process won't quit when PM2 daemon is crashed #2861

Closed
nickeljew opened this issue Apr 30, 2017 · 1 comment
Closed

Comments

@nickeljew
Copy link

nickeljew commented Apr 30, 2017

PM2 version: v2.4.6

Option --auto-exit only works when PM2 daemon is running well.
However, in my cases, PM2 daemon crashed for some reason, and pm2-docker process remained running. This is not normal and make my docker container still alive but no application is actually working.

I debugged in the source code of pm2-docker.js and found out.
Original codes

function autoExit() {
  setTimeout(function() {
    pm2.list(function(err, apps) {
      if (err) console.error(err.stack || err);

      var online_count = 0;

      apps.forEach(function(app) {
        if (app.pm2_env.status == cst.ONLINE_STATUS ||
            app.pm2_env.status == cst.LAUNCHING_STATUS)
          online_count++;
      });

      if (online_count == 0) {
        console.log('0 application online, exiting');
        exitPM2();
      }
      autoExit();
    });
  }, 3000);
}

When PM2 daemon is dead, pm2.list won't trigger any exception but the callback function will not be called too, so pm2-docker process remains without any actual service running.

Here is my modification:

function autoExit() {
  var interval = 3000;
  var aliveInterval = interval * 1.5;

  setTimeout(function() {
    var alive = false
    var aliveTimer = setTimeout(function() {
      if (!alive) {
        console.error('PM2 Daemon is dead');
        process.exit(1);
      }
    }, aliveInterval);

    pm2.list(function(err, apps) {
      if (err) {
        console.log('pm2.list got error')
        console.error(err.stack || err);
        exitPM2();
      }

      clearTimeout(aliveTimer);
      alive = true;

      var online_count = 0;

      apps.forEach(function(app) {
        if (app.pm2_env.status == cst.ONLINE_STATUS ||
            app.pm2_env.status == cst.LAUNCHING_STATUS)
          online_count++;
      });

      if (online_count == 0) {
        console.log('0 application online, exiting');
        exitPM2();
      }
      autoExit();
    });
  }, interval);
}
@vmarchaud
Copy link
Contributor

Published under PM2 2.5

inerc pushed a commit to inerc/pm2 that referenced this issue Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants