import { WORKER_HEARTBEAT_TTL_SECONDS } from "@/lib/constants/rpa";
import { listWorkerHeartbeats } from "@/workers/rpa/worker-state";

/** 是否存在近期有心跳的 RPA Worker（用于地址候选是否走 BullMQ） */
export async function isAnyRpaWorkerHealthy(
  maxAgeMs = WORKER_HEARTBEAT_TTL_SECONDS * 1_000,
): Promise<boolean> {
  try {
    const workers = await listWorkerHeartbeats();
    const now = Date.now();
    return workers.some(
      (worker) =>
        worker.lastSeenMs !== null && now - worker.lastSeenMs <= maxAgeMs,
    );
  } catch {
    return false;
  }
}
