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

Windows gracefull stop not working #4469

Closed
tony-0tis opened this issue Oct 23, 2019 · 3 comments
Closed

Windows gracefull stop not working #4469

tony-0tis opened this issue Oct 23, 2019 · 3 comments

Comments

@tony-0tis
Copy link

What's going wrong?

Windows graceful stop not working

How could we reproduce this issue?

Run pm2 on windows with cluster or fork mode

Supporting information

http://pm2.keymetrics.io/docs/usage/signals-clean-restart/#comment-4662727595

--- PM2 report ----------------------------------------------------------------
Date                 : Wed Oct 23 2019 15:37:07 GMT+0300 (GMT+03:00)
===============================================================================
--- Daemon --------------------------------------------------------------------
pm2d version         : 4.1.2
node version         : 10.15.3
node path            : C:/Program Files/nodejs/node
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Den\AppData\Local\Yarn\Data\global\node_modules\pm2\lib\Daemon.js
argv0                : node
user                 : undefined
uid                  : N/A
gid                  : N/A
uptime               : 10min
===============================================================================
--- CLI -----------------------------------------------------------------------
local pm2            : 4.1.2
node version         : 10.15.3
node path            : C:/Program Files/nodejs/node
argv                 : C:\Program Files\nodejs\node.exe,C:\Users\Den\AppData\Local\Yarn\Data\global\node_modules\pm2\bin\pm2,report
argv0                : C:\Program Files\nodejs\node.exe
user                 : Den
===============================================================================
--- System info ---------------------------------------------------------------
arch                 : x64
platform             : win32
type                 : Windows_NT
cpus                 : AMD Ryzen 5 1600X Six-Core Processor
cpus nb              : 12
freemem              : 10833846272
totalmem             : 17109442560
home                 : C:\Users\Den
===============================================================================
--- PM2 list ------------------------------------------------------------------
┌────┬─────────────────────────┬─────────┬─────────┬──────────┬────────┬──────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name                    │ version │ mode    │ pid      │ uptime │ ↺    │ status   │ cpu      │ mem      │ user     │ watching │
├────┼─────────────────────────┼─────────┼─────────┼──────────┼────────┼──────┼──────────┼──────────┼──────────┼──────────┼──────────┤
│ 1  │ cron                    │ 1.0.0   │ fork    │ 7928     │ 6m     │ 7    │ online   │ 0%       │ 46.8mb   │ Den      │ enabled  │
│ 0  │ mine                    │ 1.0.0   │ cluster │ 7312     │ 6m     │ 13   │ online   │ 0%       │ 46.2mb   │ Den      │ enabled  │
└────┴─────────────────────────┴─────────┴─────────┴──────────┴────────┴──────┴──────────┴──────────┴──────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs ---------------------------------------------------------------
C:\Users\Den\.pm2\pm2.log last 20 lines:
PM2        | For help, see: https://nodejs.org/en/docs/inspector
PM2        | 2019-10-23T15:30:33: PM2 log: App [mine:0] online
PM2        | 2019-10-23T15:30:35: PM2 log: App [mine:0] online
PM2        | 2019-10-23T15:30:36: PM2 log: App [mine:0] online
PM2        | 2019-10-23T15:30:38: PM2 log: App [mine:0] online
PM2        | 2019-10-23T15:30:51: PM2 log: Change detected on path functions.js for app cron - restarting
PM2        | 2019-10-23T15:30:51: PM2 log: Change detected on path functions.js for app mine - restarting
PM2        | 2019-10-23T15:30:51: PM2 log: Stopping app:cron id:1
PM2        | 2019-10-23T15:30:51: PM2 log: Stopping app:mine id:0
PM2        | 2019-10-23T15:30:51: PM2 log: App name:mine id:0 disconnected
PM2        | 2019-10-23T15:30:51: PM2 log: App [cron:1] exited with code [1] via signal [SIGINT]
PM2        | 2019-10-23T15:30:51: PM2 log: App [mine:0] exited with code [1] via signal [SIGINT]
PM2        | 2019-10-23T15:30:51: PM2 log: pid=10796 msg=process killed
PM2        | 2019-10-23T15:30:51: PM2 log: App [cron:1] starting in -fork mode-
PM2        | 2019-10-23T15:30:51: PM2 log: pid=8004 msg=process killed
PM2        | 2019-10-23T15:30:51: PM2 log: App [mine:0] starting in -cluster mode-
PM2        | 2019-10-23T15:30:51: PM2 log: App [cron:1] online
PM2        | Debugger listening on ws://127.0.0.1:9243/82308d40-b22c-47b2-8c50-740d0c5fb95b
PM2        | For help, see: https://nodejs.org/en/docs/inspector
PM2        | 2019-10-23T15:30:57: PM2 log: App [mine:0] online
@tony-0tis
Copy link
Author

tony-0tis commented Oct 23, 2019

Fast decision, working for cluster mode and next code in your app:

process.on('message', function (msg) {
    if (msg == 'shutdown') {
        /*your code*/
        process.exit(0)
    }
});

In file lib/God/ActionMethods.js in God.stopProcessId function before call God.killProcess add code:

if(process.platform == 'win32' && proc.process.send){
  return God.softKillProcess(proc, function (err){
    proc.pm2_env.status = cst.STOPPED_STATUS;

    God.notify('exit', proc);

    if (err && err.type && err.type === 'timeout') {
      console.error('app=%s id=%d pid=%s could not be stopped',
                    proc.pm2_env.name,
                    proc.pm2_env.pm_id,
                    proc.process.pid);
      proc.pm2_env.status = cst.ERRORED_STATUS;
      return cb(null, God.getFormatedProcess(id));
    }

    if (proc.pm2_env.pm_id.toString().indexOf('_old_') !== 0) {
      try {
        fs.unlinkSync(proc.pm2_env.pm_pid_path);
      } catch (e) {}
    }

    if (proc.pm2_env.axm_actions) proc.pm2_env.axm_actions = [];
    if (proc.pm2_env.axm_monitor) proc.pm2_env.axm_monitor = {};

    proc.process.pid = 0;
    return cb(null, God.getFormatedProcess(id));
  });
}

Then in file lib/God/Methods.js add new function:

/**
* Description
* @method softKillProcess
* @param Object proc
* @param function cb
* @return CallExpression
*/
God.softKillProcess = function(proc, cb) {
    console.log('Send shutdown message for', proc.process.pid);
    proc.process.send('shutdown');
    God.processIsDead(proc.process.pid, proc.pm2_env, cb);
};

Do not forget add kill_timeout into your ecosystem.config.js file or by default the application will be closed using SIGKILL after 1600ms as indicated in constants.js.

@tony-0tis
Copy link
Author

tony-0tis commented Oct 23, 2019

I think for my code you can add a check for some attribute in the ecosystem.config.js, for example sendMessageBeforeShutdown: true, and add the appropriate check in the modified code in the lib/God/ActionMethods.js:

if(process.platform == 'win32' && proc.process.send && proc.pm2_env.sendMessageBeforeShutdown){

And add new attribute here indicating that only works for cluster mode.

@tony-0tis
Copy link
Author

Issue can be closed because changes was been merged into master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant