fix : correct api healthz
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const execute = vi.fn();
|
||||
const ping = vi.fn();
|
||||
const checkSlip2Go = vi.fn();
|
||||
const checkDiscordBot = vi.fn();
|
||||
const checkLunaris = vi.fn();
|
||||
|
||||
vi.mock("next/server", () => ({ connection: vi.fn() }));
|
||||
vi.mock("@/db/client", () => ({ getDb: () => ({ execute }) }));
|
||||
vi.mock("@/lib/redis/client", () => ({ getRedisClient: async () => ({ ping }) }));
|
||||
vi.mock("@/lib/health/services", () => ({ checkSlip2Go, checkDiscordBot, checkLunaris }));
|
||||
|
||||
const { GET } = await import("./route");
|
||||
|
||||
describe("healthz endpoint", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
execute.mockResolvedValue(undefined);
|
||||
ping.mockResolvedValue("PONG");
|
||||
checkSlip2Go.mockResolvedValue(undefined);
|
||||
checkDiscordBot.mockResolvedValue(undefined);
|
||||
checkLunaris.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
it("checks all five dependencies", async () => {
|
||||
const response = await GET();
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Cache-Control")).toBe("no-store");
|
||||
expect(await response.json()).toMatchObject({
|
||||
status: "ready",
|
||||
checks: { database: "ok", redis: "ok", slip2go: "ok", discordBot: "ok", lunaris: "ok" },
|
||||
});
|
||||
});
|
||||
|
||||
it("returns 503 and identifies a failed dependency", async () => {
|
||||
checkDiscordBot.mockRejectedValueOnce(new Error("offline"));
|
||||
const response = await GET();
|
||||
expect(response.status).toBe(503);
|
||||
expect(await response.json()).toMatchObject({
|
||||
status: "not-ready",
|
||||
checks: { database: "ok", redis: "ok", slip2go: "ok", discordBot: "failed", lunaris: "ok" },
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { connection } from "next/server";
|
||||
|
||||
import { getDb } from "@/db/client";
|
||||
import { checkReadiness } from "@/lib/health/readiness";
|
||||
import { checkDiscordBot, checkLunaris, checkSlip2Go } from "@/lib/health/services";
|
||||
import { getRedisClient } from "@/lib/redis/client";
|
||||
|
||||
export async function GET() {
|
||||
await connection();
|
||||
const deploymentId = process.env.NEXT_DEPLOYMENT_ID || "unknown";
|
||||
const configuredTimeout = Number(process.env.HEALTHCHECK_TIMEOUT_MS || 2_500);
|
||||
const timeoutMs = Number.isFinite(configuredTimeout)
|
||||
? Math.max(100, Math.min(10_000, configuredTimeout))
|
||||
: 2_500;
|
||||
const result = await checkReadiness({
|
||||
database: async () => {
|
||||
await getDb().execute(sql`select 1`);
|
||||
},
|
||||
redis: async () => {
|
||||
const client = await getRedisClient();
|
||||
await client.ping();
|
||||
},
|
||||
slip2go: () => checkSlip2Go(AbortSignal.timeout(timeoutMs)),
|
||||
discordBot: checkDiscordBot,
|
||||
lunaris: () => checkLunaris(AbortSignal.timeout(timeoutMs)),
|
||||
}, timeoutMs);
|
||||
|
||||
return Response.json(
|
||||
{ ...result, deploymentId },
|
||||
{ status: result.status === "ready" ? 200 : 503, headers: { "Cache-Control": "no-store" } },
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user