Fix health DB connectivity probe
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
db4e146551
commit
88e742a129
2 changed files with 54 additions and 3 deletions
|
|
@ -1,16 +1,56 @@
|
||||||
import { describe, it, expect } from "vitest";
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import request from "supertest";
|
import request from "supertest";
|
||||||
|
import type { Db } from "@paperclipai/db";
|
||||||
import { healthRoutes } from "../routes/health.js";
|
import { healthRoutes } from "../routes/health.js";
|
||||||
|
import * as devServerStatus from "../dev-server-status.js";
|
||||||
import { serverVersion } from "../version.js";
|
import { serverVersion } from "../version.js";
|
||||||
|
|
||||||
describe("GET /health", () => {
|
describe("GET /health", () => {
|
||||||
const app = express();
|
beforeEach(() => {
|
||||||
app.use("/health", healthRoutes());
|
vi.spyOn(devServerStatus, "readPersistedDevServerStatus").mockReturnValue(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
it("returns 200 with status ok", async () => {
|
it("returns 200 with status ok", async () => {
|
||||||
|
const app = express();
|
||||||
|
app.use("/health", healthRoutes());
|
||||||
|
|
||||||
const res = await request(app).get("/health");
|
const res = await request(app).get("/health");
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
expect(res.body).toEqual({ status: "ok", version: serverVersion });
|
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",
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,17 @@ export function healthRoutes(
|
||||||
return;
|
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 bootstrapStatus: "ready" | "bootstrap_pending" = "ready";
|
||||||
let bootstrapInviteActive = false;
|
let bootstrapInviteActive = false;
|
||||||
if (opts.deploymentMode === "authenticated") {
|
if (opts.deploymentMode === "authenticated") {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue