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

process: add process.gracefulExit() #6175

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Apr 13, 2016

Checklist
  • tests and code linting passes
  • a test and/or benchmark is included
  • documentation is changed or added
  • the commit message follows commit guidelines
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:

process.gracefulExit(1);

Exits with code '0' on process.nextTick():

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:

process.gracefulExit(0, 10000, (code, exit) => {
  // Wait, do nothing. don't call exit.
});

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.

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().
@jasnell jasnell added semver-minor PRs that contain new features and should be released in the next minor version. process Issues and PRs related to the process subsystem. labels Apr 13, 2016
@ronkorving
Copy link
Contributor

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also set _exiting?

@benjamingr
Copy link
Member

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.

@bnoordhuis
Copy link
Member

This is basically setImmediate(() => process.exit(exitCode)), isn't it? What's the point of adding a method for that?

@jasnell
Copy link
Member Author

jasnell commented Apr 13, 2016

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.

@mscdex
Copy link
Contributor

mscdex commented Apr 13, 2016

I think this should probably be left to userland as well.

@jasnell
Copy link
Member Author

jasnell commented Apr 13, 2016

Works for me! Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
process Issues and PRs related to the process subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants