Skip to content

feat: support graceful shutdown and restart - #1227

Merged
pi0 merged 10 commits into
nitrojs:mainfrom
trylovetom:main
May 16, 2023
Merged

pi0 merged 10 commits into
nitrojs:mainfrom
trylovetom:main

Conversation

@trylovetom

@trylovetom trylovetom commented May 11, 2023 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Resolves #1224

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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, and nitro-dev presets. 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 set
  • NITRO_SHUTDOWN_SIGNALS - Allows you to specify which signals should be handled. Each signal should be separated with a space. Defaults to SIGINT SIGTERM.
  • NITRO_SHUTDOWN_TIMEOUT - Sets the amount of time (in milliseconds) before a forced shutdown occurs. Defaults to 30000 milliseconds.
  • NITRO_SHUTDOWN_NO_FORCE_EXIT - If it's set, the process will simply let the event loop clear without exiting the process instead of process.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:

NITRO_PRESET=node-cluster pnpm nitro build examples/graceful-shutdown
node examples/graceful-shutdown/.output/server/index.mjs

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 (using Ctrl+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-prerenderer and node presets 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 in src/dev/server.ts and src/cli/commands/dev.ts. I would appreciate it if someone could provide some guidance on resolving this issue. (nitro-dev shutdown 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-dev issue and test coverage would be greatly appreciated.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@ghost

ghost commented May 11, 2023 •

Copy link
Copy Markdown

✅ Live Preview ready!

Name Edit Preview Latest Commit
nitro Edit on Studio ↗︎ View Live Preview 5f9095a

@zhuoqi-chen

Copy link
Copy Markdown

I'm so excited about this feature, I hope it will be released soon!

@codecov

codecov Bot commented May 16, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #1227 (20a4dfc) into main (44bf0e7) will decrease coverage by 0.01%.
The diff coverage is 80.00%.

❗ Current head 20a4dfc differs from pull request most recent head 5f9095a. Consider uploading reports for the commit 5f9095a to get more accurate results

@@            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              
Impacted Files Coverage Δ
src/dev/server.ts 85.32% <80.00%> (-0.63%) ⬇️

@pi0 pi0 changed the title [WIP] feat: support runtime hooks for graceful shutdown/restart feat: support graceful shutdown and restart May 16, 2023
@pi0 pi0 self-assigned this May 16, 2023
@pi0
pi0 merged commit 3cdaf08 into nitrojs:main May 16, 2023
@pi0 pi0 mentioned this pull request May 16, 2023
@pi0

pi0 commented May 16, 2023 •

Copy link
Copy Markdown
Member

Thanks! Followup issue for docs #1243

This branch was successfully deployed

1 active deployment
Preview — 5f9095a3 Deployed May 16, 2023 by vercel[bot]
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

Successfully merging this pull request may close these issues.

Support runtime hooks for graceful shutdown/restart

3 participants