Fix shutdown behaviour (#2239)

* Handle SIGTERM signal

* Use constants

* Styling

* Prettier

* setTimeout

* Prettier

* Display version

* Display version

* Prettier
This commit is contained in:
Kroese 2023-11-28 00:37:37 +01:00 committed by GitHub
parent 1b20bf7807
commit aafcfaf649
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 6 deletions

View File

@ -1,5 +1,6 @@
import { setupDateFns } from "@utils/app";
import { getStaticDir } from "@utils/env";
import { VERSION } from "../shared/version";
import express from "express";
import path from "path";
import process from "process";
@ -52,12 +53,41 @@ server.get("/css/code-themes/:name", CodeThemeHandler);
server.get("/css/themelist", ThemesListHandler);
server.get("/*", CatchAllHandler);
server.listen(Number(port), hostname, () => {
const listener = server.listen(Number(port), hostname, () => {
setupDateFns();
console.log(`http://${hostname}:${port}`);
console.log(
`Lemmy-ui v${VERSION} started listening on http://${hostname}:${port}`,
);
});
process.on("SIGINT", () => {
console.info("Interrupted");
process.exit(0);
});
const signals = {
SIGHUP: 1,
SIGINT: 2,
SIGTERM: 15,
};
const exit_signal = 128; // Fatal error signal code on Linux systems
const exit_timeout = 8000; // Because Docker SIGTERMs after 10 secs
const shutdown = (signal, value) => {
// TODO: Should set a flag here for the listener to reject any further
// incoming connections with a HTTP 503 error while shutting down.
// Otherwise the connection count may not reach zero before timeout.
listener.close(() => {
console.log(`Lemmy stopped by ${signal} with value ${value}`);
process.exit(exit_signal + value);
});
setTimeout(() => {
console.error(
`Could not close all connections in time, forcing shutdown because of ${signal}...`,
);
process.exit(exit_signal + value);
}, exit_timeout);
};
for (const [signal, value] of Object.entries(signals)) {
process.on(signal, () => {
console.log(`Process received a ${signal} signal, shutting down...`);
shutdown(signal, value);
});
}