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

feat: graceful shutdown and systemd socket activation in adapter-node #11653

Merged
merged 51 commits into from
Mar 8, 2024

Conversation

karimfromjordan
Copy link
Contributor

@karimfromjordan karimfromjordan commented Jan 17, 2024

Closes #10134
Closes #9540

This is an alternative PR to #10977 that addresses all the points mentioned there, mainly:

  1. simpler code by allowing only one file descriptor (fd 3)
  2. handling the automatic shutdown of the server which is controlled by a new environment variable IDLE_TIMEOUT
  3. add documentation

It also gracefully shuts down the app by default. A new SHUTDOWN_TIMEOUT environment variable controls how long the app should wait for all remaining requests to finish before the server is forcibly closed.

If you are on Linux or have access to a Linux server you can try this out yourself by installing the adapter into a new/existing SvelteKit project (requires npm, pnpm doesn't work) using:

npm install -D 'https://gitpkg.now.sh/karimfromjordan/sveltejs-kit/packages/adapter-node?systemd-socket-activation-build'

Then create a service and socket unit:

# /etc/systemd/system/myapp.service

[Service]
Environment=NODE_ENV=production IDLE_TIMEOUT=10
ExecStart=/usr/bin/node /usr/bin/myapp/build
# /etc/systemd/system/myapp.socket

[Socket]
ListenStream=3000

[Install]
WantedBy=sockets.target

Reload the daemon:

sudo systemctl daemon-reload

Start the socket:

sudo systemctl start myapp.socket

Navigate to localhost:3000. The app should then start as soon as the first request is made. If IDLE_TIMEOUT is set it will automatically terminate the app after being inactive for IDLE_TIMEOUT seconds. You can verify this behavior by following the logs using sudo journalctl -u myapp.service -f.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: /~https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Jan 17, 2024

🦋 Changeset detected

Latest commit: d8c8a90

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-node Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@karimfromjordan karimfromjordan changed the title adapter-node: add systemd socket activation feat: add systemd socket activation to adapter-node Jan 18, 2024
Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

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

LGTM but I had one question

packages/adapter-node/src/index.js Show resolved Hide resolved
@karimfromjordan karimfromjordan changed the title feat: add systemd socket activation to adapter-node feat: graceful shutdown and systemd socket activation in adapter-node Jan 21, 2024
Copy link
Member

@Conduitry Conduitry left a comment

Choose a reason for hiding this comment

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

Thank you for your patience during the protracted review process - let's ship it!

@benmccann benmccann merged commit 63c7f79 into sveltejs:main Mar 8, 2024
12 checks passed
@github-actions github-actions bot mentioned this pull request Mar 8, 2024
@karimfromjordan karimfromjordan deleted the systemd-socket-activation branch March 8, 2024 17:16
@weepy
Copy link

weepy commented Mar 15, 2024

I'm having issues with this - in particular when connecting to a database (mongodb). Out of the box it works fine via "npm run dev", but hangs if I build with adapter-node and run with node.
If I put in some code to close the db connection on SIGINT then it exits ok via adapter-node, but hangs on exit if use "npm run dev"

example code:
function closeMongoConnection() {
    client.close()
}
process.on('SIGINT', closeMongoConnection);
process.on('SIGTERM', closeMongoConnection);

Maybe I'm doing something dumb, but if there some best practice here ?

@karimfromjordan
Copy link
Contributor Author

Let's please keep the conversation about this in one thread. I've already replied in the other thread and I saw your follow up comment but it can take a few days for me to get back. You can also ask on the Svelte Discord server and ping me there

@benmccann
Copy link
Member

Just in case others come across this, here's the other thread: #9540 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

adapter-node: systemd socket activation support Close adapter-node server gracefully on SIGINT and SIGTERM
7 participants