-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Adds asynchronous support #20
Conversation
This is not mentioned in the docs. |
|
The downsides with async must be more clearly documented. |
I think we should provide a reasonable default timeout. |
Doc Changesrequired param in docs, Given this feedback, I think it makes more sense to keep the exitHook API the way it is and make the Code Changes
I'll pull all these changes together and incrementally update the PR. Will put it into draft until all the changes are landed. Thank you for the detailed feedback. |
I think it should be a named export: import exitHook, {asyncExitHook} from 'exit-hook'; |
All great feedback, I'll return to draft and reopen once all the feedback is addressed. Planned:
|
* `exitHook.async` renamed to `asyncExitHook` * `exitHook.exit` renamed to `gracefulExit` * `minWait` changed to `minimumWait` * Adds example of async hook to readme * Removes hard-wraps * Simplifies Asynchronous Notes, and adds a tl;dr
I've made the suggested changes, save for the parameter ordering on I feel the API aligns closely with p-retry, where first argument to I've also made sure that our |
Yeah, it would be useful to have such a check. |
Alright, type tests added as part of this change for both |
Co-authored-by: Sindre Sorhus <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
Co-authored-by: Sindre Sorhus <[email protected]>
Thanks :) |
This is an attempt to add asynchronous support in a backward-compatible manner.
Fixes #1
API Changes
All changes are additive as new features. The original
exitHook
continues to work with its original API. This code change does check that you passed a function intoexitHook
, though this was already the assumed behavior.new
asyncExitHook
exported from the package with the following signature:new
gracefulExit
exported from the package with the following signature:type
Options
When the exit handler is run, all synchronous handlers are executed first, regardless of the order added. Attempting to exit synchronously with asynchronous exit handlers registered will create a console.error explaining that asynchronous shutdown should use
gracefulExit
instead ofprocess.exit
.Internals
callbacks
andasyncCallbacks
exitHook
was moved to an internaladdHook
function, allowing both async and non async versions to use the same code pathbeforeExit
in addition toSIGINT
andSIGTERM
and PM2 message, giving us an additional chance to attempt cleanupminimumWait
value will be used. After this time elapses, the shutdown operation will continueOriginal Post
API Changes
exitHook The default signature has an additional optional parameter
maxWait
, which is required if your hook is asynchronous.In this example, the
exitHook
handler will wait a maximum of 100ms for the registered hook to complete before forcibly terminating the process. A duration is required for all asynchronous hooks to ensure that the node process cannot be extended indefinitely.Internals
process.exit()
) and if they have asynchronous callbacks registered. If both conditions are true, we notify the user asynchronous callbacks will not be run and offer a remediation.beforeExit
events which can be asynchronous