From da671d4f4b438a0645e92551f7ea534db84eb9b6 Mon Sep 17 00:00:00 2001 From: amianthus <49116958+SirTenzin@users.noreply.github.com> Date: Tue, 26 May 2026 16:50:18 +0100 Subject: [PATCH] revert: healthcheck redis startup gate --- server/src/honoUtils/handleHealthCheck.ts | 30 ------- .../honoUtils/handle-health-check.test.ts | 78 ------------------- 2 files changed, 108 deletions(-) delete mode 100644 server/tests/unit/honoUtils/handle-health-check.test.ts diff --git a/server/src/honoUtils/handleHealthCheck.ts b/server/src/honoUtils/handleHealthCheck.ts index fed88fca0..fb57dd907 100644 --- a/server/src/honoUtils/handleHealthCheck.ts +++ b/server/src/honoUtils/handleHealthCheck.ts @@ -1,36 +1,6 @@ import type { Context } from "hono"; -import { logger } from "@/external/logtail/logtailUtils.js"; -import { redis } from "@/external/redis/initRedis.js"; -import { redisV2 } from "@/external/redis/initRedisV2.js"; import type { HonoEnv } from "./HonoEnv"; -let startupReady = false; - -const isDisabledRedis = (status: string) => status === "end"; - -const tryLatchStartupReady = () => { - if (startupReady) return; - const redisOk = redis.status === "ready" || isDisabledRedis(redis.status); - const redisV2Ok = - redisV2.status === "ready" || isDisabledRedis(redisV2.status); - if (!redisOk || !redisV2Ok) return; - startupReady = true; - logger.info("[health-check] startup gate latched", { - redis_status: redis.status, - redis_v2_status: redisV2.status, - }); -}; - -redis.once("ready", tryLatchStartupReady); -redisV2.once("ready", tryLatchStartupReady); -tryLatchStartupReady(); - export const handleHealthCheck = async (c: Context) => { - if (!startupReady) { - tryLatchStartupReady(); - if (!startupReady) { - return c.text("Redis not ready", 503); - } - } return c.text("Hello from Autumn (test 1) 🍂🍂🍂"); }; diff --git a/server/tests/unit/honoUtils/handle-health-check.test.ts b/server/tests/unit/honoUtils/handle-health-check.test.ts deleted file mode 100644 index 302e5c038..000000000 --- a/server/tests/unit/honoUtils/handle-health-check.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { describe, expect, mock, test } from "bun:test"; -import { EventEmitter } from "node:events"; - -class FakeRedis extends EventEmitter { - status: "wait" | "connecting" | "ready" | "end" = "wait"; - - setReady() { - this.status = "ready"; - this.emit("ready"); - } -} - -const fakeRedis = new FakeRedis(); -const fakeRedisV2 = new FakeRedis(); -const fakeLogger = { - info: mock(() => undefined), - warn: mock(() => undefined), - error: mock(() => undefined), - debug: mock(() => undefined), -}; - -mock.module("@/external/redis/initRedis.js", () => ({ redis: fakeRedis })); -mock.module("@/external/redis/initRedisV2.js", () => ({ - redisV2: fakeRedisV2, -})); -mock.module("@/external/logtail/logtailUtils.js", () => ({ - logger: fakeLogger, -})); - -const { handleHealthCheck } = await import("@/honoUtils/handleHealthCheck.js"); - -const callHealthCheck = async () => { - const responses: { status: number; body: string }[] = []; - const ctx = { - text: (body: string, status = 200) => { - responses.push({ status, body }); - return { status, body }; - }, - }; - await handleHealthCheck( - ctx as unknown as Parameters[0], - ); - return responses[0]!; -}; - -describe("handleHealthCheck startup gate", () => { - test("returns 503 when both Redis clients are not ready", async () => { - const res = await callHealthCheck(); - expect(res.status).toBe(503); - expect(res.body).toBe("Redis not ready"); - }); - - test("returns 503 when only one Redis client is ready", async () => { - fakeRedis.setReady(); - const res = await callHealthCheck(); - expect(res.status).toBe(503); - }); - - test("returns 200 once both Redis clients become ready", async () => { - fakeRedisV2.setReady(); - const res = await callHealthCheck(); - expect(res.status).toBe(200); - expect(res.body).toContain("Autumn"); - }); - - test("logs a single latch info message on flip", async () => { - expect(fakeLogger.info).toHaveBeenCalledTimes(1); - const firstCall = fakeLogger.info.mock.calls[0] as unknown as [string]; - expect(firstCall[0]).toContain("[health-check] startup gate latched"); - }); - - test("returns 200 unconditionally after latch — even if Redis regresses", async () => { - fakeRedis.status = "end"; - fakeRedisV2.status = "end"; - const res = await callHealthCheck(); - expect(res.status).toBe(200); - }); -});