72 lines
3.8 KiB
TypeScript
72 lines
3.8 KiB
TypeScript
// PEP-205: the checks executor (Gitea Actions `peppa-act-runner`, a docker container) is under the
|
|
// watchdog's eye: if it is down it is started again; if it is up but nobody takes the jobs of a
|
|
// standing checks queue (no job at all on the heads), it is restarted once per half hour.
|
|
// PEP-233: never while jobs run (their containers `GITEA-ACTIONS-TASK-*` are up — a restart would
|
|
// cut them, a deploy half-done), and only for a head first seen after the last restart: the same
|
|
// standing head restarts the executor once, a new one is a new sign. Heads without a workflow or
|
|
// older than two days are not the executor's fault (`checksQueueTick` marks them `orphan`).
|
|
// Every restart is an event in the platform project's journal.
|
|
// glossary-ignore-file: events here are read by the superadmin.
|
|
import { execFile } from "node:child_process";
|
|
import { promisify } from "node:util";
|
|
import { getSetting, listProjects, logEvent, setSetting } from "@peppa/core";
|
|
import { settings, type Db, keyPrefix } from "@peppa/db";
|
|
import { platformKey } from "./platform-release.ts";
|
|
|
|
const run = promisify(execFile);
|
|
const docker = () => process.env.PEPPA_DOCKER_BIN ?? "docker";
|
|
const container = () => process.env.PEPPA_ACT_RUNNER_CONTAINER ?? "peppa-act-runner";
|
|
const RESTART_KEY = "actrunner.restarted";
|
|
/** A head without any job this long (after the stuck threshold) — the executor does not take jobs. */
|
|
const HUNG_AFTER_MS = 10 * 60_000;
|
|
const RESTART_GAP_MS = 30 * 60_000;
|
|
/** PEP-233: the containers act_runner starts for jobs. */
|
|
const JOB_PREFIX = "GITEA-ACTIONS-TASK-";
|
|
|
|
/** PEP-233: jobs run on the host's docker (or we can't tell — then we don't cut anything). */
|
|
async function jobsRunning(): Promise<boolean> {
|
|
try {
|
|
const out = (await run(docker(), ["ps", "--format", "{{.Names}}"], { timeout: 20_000 })).stdout;
|
|
return out.split("\n").some((l) => l.split("\t").some((f) => f.trim().startsWith(JOB_PREFIX)));
|
|
} catch {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export type ActRunnerRestart = "down" | "hung";
|
|
|
|
export async function actRunnerTick(db: Db, o: { now?: Date; log?: (m: string) => void } = {}): Promise<ActRunnerRestart | null> {
|
|
if (process.env.PEPPA_ACT_RUNNER_GUARD === "off") return null;
|
|
const log = o.log ?? (() => {});
|
|
const now = (o.now ?? new Date()).getTime();
|
|
let state: string;
|
|
try {
|
|
state = (await run(docker(), ["inspect", "-f", "{{.State.Status}}", container()], { timeout: 20_000 })).stdout.trim();
|
|
} catch {
|
|
return null; // no such container here (another machine) or docker is down — not ours to judge
|
|
}
|
|
let reason: ActRunnerRestart | null = state === "running" ? null : "down";
|
|
const last = (await getSetting<{ at: number }>(db, RESTART_KEY))?.at ?? 0;
|
|
if (!reason && now - last >= RESTART_GAP_MS) {
|
|
const waits = await db.select().from(settings).where(keyPrefix(settings.key, "checks.wait:"));
|
|
const hung = waits.some((w) => {
|
|
const v = w.value as { stuck?: boolean; cause?: string; since: number; orphan?: boolean };
|
|
return v.stuck && !v.orphan && v.cause === "no_job" && v.since >= last && now - v.since >= HUNG_AFTER_MS;
|
|
});
|
|
if (hung && !(await jobsRunning())) reason = "hung";
|
|
}
|
|
if (!reason) return null;
|
|
if (reason === "down" && now - last < 60_000) return null;
|
|
try {
|
|
await run(docker(), ["restart", container()], { timeout: 120_000 });
|
|
} catch (e) {
|
|
log(`act runner restart failed: ${(e as Error).message.split("\n")[0]}`);
|
|
return null;
|
|
}
|
|
await setSetting(db, RESTART_KEY, { at: now, reason });
|
|
const platform = (await listProjects(db)).find((p) => p.key === platformKey());
|
|
if (platform) await logEvent(db, { projectId: platform.id, type: "act_runner_restarted", payload: { reason, container: container(), was: state } });
|
|
log(`act runner restarted (${reason}, was ${state})`);
|
|
return reason;
|
|
}
|