Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add health endpoint #459

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions apps/server/src/database/queries/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,55 @@ export async function setFeatureCompatibilityVersion(version: string) {
const admin = mongoose.connection.db.admin();
await admin.command({ setFeatureCompatibilityVersion: version });
}

export async function getDatabaseHealth(): Promise<{
status: "UP" | "DOWN" | "READONLY";
details?: {
version: string;
uptime: number;
writable: boolean;
};
message?: string;
}> {
if (!mongoose.connection.readyState || !mongoose.connection.db) {
return { status: "DOWN", message: "Database not connected" };
}

try {
const mongoInfos = await getMongoInfos();

// Fetch server status to get uptime
const admin = mongoose.connection.db.admin();
const serverStatus = await admin.command({ serverStatus: 1 });

// Check for fsyncLock using db.currentOp()
const currentOp = await admin.command({ currentOp: 1 });
const isWriteLocked = currentOp.inprog.some((op: { desc: string, active: boolean }) => op.desc === 'fsyncLockWorker' && op.active);

if (isWriteLocked) {
return {
status: "READONLY",
details: {
version: mongoInfos.version,
uptime: serverStatus.uptime || 0,
writable: false,
},
message: "Database is read-only (fsyncLock enabled)",
};
}

// If no lock is detected, check if the DB is generally writable
return {
status: "UP",
details: {
version: mongoInfos.version,
uptime: serverStatus.uptime || 0,
writable: true,
},
};

} catch (error) {
console.error("Error querying database health:", error);
return { status: "DOWN", message: "Error querying database health" };
}
}
18 changes: 18 additions & 0 deletions apps/server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setUserPublicToken,
storeInUser,
} from "../database";
import { getDatabaseHealth } from "../database/queries/meta";
import { logger } from "../tools/logger";
import {
LoggedRequest,
Expand All @@ -36,6 +37,23 @@ router.get("/", (_, res) => {
res.status(200).send("Hello !");
});

router.get("/health", async (_, res) => {
try {
const dbHealth = await getDatabaseHealth();

if (dbHealth.status === "DOWN") {
res.status(503).send({status: "error", db_health: dbHealth});
} else if (dbHealth.status === "READONLY") {
res.status(503).send({status: "warning", db_health: dbHealth});
} else {
res.status(200).send({status: "ok", db_health: dbHealth});
}
} catch (e) {
logger.error(e);
res.status(500).send({ status: "error", message: "Unexpected error" });
}
});

router.post("/logout", async (_, res) => {
res.clearCookie("token");
res.status(200).end();
Expand Down