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

Best way to shutdown on exit? #3

Closed
amcdnl opened this issue Mar 22, 2016 · 13 comments
Closed

Best way to shutdown on exit? #3

amcdnl opened this issue Mar 22, 2016 · 13 comments

Comments

@amcdnl
Copy link

amcdnl commented Mar 22, 2016

I'm trying to kill the process when the server exits, I'm thinking something like this:

  process.on('beforeExit', function() {
    server.stop();
  });

but it doesn't always work. This is probably sometime you need to handle internally too tho. Thoughts?

@dewe
Copy link
Owner

dewe commented Mar 23, 2016

Is it not stopping the process, or is it never calling the callback?

That code is just issuing a call to nginx and waiting for process to close. Probably there's better and more robust ways to do that, possibly involving keeping track of the PID file...

@amcdnl
Copy link
Author

amcdnl commented Mar 23, 2016

Yup, looked through the code and did not see anything crazy but in researching I did find this comment: https://nodejs.org/api/process.html#process_event_exit

Agreed, re: the pid.

@dewe
Copy link
Owner

dewe commented Mar 23, 2016

Hmm, yeah since nginx is in a separate process, it's kind of hard to be synchronous with that... I'll think about it for a while. If you come up with something, please let me know.

@amcdnl
Copy link
Author

amcdnl commented Mar 23, 2016

Will do!

@dewe
Copy link
Owner

dewe commented Mar 25, 2016

I've been trying to reproduce this issue, with no luck. When calling server.stop() in the exit event callback, the nginx server receives the correct signal and shuts down after a very short time. It's sort of a race condition: the node process might shutdown before nginx is done closing, but I haven't been observing any nginx processes left running for more than a few milliseconds.

One thing I found though: the exit event doesn't seem to be reliable. When killing node with Ctrl-C it just doesn't trigger an exit event. But if I call process.exit() it works like a charm...

@amcdnl Any thoughts on this? Did you verify that the exitevent really happens?

@amcdnl
Copy link
Author

amcdnl commented Mar 25, 2016

When killing node with Ctrl-C it just doesn't trigger an exit event. But if I call process.exit() it works like a charm...

I was using ctrl+c ... makes sense ... wonder why that is!

@dewe
Copy link
Owner

dewe commented Mar 25, 2016

For catching Ctrl-C and closing the server, use something like this (not tested):

process.on('exit', () => server.close());
process.on('SIGINT', () => process.exit());

@dewe
Copy link
Owner

dewe commented Mar 25, 2016

Found a bit more info here: nodejs/node#2853

@amcdnl
Copy link
Author

amcdnl commented Mar 28, 2016

Would be nice to incorporate this into the project. ;)

@amcdnl
Copy link
Author

amcdnl commented Mar 28, 2016

FYI -> server.stop() ;)

@amcdnl
Copy link
Author

amcdnl commented Mar 28, 2016

Note that even though the name of this function is process.kill, it is really just a signal sender, like the kill system call. The signal sent may do something other than kill the target process.

I'm on windows, could be my issue.

@amcdnl
Copy link
Author

amcdnl commented Mar 28, 2016

FYI: this seemed to do the trick:

  // http://stackoverflow.com/questions/5006821/nodejs-how-to-read-keystrokes-from-stdin
  var stdin = process.stdin;
  stdin.setRawMode(true);
  stdin.resume();
  stdin.setEncoding('utf8');
  stdin.on('data', function(key){
    // ctrl-c
    if (key === '\u0003') {
      console.log('EXITING!')
      server.stop(function() {
        process.exit();
      });
    }
  });

@amcdnl
Copy link
Author

amcdnl commented Mar 28, 2016

I'm closing this since got a work around.

@amcdnl amcdnl closed this as completed Mar 28, 2016
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

2 participants