Skip to content

The open-source app everyone uses to manage agents at work

Fix health DB connectivity probe

dotta88e742a129dc
diff --git a/server/src/__tests__/health.test.ts b/server/src/__tests__/health.test.ts
index 1511b95ea1..8f80eec628 100644
--- a/server/src/__tests__/health.test.ts
+++ b/server/src/__tests__/health.test.ts
@@ -1,16 +1,56 @@
-import { describe, it, expect } from "vitest";
+import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import express from "express";
import request from "supertest";
+import type { Db } from "@paperclipai/db";
import { healthRoutes } from "../routes/health.js";
+import * as devServerStatus from "../dev-server-status.js";
import { serverVersion } from "../version.js";
describe("GET /health", () => {
- const app = express();
- app.use("/health", healthRoutes());
+ beforeEach(() => {
+ vi.spyOn(devServerStatus, "readPersistedDevServerStatus").mockReturnValue(undefined);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
it("returns 200 with status ok", async () => {
+ const app = express();
+ app.use("/health", healthRoutes());
+
const res = await request(app).get("/health");
expect(res.status).toBe(200);
expect(res.body).toEqual({ status: "ok", version: serverVersion });
});
+
+ it("returns 200 when the database probe succeeds", async () => {
+ const db = {
+ execute: vi.fn().mockResolvedValue([{ "?column?": 1 }]),
+ } as unknown as Db;
+ const app = express();
+ app.use("/health", healthRoutes(db));
+
+ const res = await request(app).get("/health");
+
+ expect(res.status).toBe(200);
+ expect(res.body).toMatchObject({ status: "ok", version: serverVersion });
+ });
+
+ it("returns 503 when the database probe fails", async () => {
+ const db = {
+ execute: vi.fn().mockRejectedValue(new Error("connect ECONNREFUSED")),
+ } as unknown as Db;
+ const app = express();
+ app.use("/health", healthRoutes(db));
+
+ const res = await request(app).get("/health");
+
+ expect(res.status).toBe(503);
+ expect(res.body).toEqual({
+ status: "unhealthy",
+ version: serverVersion,
+ error: "database_unreachable",
+ });
+ });
});
diff --git a/server/src/routes/health.ts b/server/src/routes/health.ts
index 0bf6e92fe5..795eb9a5cb 100644
--- a/server/src/routes/health.ts
+++ b/server/src/routes/health.ts
@@ -29,6 +29,17 @@ export function healthRoutes(
return;
}
+ try {
+ await db.execute(sql`SELECT 1`);
+ } catch {
+ res.status(503).json({
+ status: "unhealthy",
+ version: serverVersion,
+ error: "database_unreachable",
+ });
+ return;
+ }
+
let bootstrapStatus: "ready" | "bootstrap_pending" = "ready";
let bootstrapInviteActive = false;
if (opts.deploymentMode === "authenticated") {