Skip to content

Commit

Permalink
πŸ› server: fix graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Nov 26, 2024
1 parent 4422926 commit 072cf1c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ app.onError((error, c) => {
return c.text(error instanceof Error ? error.message : String(error), 500);
});

serve(app);
const server = serve(app);

["SIGINT", "SIGTERM"].map((code) =>
process.on(code, () => {
Promise.allSettled([close(), closeAndFlush()]).catch(() => undefined);
}),
process.on(code, () =>
server.close((error) => {
Promise.allSettled([close(), closeAndFlush()])
.then((results) => {
process.exit(error || results.some((result) => result.status === "rejected") ? 1 : 0); // eslint-disable-line n/no-process-exit
})
.catch(() => undefined);
}),
),
);

declare module "hono" {
Expand Down

0 comments on commit 072cf1c

Please sign in to comment.