// Redis 健康探针（/readyz P2 必需依赖）
// PING 命令检查 Redis 连通性，超时 2s
import type { Redis } from "ioredis";
import type { HealthProbe } from "./readiness.probe.js";

export class RedisHealthProbe implements HealthProbe {
  readonly name = "redis";
  readonly required = true;

  constructor(private readonly redis: Redis) {}

  async check(): Promise<{
    status: "up" | "down";
    latencyMs: number;
    error?: string;
  }> {
    const start = Date.now();
    try {
      const result = await this.redis.ping();
      const latencyMs = Date.now() - start;
      if (result === "PONG") {
        return { status: "up", latencyMs };
      }
      return {
        status: "down",
        latencyMs,
        error: `Unexpected PING response: ${result}`,
      };
    } catch (err) {
      return {
        status: "down",
        latencyMs: Date.now() - start,
        error: err instanceof Error ? err.message : String(err),
      };
    }
  }
}
