mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-07-09 00:30:49 +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,
|
||||
});
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
systemLogger.info(
|
||||
"Received SIGINT signal, initiating graceful shutdown...",
|
||||
{ operation: "shutdown" },
|
||||
);
|
||||
const gracefulShutdown = async (signal: string) => {
|
||||
systemLogger.info(`Received ${signal}, initiating graceful 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.on("SIGTERM", () => {
|
||||
systemLogger.info(
|
||||
"Received SIGTERM signal, initiating graceful shutdown...",
|
||||
{ operation: "shutdown" },
|
||||
);
|
||||
process.exit(0);
|
||||
});
|
||||
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
|
||||
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
|
||||
|
||||
process.on("message", (msg: { type?: string }) => {
|
||||
if (msg?.type === "shutdown") {
|
||||
systemLogger.info(
|
||||
"Received IPC shutdown, initiating graceful shutdown...",
|
||||
{ operation: "shutdown" },
|
||||
);
|
||||
process.exit(0);
|
||||
gracefulShutdown("IPC shutdown");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user