fixed expressjs shutdown taking a long time

This commit is contained in:
Ludwig Lehnert
2026-01-12 18:24:14 +01:00
parent 2aa23f7b5b
commit 3e5c36f8d8
2 changed files with 20 additions and 2 deletions

View File

@@ -60,6 +60,7 @@ services:
context: ./expressjs
container_name: expressjs
stop_grace_period: 5s
environment:
- BASE_PATH=/manage

View File

@@ -334,7 +334,7 @@ async function cleanupExpired() {
}
}
setInterval(() => {
const cleanupTimer = setInterval(() => {
cleanupExpired().catch(() => undefined);
}, 60 * 1000);
cleanupExpired().catch(() => undefined);
@@ -804,6 +804,23 @@ app.use((req, res) => {
res.status(404).send(renderPage('Nicht gefunden', '<p class="card">Seite nicht gefunden.</p>'));
});
app.listen(port, () => {
const server = app.listen(port, () => {
console.log(`Express server listening on ${port} with base path ${basePath}`);
});
function shutdown(signal) {
clearInterval(cleanupTimer);
server.close(() => {
db.close(() => {
console.log(`Shutdown complete (${signal}).`);
process.exit(0);
});
});
setTimeout(() => {
console.error('Forced shutdown after timeout.');
process.exit(1);
}, 5000).unref();
}
process.on('SIGTERM', () => shutdown('SIGTERM'));
process.on('SIGINT', () => shutdown('SIGINT'));