Public: anyone can read this; the crew can change it.
Healer: a server still starting, its log moving, is not started again #18053
Merged 11h 19m ago, live in production
sold/heal-patience → main 0 commits 5 threads · 5 unresolved
2 files
changed +58
−2
src/games/heal.ts +24 −1
| @@ -26,6 +26,10 @@ export const GAP_MS = 120_000; | ||
| 26 | 26 | export const LOST_MS = 5 * 60_000; |
| 27 | 27 | /** An agent silent this long is stuck or down. */ |
| 28 | 28 | export const SILENT_MS = 3 * 60_000; |
| 29 | /** A server starting whose log gained a line this recently is slow, not failed: a modded start can take minutes. */ | |
| 30 | export const PATIENCE_MS = 120_000; | |
| 31 | /** A moving log holds a restart off no longer than this after the container came up: past it the start is stuck, not slow. */ | |
| 32 | export const START_CAP_MS = 10 * 60_000; | |
| 29 | 33 | export const HEALER = "forge heal"; |
| 30 | 34 | |
| 31 | 35 | export interface JoinEvent { |
| @@ -62,7 +66,21 @@ const tried = (db: Database, boxId: string, service: string, since: string): { n | ||
| 62 | 66 | return { n: r.n, last: r.last ?? "" }; |
| 63 | 67 | }; |
| 64 | 68 | |
| 69 | /** | |
| 70 | * A container that is running and still `starting` (not unhealthy, not restarting: a crash loop | |
| 71 | * logs too), whose log gained a line within `PATIENCE_MS` and which came up less than | |
| 72 | * `START_CAP_MS` ago: a slow start. | |
| 73 | */ | |
| 74 | function slowStart(db: Database, boxId: string, service: string, now: number): boolean { | |
| 75 | const s = db.query<{ state: string; health: string; up: string | null }, [string, string]>("SELECT state, health, up_since AS up FROM game_service WHERE box_id = ?1 AND name = ?2").get(boxId, service); | |
| 76 | if (s === null || s.state !== "running" || s.health !== "starting") return false; | |
| 77 | if (s.up !== null && now - Date.parse(s.up) >= START_CAP_MS) return false; | |
| 78 | const l = db.query<{ at: string | null }, [string, string]>("SELECT MAX(at) AS at FROM game_log WHERE box_id = ?1 AND service = ?2").get(boxId, service); | |
| 79 | return l?.at != null && now - Date.parse(l.at) < PATIENCE_MS; | |
| 80 | } | |
| 81 | ||
| 65 | 82 | function startOnce(db: Database, box: Pick<GameBox, "id">, service: string, kind: string, why: string, now: number): boolean { |
| 83 | if (slowStart(db, box.id, service, now)) return false; | |
| 66 | 84 | const at = new Date(now).toISOString(); |
| 67 | 85 | const since = new Date(now - WINDOW_MS).toISOString(); |
| 68 | 86 | const have = tried(db, box.id, service, since); |
| @@ -96,8 +114,13 @@ export function heal(db: Database, box: Pick<GameBox, "id">, composeText: string | ||
| 96 | 114 | .all(box.id, new Date(now - LOST_MS).toISOString(), new Date(now - WINDOW_MS).toISOString()); |
| 97 | 115 | for (const c of lost) { |
| 98 | 116 | if (!SERVICE_NAME.test(c.service)) continue; |
| 117 | const slow = slowStart(db, box.id, c.service, now); | |
| 99 | 118 | const again = startOnce(db, box, c.service, "retry", `the agent never answered a start of ${c.service}`, now); |
| 100 | const output = again ? "the agent never answered; the forge started it again" : "the agent never answered; the forge did not start it again (limit or wait between tries)"; | |
| 119 | const output = again | |
| 120 | ? "the agent never answered; the forge started it again" | |
| 121 | : slow | |
| 122 | ? "the agent never answered, but the server is still starting (its log moved in the last 2 minutes); the forge did not start it again" | |
| 123 | : "the agent never answered; the forge did not start it again (limit or wait between tries)"; | |
| 101 | 124 | db.query("UPDATE game_command SET done_at = ?2, ok = 0, output = ?3 WHERE id = ?1").run(c.id, new Date(now).toISOString(), output); |
| 102 | 125 | } |
| 103 | 126 | } |
test/game-heal.test.ts +34 −1
| @@ -10,7 +10,7 @@ import { rm } from "node:fs/promises"; | ||
| 10 | 10 | import { tmpdir } from "node:os"; |
| 11 | 11 | import { join } from "node:path"; |
| 12 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 13 | import { GAP_MS, heal, healWords, LOST_MS, SILENT_MS, TRIES, waiting, WAKE_GRACE_MS } from "../src/games/heal.ts"; | |
| 13 | import { GAP_MS, heal, healWords, LOST_MS, SILENT_MS, START_CAP_MS, TRIES, waiting, WAKE_GRACE_MS } from "../src/games/heal.ts"; | |
| 14 | 14 | import { boxNamed, createGameBox, markAsleep, queueCommand, recordSync, recentCommands, type GameBox } from "../src/games/store.ts"; |
| 15 | 15 | import { quietLogs } from "./helpers/globals.ts"; |
| 16 | 16 | |
| @@ -141,6 +141,39 @@ describe("a stuck agent", () => { | ||
| 141 | 141 | expect(old.output).not.toContain("forge started it again"); |
| 142 | 142 | }); |
| 143 | 143 | |
| 144 | /** A lost start whose server says `health` in state `state`, its last log line `agoMs` before the heal, up since `upMs` before it. */ | |
| 145 | const lostStart = (o: { state?: string; health: string; agoMs: number; upMs?: number }) => { | |
| 146 | const b = box({ join: false }); | |
| 147 | queueCommand(app.db, b.id, "hunting", "start", "", "alice"); | |
| 148 | recordSync(app.db, b, {}, new Date(T0)); | |
| 149 | const now = T0 + LOST_MS + 1000; | |
| 150 | recordSync( | |
| 151 | app.db, | |
| 152 | b, | |
| 153 | { services: [{ name: "hunting", state: o.state ?? "running", health: o.health, upSince: iso(now - (o.upMs ?? 90_000)) }], logs: { hunting: ["Preparing spawn area: 40%"] } }, | |
| 154 | new Date(now - o.agoMs), | |
| 155 | ); | |
| 156 | heal(app.db, b, COMPOSE, now); | |
| 157 | return starts(b); | |
| 158 | }; | |
| 159 | ||
| 160 | test("a lost start of a server that is up and starting, its log moving, is not started again", () => { | |
| 161 | const all = lostStart({ health: "starting", agoMs: 60_000 }); | |
| 162 | expect(all).toHaveLength(1); | |
| 163 | expect(all[0]!.output).toContain("still starting"); | |
| 164 | }); | |
| 165 | ||
| 166 | test("a silent log, a crash loop or an unhealthy server is still started again", () => { | |
| 167 | expect(lostStart({ health: "starting", agoMs: 121_000 })).toHaveLength(2); | |
| 168 | expect(lostStart({ health: "unhealthy", agoMs: 10_000 })).toHaveLength(2); | |
| 169 | expect(lostStart({ state: "restarting", health: "starting", agoMs: 10_000 })).toHaveLength(2); | |
| 170 | }); | |
| 171 | ||
| 172 | test("a moving log holds a restart off for START_CAP_MS after the container came up, no longer", () => { | |
| 173 | expect(lostStart({ health: "starting", agoMs: 10_000, upMs: START_CAP_MS - 1000 })).toHaveLength(1); | |
| 174 | expect(lostStart({ health: "starting", agoMs: 10_000, upMs: START_CAP_MS })).toHaveLength(2); | |
| 175 | }); | |
| 176 | ||
| 144 | 177 | test("a silent agent is said, and that the forge cannot restart it", () => { |
| 145 | 178 | const b = { id: "none", seenAt: iso(T0) }; |
| 146 | 179 | expect(healWords(app.db, b, T0 + SILENT_MS - 1)).toEqual([]); |
new above
offline
Nothing caps how long a moving log can hold off a restart. Add a ceiling or say why none is needed.