-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: document shutdown config option for @astrojs/node #14535
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
Open
WhatCats
wants to merge
2
commits into
withastro:main
Choose a base branch
from
WhatCats:docs/node-shutdown-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,6 +185,69 @@ export default defineConfig({ | |
| }); | ||
| ``` | ||
|
|
||
| ### `shutdown` | ||
|
|
||
| <p> | ||
| **Type:** `{ timeout?: number; exit?: boolean }` <br /> | ||
| **Default:** `{ timeout: 10000, exit: false }`<br /> | ||
| <Since pkg="@astrojs/node" v="11.2.0" /> | ||
| </p> | ||
|
|
||
| Controls graceful shutdown behavior of the [`standalone`](#mode) server when it receives a `SIGTERM` or `SIGINT` signal (e.g. when a host stops or restarts the process). The server stops accepting new connections but waits for active requests to finish before closing. | ||
|
|
||
| The following example configures a 30-second shutdown timeout and forces the process to exit once the shutdown is complete: | ||
|
|
||
| ```js title="astro.config.mjs" {7-10} | ||
| import { defineConfig } from 'astro/config'; | ||
| import node from '@astrojs/node'; | ||
|
|
||
| export default defineConfig({ | ||
| adapter: node({ | ||
| mode: 'standalone', | ||
| shutdown: { | ||
| timeout: 30 * 1000, // wait up to 30 seconds | ||
| exit: true, | ||
| }, | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
||
| #### `shutdown.timeout` | ||
|
|
||
| <p> | ||
| **Type:** `number`<br /> | ||
| **Default:** `10000` | ||
| </p> | ||
|
|
||
| The duration in milliseconds to wait for active requests to finish before force-closing remaining connections. Set to `0` to force-close immediately, or `Infinity` to wait indefinitely. | ||
|
|
||
| #### `shutdown.exit` | ||
|
|
||
| <p> | ||
| **Type:** `boolean`<br /> | ||
| **Default:** `false` | ||
| </p> | ||
|
|
||
| Controls whether to call `process.exit()` once shutdown completes. By default, the process exits naturally when the event loop is empty, allowing other signal listeners (e.g. database disconnect handlers) to finish before closing. Set to `true` to force the process to exit even if open timers or connections remain. | ||
|
|
||
| If the process does not exit after receiving a shutdown signal, check the adapter's logs for a `Shutdown complete.` message. This confirms that the server itself closed successfully, so something else in the process is keeping the event loop active. | ||
|
|
||
| Register a separate [`SIGTERM`/`SIGINT` listener](https://nodejs.org/api/process.html#signal-events) in the application to close any other open resources, such as a database connection, when the server shuts down: | ||
|
|
||
| ```js | ||
| process.on('SIGINT', () => db.close()); | ||
| process.on('SIGTERM', () => db.close()); | ||
| ``` | ||
|
|
||
| A `setInterval()` or `setTimeout()` also keeps the event loop open, and therefore the process running. This is often unintentional. Call `.unref()` on the timer so it does not block shutdown: | ||
|
|
||
| ```js | ||
| setInterval(() => {}, 1000).unref(); | ||
| setTimeout(() => {}, 1000).unref(); | ||
| ``` | ||
|
|
||
| To avoid tracking down every open resource, set `exit` to `true` and let the adapter force the process to exit once its own shutdown completes. | ||
|
|
||
| ## Usage | ||
|
|
||
| First, [performing a build](/en/guides/deploy/#building-your-site-locally). Depending on which `mode` selected (see above) follow the appropriate steps below: | ||
|
|
@@ -328,7 +391,7 @@ When self-hosting, you can load environment variables through CLI commands or co | |
|
|
||
| ENV DB_HOST=... | ||
| ENV DB_PASSWORD=... | ||
| CMD node ./dist/server/entry.mjs | ||
| CMD ["node", "./dist/server/entry.mjs"] | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exec form so Node runs as PID 1 and receives |
||
| ``` | ||
| </TabItem> | ||
| </Tabs> | ||
Oops, something went wrong.
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.
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.
I moved your the nested properties description to their own subheadings. And I tried to tighten a bit the wording to be more direct. Feel free to tell if any important information is missing.