Repository navigation
feat: support graceful shutdown and restart - #1227
Merged
Merged
Conversation
✅ Live Preview ready!
|
|
I'm so excited about this feature, I hope it will be released soon! |
Codecov Report
@@ Coverage Diff @@
## main #1227 +/- ##
==========================================
- Coverage 77.30% 77.30% -0.01%
==========================================
Files 67 67
Lines 6712 6729 +17
Branches 689 691 +2
==========================================
+ Hits 5189 5202 +13
- Misses 1522 1526 +4
Partials 1 1
|
pi0
approved these changes
May 16, 2023
Member
|
Thanks! Followup issue for docs #1243 |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Linked issue
Resolves #1224
❓ Type of change
📚 Description
Introducing Graceful Shutdown to Nitro Server
This pull request brings the addition of a graceful shutdown feature to the
node-cluster,node-server, andnitro-devpresets. This enhancement ensures all ongoing requests are successfully processed before the server terminates, minimizing the risk of sudden connection losses. Furthermore, it allows developers to integrate their cleanup routines such as closing database connections prior to server halt.We have introduced four new environment variables for customizing this behavior:
NITRO_SHUTDOWN_DISABLED- Disables graceful shutdown feature if setNITRO_SHUTDOWN_SIGNALS- Allows you to specify which signals should be handled. Each signal should be separated with a space. Defaults toSIGINT SIGTERM.NITRO_SHUTDOWN_TIMEOUT- Sets the amount of time (in milliseconds) before a forced shutdown occurs. Defaults to30000milliseconds.NITRO_SHUTDOWN_NO_FORCE_EXIT- If it's set, the process will simply let the event loop clear without exiting the process instead ofprocess.exit(0)at the end of the shutdown process.Example of Graceful Shutdown
We've also added an example (examples/graceful-shutdown) demonstrating how to utilize these new features in a Nitro server application. The example shows the server awaiting the completion of the HTTP response before initiating the shutdown process and handling server shutdowns gracefully.
To start the server, use the following command:
Then, send an HTTP request with
curl localhost:3000. The server will respond after a three-second delay. During this delay, you can send a SIGINT signal to Nitro (usingCtrl+C). The server will then wait for the HTTP response to complete before initiating the shutdown process.Changes Notice
I have added the http-graceful-shutdown package to our dependencies to manage the shutdown process.
Cnsidering that
nitro-prerendererandnodepresets do not involve HTTP listening, I have decided not to add the graceful shutdown functionality to them. We believe that the responsibility of managing graceful shutdowns for these presets lies in the capable hands of developers if needed. We've also highlighted this point in the documentation.Challenges
Regarding(nitro-dev, I've identified that the graceful shutdown feature isn't functioning as anticipated. This issue appears to be related to the worker implementation insrc/dev/server.tsandsrc/cli/commands/dev.ts. I would appreciate it if someone could provide some guidance on resolving this issue.nitro-devshutdown support is implemented using cluster messaging since signals are not available to Node workers)While the graceful shutdown feature has successfully passed our initial tests, I have yet to add specific tests for it. I need assist in introducing relevant tests to verify its effectiveness.
I kindly invite @pi0 to review these changes. Your feedback on the
nitro-devissue and test coverage would be greatly appreciated.📝 Checklist