mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-07-15 21:03:47 +00:00
fix: flush in-memory database to disk on graceful shutdown (#792)
SIGTERM/SIGINT handlers previously called process.exit(0) immediately without persisting the in-memory SQLite database. This caused session data loss on container restart, forcing users to re-authenticate.
This commit is contained in:
+21
-18
@@ -194,29 +194,32 @@ import {
|
|||||||
duration: Date.now() - initStartTime,
|
duration: Date.now() - initStartTime,
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("SIGINT", () => {
|
const gracefulShutdown = async (signal: string) => {
|
||||||
systemLogger.info(
|
systemLogger.info(`Received ${signal}, initiating graceful shutdown...`, {
|
||||||
"Received SIGINT signal, initiating graceful shutdown...",
|
operation: "shutdown",
|
||||||
{ operation: "shutdown" },
|
});
|
||||||
);
|
try {
|
||||||
|
const { saveMemoryDatabaseToFile } = await import(
|
||||||
|
"./database/db/index.js"
|
||||||
|
);
|
||||||
|
await saveMemoryDatabaseToFile();
|
||||||
|
systemLogger.info("Database saved to disk before exit", {
|
||||||
|
operation: "shutdown_db_saved",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
systemLogger.error("Failed to save database during shutdown", error, {
|
||||||
|
operation: "shutdown_db_save_failed",
|
||||||
|
});
|
||||||
|
}
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
};
|
||||||
|
|
||||||
process.on("SIGTERM", () => {
|
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
|
||||||
systemLogger.info(
|
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
|
||||||
"Received SIGTERM signal, initiating graceful shutdown...",
|
|
||||||
{ operation: "shutdown" },
|
|
||||||
);
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on("message", (msg: { type?: string }) => {
|
process.on("message", (msg: { type?: string }) => {
|
||||||
if (msg?.type === "shutdown") {
|
if (msg?.type === "shutdown") {
|
||||||
systemLogger.info(
|
gracefulShutdown("IPC shutdown");
|
||||||
"Received IPC shutdown, initiating graceful shutdown...",
|
|
||||||
{ operation: "shutdown" },
|
|
||||||
);
|
|
||||||
process.exit(0);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user