feat: backend status & health check

This commit is contained in:
Nicolas Meienberger
2025-09-03 22:18:59 +02:00
parent 63b983b1b1
commit 7fe75c64e8
12 changed files with 239 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
import * as fs from "node:fs/promises";
import type { BackendConfig } from "@ironmount/schemas";
import * as npath from "node:path";
import { BACKEND_STATUS, type BackendConfig } from "@ironmount/schemas";
import type { VolumeBackend } from "../backend";
const mount = async (_config: BackendConfig, path: string) => {
@@ -11,7 +12,24 @@ const unmount = async () => {
console.log("Cannot unmount directory volume.");
};
const checkHealth = async (path: string) => {
try {
await fs.access(path);
// Try to create a temporary file to ensure write access
const tempFilePath = npath.join(path, `.healthcheck-${Date.now()}`);
await fs.writeFile(tempFilePath, "healthcheck");
await fs.unlink(tempFilePath);
return { status: BACKEND_STATUS.mounted };
} catch (error) {
console.error("Directory health check failed:", error);
return { status: BACKEND_STATUS.error, error: error instanceof Error ? error.message : String(error) };
}
};
export const makeDirectoryBackend = (config: BackendConfig, path: string): VolumeBackend => ({
mount: () => mount(config, path),
unmount,
checkHealth: () => checkHealth(path),
});