mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
fix: cleanup volumes on shutdown
This commit is contained in:
@@ -3,3 +3,4 @@ export const VOLUME_MOUNT_BASE = "/var/lib/ironmount/volumes";
|
||||
export const REPOSITORY_BASE = "/var/lib/ironmount/repositories";
|
||||
export const DATABASE_URL = "/var/lib/ironmount/data/ironmount.db";
|
||||
export const RESTIC_PASS_FILE = "/var/lib/ironmount/data/restic.pass";
|
||||
export const SOCKET_PATH = "/run/docker/plugins/ironmount.sock";
|
||||
|
||||
@@ -17,6 +17,8 @@ import { backupScheduleController } from "./modules/backups/backups.controller";
|
||||
import { eventsController } from "./modules/events/events.controller";
|
||||
import { handleServiceError } from "./utils/errors";
|
||||
import { logger } from "./utils/logger";
|
||||
import { shutdown } from "./modules/lifecycle/shutdown";
|
||||
import { SOCKET_PATH } from "./core/constants";
|
||||
|
||||
export const generalDescriptor = (app: Hono) =>
|
||||
openAPIRouteHandler(app, {
|
||||
@@ -70,17 +72,15 @@ runDbMigrations();
|
||||
const { docker } = await getCapabilities();
|
||||
|
||||
if (docker) {
|
||||
const socketPath = "/run/docker/plugins/ironmount.sock";
|
||||
|
||||
try {
|
||||
await fs.mkdir("/run/docker/plugins", { recursive: true });
|
||||
|
||||
Bun.serve({
|
||||
unix: socketPath,
|
||||
unix: SOCKET_PATH,
|
||||
fetch: driver.fetch,
|
||||
});
|
||||
|
||||
logger.info(`Docker volume plugin server running at ${socketPath}`);
|
||||
logger.info(`Docker volume plugin server running at ${SOCKET_PATH}`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to start Docker volume plugin server: ${error}`);
|
||||
}
|
||||
@@ -96,3 +96,16 @@ startup();
|
||||
logger.info(`Server is running at http://localhost:4096`);
|
||||
|
||||
export type AppType = typeof app;
|
||||
|
||||
process.on("SIGTERM", async () => {
|
||||
logger.info("SIGTERM received, starting graceful shutdown...");
|
||||
|
||||
await shutdown();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
process.on("SIGINT", async () => {
|
||||
logger.info("SIGINT received, starting graceful shutdown...");
|
||||
await shutdown();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
28
apps/server/src/modules/lifecycle/shutdown.ts
Normal file
28
apps/server/src/modules/lifecycle/shutdown.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Scheduler } from "../../core/scheduler";
|
||||
import { eq, or } from "drizzle-orm";
|
||||
import { db } from "../../db/db";
|
||||
import { volumesTable } from "../../db/schema";
|
||||
import { logger } from "../../utils/logger";
|
||||
import { SOCKET_PATH } from "../../core/constants";
|
||||
import { createVolumeBackend } from "../backends/backend";
|
||||
|
||||
export const shutdown = async () => {
|
||||
await Scheduler.stop();
|
||||
|
||||
await Bun.file(SOCKET_PATH)
|
||||
.delete()
|
||||
.catch(() => {
|
||||
// Ignore errors if the socket file does not exist
|
||||
});
|
||||
|
||||
const volumes = await db.query.volumesTable.findMany({
|
||||
where: or(eq(volumesTable.status, "mounted")),
|
||||
});
|
||||
|
||||
for (const volume of volumes) {
|
||||
const backend = createVolumeBackend(volume);
|
||||
const { status, error } = await backend.unmount();
|
||||
|
||||
logger.info(`Volume ${volume.name} unmount status: ${status}${error ? `, error: ${error}` : ""}`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user