|
| 1 | +"use server" |
| 2 | + |
| 3 | +import fs from "fs" |
| 4 | +import { spawn } from "child_process" |
| 5 | +import { revalidatePath } from "next/cache" |
| 6 | + |
| 7 | +import { deleteRun as _deleteRun } from "@roo-code/evals" |
| 8 | + |
| 9 | +import { redisClient } from "@/lib/server/redis" |
| 10 | + |
| 11 | +const RUN_QUEUE_KEY = "evals:run-queue" |
| 12 | +const ACTIVE_RUN_KEY = "evals:active-run" |
| 13 | +const DISPATCH_LOCK_KEY = "evals:dispatcher:lock" |
| 14 | +const ACTIVE_RUN_TTL_SECONDS = 60 * 60 * 12 // 12 hours |
| 15 | +const DISPATCH_LOCK_TTL_SECONDS = 30 |
| 16 | + |
| 17 | +async function spawnController(runId: number) { |
| 18 | + const isRunningInDocker = fs.existsSync("/.dockerenv") |
| 19 | + |
| 20 | + const dockerArgs = [ |
| 21 | + `--name evals-controller-${runId}`, |
| 22 | + "--rm", |
| 23 | + "--network evals_default", |
| 24 | + "-v /var/run/docker.sock:/var/run/docker.sock", |
| 25 | + "-v /tmp/evals:/var/log/evals", |
| 26 | + "-e HOST_EXECUTION_METHOD=docker", |
| 27 | + ] |
| 28 | + |
| 29 | + const cliCommand = `pnpm --filter @roo-code/evals cli --runId ${runId}` |
| 30 | + |
| 31 | + const command = isRunningInDocker |
| 32 | + ? `docker run ${dockerArgs.join(" ")} evals-runner sh -c "${cliCommand}"` |
| 33 | + : cliCommand |
| 34 | + |
| 35 | + const childProcess = spawn("sh", ["-c", command], { |
| 36 | + detached: true, |
| 37 | + stdio: ["ignore", "pipe", "pipe"], |
| 38 | + }) |
| 39 | + |
| 40 | + // Best-effort logging of controller output |
| 41 | + try { |
| 42 | + const logStream = fs.createWriteStream("/tmp/roo-code-evals.log", { flags: "a" }) |
| 43 | + childProcess.stdout?.pipe(logStream) |
| 44 | + childProcess.stderr?.pipe(logStream) |
| 45 | + } catch (_error) { |
| 46 | + // Intentionally ignore logging pipe errors |
| 47 | + } |
| 48 | + |
| 49 | + childProcess.unref() |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Enqueue a run into the global FIFO (idempotent). |
| 54 | + */ |
| 55 | +export async function enqueueRun(runId: number) { |
| 56 | + const redis = await redisClient() |
| 57 | + const exists = await redis.lPos(RUN_QUEUE_KEY, runId.toString()) |
| 58 | + if (exists === null) { |
| 59 | + await redis.rPush(RUN_QUEUE_KEY, runId.toString()) |
| 60 | + } |
| 61 | + revalidatePath("/runs") |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Dispatcher: if no active run, pop next from queue and start controller. |
| 66 | + * Uses a short-lived lock to avoid races between concurrent dispatchers. |
| 67 | + */ |
| 68 | +export async function dispatchNextRun() { |
| 69 | + const redis = await redisClient() |
| 70 | + |
| 71 | + // Try to acquire dispatcher lock |
| 72 | + const locked = await redis.set(DISPATCH_LOCK_KEY, "1", { NX: true, EX: DISPATCH_LOCK_TTL_SECONDS }) |
| 73 | + if (!locked) return |
| 74 | + |
| 75 | + try { |
| 76 | + // If an active run is present, nothing to do. |
| 77 | + const active = await redis.get(ACTIVE_RUN_KEY) |
| 78 | + if (active) return |
| 79 | + |
| 80 | + const nextId = await redis.lPop(RUN_QUEUE_KEY) |
| 81 | + if (!nextId) return |
| 82 | + |
| 83 | + const ok = await redis.set(ACTIVE_RUN_KEY, nextId, { NX: true, EX: ACTIVE_RUN_TTL_SECONDS }) |
| 84 | + if (!ok) { |
| 85 | + // put it back to preserve order and exit |
| 86 | + await redis.lPush(RUN_QUEUE_KEY, nextId) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + await spawnController(Number(nextId)) |
| 91 | + } finally { |
| 92 | + await redis.del(DISPATCH_LOCK_KEY).catch(() => {}) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Return 1-based position in the global FIFO queue, or null if not queued. |
| 98 | + */ |
| 99 | +export async function getQueuePosition(runId: number): Promise<number | null> { |
| 100 | + const redis = await redisClient() |
| 101 | + const idx = await redis.lPos(RUN_QUEUE_KEY, runId.toString()) |
| 102 | + return idx === null ? null : idx + 1 |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Remove a queued run from the FIFO queue and delete the run record. |
| 107 | + */ |
| 108 | +export async function cancelQueuedRun(runId: number) { |
| 109 | + const redis = await redisClient() |
| 110 | + await redis.lRem(RUN_QUEUE_KEY, 1, runId.toString()) |
| 111 | + await _deleteRun(runId) |
| 112 | + revalidatePath("/runs") |
| 113 | +} |
0 commit comments