fix: let's try to refactor shutdown again, again

This commit is contained in:
Andy Burke 2025-07-24 12:00:22 -07:00
parent 0b5f0c5d5e
commit a9b20fea40
4 changed files with 196 additions and 9 deletions

View file

@ -6,6 +6,8 @@
import * as colors from '@std/fmt/colors';
import * as path from '@std/path';
const EVENTS_TO_SHUTDOWN_ON: Deno.Signal[] = ['SIGTERM', 'SIGINT'];
const DEFAULT_HANDLER_DIRECTORIES = [import.meta.resolve('./handlers')];
/** A `HANDLER` must take a `Request` and return a `Response` if it can handle it. */
@ -17,7 +19,7 @@ type LOGGER = (request: Request, response: Response, processing_time: number) =>
/** A `HANDLER_MODULE` must export a default method and may export an unload method to be called at shutdown. */
interface HANDLER_MODULE {
default: HANDLER;
unload?: () => void;
unload?: () => void | Promise<void>;
}
/**
@ -178,12 +180,15 @@ export class SERVER {
);
this.shutdown_binding = this.stop.bind(this);
Deno.addSignalListener('SIGTERM', this.shutdown_binding);
Deno.addSignalListener('SIGINT', this.shutdown_binding);
if (this.options.watch) {
Deno.watchFs;
for (const event of EVENTS_TO_SHUTDOWN_ON) {
Deno.addSignalListener(event, this.shutdown_binding);
}
// if (this.options.watch) {
// Deno.watchFs;
// }
return this;
}
@ -194,8 +199,9 @@ export class SERVER {
if (this.server) {
this.server.finished.finally(() => {
if (this.shutdown_binding) {
Deno.removeSignalListener('SIGTERM', this.shutdown_binding);
Deno.removeSignalListener('SIGINT', this.shutdown_binding);
for (const event of EVENTS_TO_SHUTDOWN_ON) {
Deno.removeSignalListener(event, this.shutdown_binding);
}
}
this.shutdown_binding = undefined;
@ -203,6 +209,7 @@ export class SERVER {
this.controller?.abort();
await this.server.shutdown();
await this.server.finished;
}
if (this.original_directory) {
@ -215,7 +222,7 @@ export class SERVER {
for (const handler_module of this.handlers) {
if (typeof handler_module.unload === 'function') {
handler_module.unload();
await handler_module.unload();
}
}
this.handlers = [];