sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
17.8k stars 1.78k forks source link

feat: adapter-node shutdown event #12153

Open karimfromjordan opened 3 weeks ago

karimfromjordan commented 3 weeks ago

This is a small improvement and the missing piece to the recent graceful shutdown feature. After the server has closed gracefully Node will emit the exit event. As others have pointed out however if you have something running in the background the app will continue to run and exit will never be emitted. The exit event isn't ideal anyway because it will close your app as soon as possible so we can't do any custom async cleanup work in a process.on('exit', () => { ... }) handler. That means calling process.exit() to manually emit the exit event isn't an option. It defeats the point of a graceful shutdown. This PR emits a custom event instead that users of adapter-node can listen to and use to perform any kind of cleanup work right after the HTTP server has closed.

process.on('sveltekit:shutdown', async (reason) => {
  await db.close();
  await jobs.stop();
  process.exit();
});

We briefly discussed the option of exporting a function to register a hook instead but with the way adapters work this doesn't seem to be possible today.


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

Tests

Changesets

Edits

changeset-bot[bot] commented 3 weeks ago

🦋 Changeset detected

Latest commit: 716e2fd3b6af4317bd03e1ad4186fcec4b260cb6

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 | Minor |

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