11ty / eleventy-dev-server

A minimal generic web-development web server.
100 stars 15 forks source link

Make shutdown process graceful #84

Open VividVisions opened 2 weeks ago

VividVisions commented 2 weeks ago

Currently, the web server, the update (WebSocket) server and the Chokidar watcher are closed by calling their respective close() functions, disregarding their callbacks/returned Promises. Furthermore process.exit(); is called immediately after, preventing a graceful shutdown altogether.

I'd suggest to make the close() methods of EleventyDevServer and Cli asynchronous and to change process.exit(); to process.exitCode = 0; in case of a SIGINT signal. This would make the shutdown process clean and graceful.

I'd happily provide a PR!

Note: These changes would also necessitate small adjustments in Eleventy itself, i.e. making stopWatch() of Eleventy asynchronous and exchanging process.exit(); there as well.

Background: We've built a custom server which wraps around `EleventyDevServer` and needs a bit of cleanup during shutdown. Due to the current shutdown process, we couldn't use the `close` event of `http.Server` which either doesn't even get emitted or isn't waited for. We had to implement ugly workarounds/hacks to make this work. ;)
zachleat commented 2 weeks ago

Happy to merge this but any API breaking changes in Eleventy core and we might be able to sneak it in to 3.0 if it happens very soon (like, probably this week)

VividVisions commented 2 weeks ago

Created a PR. Please let me know if I should change anything. I'll provide a PR for the changes in Eleventy tomorrow.