Public: anyone can read this; the crew can change it.

Healer: a server still starting, its log moving, is not started again #18053

opened this

Merged 11h 19m ago, live in production

sold/heal-patience → main 0 commits 5 threads · 5 unresolved

  • Merged. sold/heal-patience was merged into main as d918642.

Review

Sign in to review this change.
2 files changed +58 −2
src/games/heal.ts +24 −1
@@ -26,6 +26,10 @@ export const GAP_MS = 120_000;
2626 export const LOST_MS = 5 * 60_000;
2727 /** An agent silent this long is stuck or down. */
2828 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. */

Nothing caps how long a moving log can hold off a restart. Add a ceiling or say why none is needed.

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;
2933 export const HEALER = "forge heal";
3034
3135 export interface JoinEvent {
@@ -62,7 +66,21 @@ const tried = (db: Database, boxId: string, service: string, since: string): { n
6266 return { n: r.n, last: r.last ?? "" };
6367 };
6468
69 /**
70 * A container that is running and still `starting` (not unhealthy, not restarting: a crash loop
outdated

Too wide: unhealthy and restarting pass this check, so a crash-looping server that logs is never healed. Require state === 'running' && health === 'starting', and test the unhealthy case.

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;

Non-blocking: a null up_since skips the cap, so a log that keeps moving holds the restart off forever. Return false when s.up is null, or the 10-minute bound is not a bound.

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
6582 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;
6684 const at = new Date(now).toISOString();
6785 const since = new Date(now - WINDOW_MS).toISOString();
6886 const have = tried(db, box.id, service, since);
@@ -96,8 +114,13 @@ export function heal(db: Database, box: Pick<GameBox, "id">, composeText: string
96114 .all(box.id, new Date(now - LOST_MS).toISOString(), new Date(now - WINDOW_MS).toISOString());
97115 for (const c of lost) {
98116 if (!SERVICE_NAME.test(c.service)) continue;
117 const slow = slowStart(db, box.id, c.service, now);
99118 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"

Non-blocking: "2 minutes" is written out here. Derive it from PATIENCE_MS (PATIENCE_MS / 60_000) so the sentence cannot go stale if the constant changes.

123 : "the agent never answered; the forge did not start it again (limit or wait between tries)";
101124 db.query("UPDATE game_command SET done_at = ?2, ok = 0, output = ?3 WHERE id = ?1").run(c.id, new Date(now).toISOString(), output);
102125 }
103126 }
test/game-heal.test.ts +34 −1
@@ -10,7 +10,7 @@ import { rm } from "node:fs/promises";
1010 import { tmpdir } from "node:os";
1111 import { join } from "node:path";
1212 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";
1414 import { boxNamed, createGameBox, markAsleep, queueCommand, recordSync, recentCommands, type GameBox } from "../src/games/store.ts";
1515 import { quietLogs } from "./helpers/globals.ts";
1616
@@ -141,6 +141,39 @@ describe("a stuck agent", () => {
141141 expect(old.output).not.toContain("forge started it again");
142142 });
143143
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", () => {
outdated

expect(id).toBeTruthy() exists only to use id. Remove both. Also add the unhealthy-with-fresh-log case, which must still be 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
144177 test("a silent agent is said, and that the forge cannot restart it", () => {
145178 const b = { id: "none", seenAt: iso(T0) };
146179 expect(healWords(app.db, b, T0 + SILENT_MS - 1)).toEqual([]);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs