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

graceful scale down #1346

Closed
rashtao opened this issue Jun 12, 2015 · 4 comments
Closed

graceful scale down #1346

rashtao opened this issue Jun 12, 2015 · 4 comments

Comments

@rashtao
Copy link

rashtao commented Jun 12, 2015

When scaling down the children are killed and not gracefully shutdown.

Eg. In my app code I have:

    process.on('message', function (msg) {
        if (msg == 'shutdown') {
            // log something to file
        }
    });

I launch it with pm2:

$ pm2 start app.js -i 4
[PM2] Process app.js launched
┌──────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode    │ pid   │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 93 │ cluster │ 20165 │ online │ 0       │ 110s   │ 25.973 MB   │ disabled │
│ app      │ 94 │ cluster │ 20167 │ online │ 0       │ 110s   │ 25.969 MB   │ disabled │
│ app      │ 95 │ cluster │ 20207 │ online │ 0       │ 105s   │ 25.938 MB   │ disabled │
│ app      │ 96 │ cluster │ 20212 │ online │ 0       │ 105s   │ 25.938 MB   │ disabled │
└──────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘

and I give the command:

$ pm2 scale app 3

one child is terminated, but the message event is not fired. So I guess that the child is brutally killed.

The message events are correctly fired when I do:

$ pm2 gracefulReload app
[PM2] Reloading process by name app
[PM2] Process app succesfully reloaded
[PM2] Process app succesfully reloaded
[PM2] All processes reloaded

What am I missing?
Is it possible to "gracefullly" scale down?

@Fantus
Copy link

Fantus commented Jul 10, 2015

i do have the same issue, as i need to close some connection before the worker process gets killed. any suggestions?

@jtokoph
Copy link

jtokoph commented Jan 27, 2016

I know it's been a while since this was closed, but nobody gave any alternative for gracefulStop.

After a bit of digging I found a solution and wanted to post it here, as my Google searches brought me here first.

gracefulReload uses process.on('message') to catch the reload notification. The rest of the stops and restarts send a SIGINT as documented here: http://pm2.keymetrics.io/docs/usage/signals-clean-restart/

Simple solution is to call your same clean shutdown code in a SIGINT handler as documented at the same link. If you want your process to be given more than the default 1.6 seconds to cleanly shutdown, you'll need to set the PM2_KILL_TIMEOUT environment variable to the number of milliseconds you want.

@Unitech
Copy link
Owner

Unitech commented Oct 31, 2016

SIGINT is the way to go:

SIGINT handle now works as expected (it will be called every time the process get exited).

To try it:

$ npm install pm2@next -g
$ pm2 update

@Unitech Unitech closed this as completed Oct 31, 2016
@Unitech
Copy link
Owner

Unitech commented Nov 7, 2016

Patch available on pm2@2.1.4 (main):

$ npm install pm2 -g
$ pm2 update

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

5 participants