-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
process: add process.gracefulExit() #6175
Conversation
Provides a deferrable, async alternative to process.exit(). Whereas process.exit() exits the process as quickly as possible, process.gracefulExit(), by default, will not exit until the next tick of the event loop (scheduled by setImmediate). A callback function can be provided to customize the exit process and invoke the actual exit when desired or appropriate to do so. Exits with code `1` on the next tick of the event loop: ```js process.gracefulExit(1); ``` Exits with code '0' on `process.nextTick()`: ```js process.gracefulExit(1, (code, exit) => { console.log('exiting gracefully on next tick'); process.nextTick(() => exit(0)); }); ``` Immediately exit if the graceful exit is not completed within 10 seconds: ```js process.gracefulExit(0, 10000, (code, exit) => { // Wait, do nothing. don't call exit. }); ``` The implementation is such that `process.exit()` is still called, so process.on('exit') handlers will be invoked. In cluster/child-processes with IPC, this does *not* take the place of process.disconnect().
Isn't this something we can (should?) leave to userland? |
process.gracefulExit = function(code, timeout, cb) { | ||
if (process._exiting || process._exitingGracefully) | ||
return; | ||
process._exitingGracefully = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also set _exiting
?
I think this is something that can be done from userland. If we include it in core - I think it would be useful to add cleanup listeners process.on("gracefulExit", (cb) => {
// call cb to mark cleanup done
}); Or something like that. |
This is basically |
Basically scratched an itch that I had in a couple apps. I've had this around for a bit and wanted to at least float the idea. Not that concerned if it doesn't land, but had it on my todo list for a while to float it and see. |
I think this should probably be left to userland as well. |
Works for me! Thanks all! |
Checklist
Affected core subsystem(s)
process
Description of change
Provides a deferrable, async alternative to process.exit(). Whereas process.exit() exits the process as quickly as possible, process.gracefulExit(), by default, will not exit until the next tick of the event loop (scheduled by setImmediate). A callback function can be provided to customize the exit process and invoke the actual exit when desired or appropriate to do so.
Exits with code
1
on the next tick of the event loop:Exits with code '0' on
process.nextTick()
:Immediately exit if the graceful exit is not completed within 10 seconds:
The implementation is such that
process.exit()
is still called ultimately, so process.on('exit') handlers will be invoked.In cluster/child-processes with IPC, this does not take the place of process.disconnect().
Note: to a degree this is speculative and yes, it is possible for this to live in userland.