Public: anyone can read this; the crew can change it.
The game box's agent outlives a job that ends between its two looks at it #17730
Merged 17h 33m ago, live in production
sold/agent-job-tick → main 0 commits 1 thread · 1 unresolved
2 files
changed +31
−2
src/games/agent.sh +3
| @@ -750,6 +750,9 @@ job_tick() { | ||
| 750 | 750 | fi |
| 751 | 751 | fi |
| 752 | 752 | if [ -f "/.job.pid" ] && ! job_running; then |
| 753 | # Read here too: the job may have ended since the look above, which then read neither. | |
| 754 | id="$(cat "/.job.id" 2>/dev/null)" | |
| 755 | sha="$(cat "/.job.sha" 2>/dev/null)" | |
| 753 | 756 | rc="$(cat "/.job.rc" 2>/dev/null || echo 1)" |
| 754 | 757 | if [ "" = 0 ] && [ -n "" ]; then printf '%s' "" >"/.compose-sha"; fi |
| 755 | 758 | # A failed up is not retried for the same compose: it would fail again every 5 s. |
test/game-jars.test.ts +28 −2
| @@ -24,7 +24,7 @@ const JAR = "our AutoStopper"; | ||
| 24 | 24 | const NAME = `AutoStopper-${sha("sha256", JAR).slice(0, 12)}.jar`; |
| 25 | 25 | |
| 26 | 26 | /** A box whose forge hands out `compose` and `jars`, and serves `served` as every jar. */ |
| 27 | function made(o: { compose?: string; jars?: boolean; served?: string; upFails?: boolean; name?: string }) { | |
| 27 | function made(o: { compose?: string; jars?: boolean; served?: string; upFails?: boolean; upTakes?: number; name?: string }) { | |
| 28 | 28 | const root = mkdtempSync(join(tmpdir(), "forge-game-jars-")); |
| 29 | 29 | const dir = join(root, "game"); |
| 30 | 30 | const plugins = join(root, "server/plugins"); |
| @@ -44,6 +44,7 @@ case "$*" in | ||
| 44 | 44 | echo "up \${all#* up }" >>"${root}/ups" |
| 45 | 45 | ls "${plugins}" >"${root}/at-up" |
| 46 | 46 | ls "${dir}/jars" >"${root}/jars-at-up" 2>/dev/null |
| 47 | sleep ${o.upTakes ?? 0} | |
| 47 | 48 | exit ${o.upFails === true ? "1" : "0"} ;; |
| 48 | 49 | esac |
| 49 | 50 | exit 0 |
| @@ -67,7 +68,7 @@ exit 0 | ||
| 67 | 68 | await rm(root, { recursive: true, force: true }); |
| 68 | 69 | }); |
| 69 | 70 | /** Run the agent until its first job has ended and been reaped. */ |
| 70 | const run = async (): Promise<void> => { | |
| 71 | const run = async (env: Record<string, string> = {}): Promise<void> => { | |
| 71 | 72 | const p = Bun.spawn(["bash", AGENT], { |
| 72 | 73 | env: { |
| 73 | 74 | PATH: process.env["PATH"] ?? "", |
| @@ -79,6 +80,7 @@ exit 0 | ||
| 79 | 80 | FORGE_GAME_DIAG_S: "0", |
| 80 | 81 | FORGE_GAME_VOLUME_CHECK: "0", |
| 81 | 82 | FORGE_GAME_ONCE: "0", |
| 83 | ...env, | |
| 82 | 84 | }, |
| 83 | 85 | stdout: "ignore", |
| 84 | 86 | stderr: "ignore", |
| @@ -108,6 +110,30 @@ test("every full sync says this agent reads jars, so the forge may send a compos | ||
| 108 | 110 | expect(full.every((b) => b["jars"] === true)).toBe(true); |
| 109 | 111 | }); |
| 110 | 112 | |
| 113 | test("a job that ends between the two looks at it is reaped, and the agent goes on", async () => { | |
| 114 | const compose = "services: { hub: {} }\n"; | |
| 115 | // Longer than the second to the next look. | |
| 116 | const box = made({ compose, jars: false, upTakes: 3 }); | |
| 117 | // `tr` reads the job's command line in the first look: here it answers only once the job has ended. | |
| 118 | const bin = join(box.dir, "../bin"); | |
| 119 | mkdirSync(bin); | |
| 120 | writeFileSync(join(bin, "tr"), `#!/usr/bin/env bash | |
| 121 | PATH="\${PATH#*:}" | |
| 122 | [ "$1" = '\\0' ] || exec tr "$@" | |
| 123 | said="$(tr "$@")" | |
| 124 | for _ in $(seq 100); do | |
| 125 | [ -f "${box.dir}/.job.rc" ] && [ -z "$(exec tr -d '\\0' <"/proc/$(cat "${box.dir}/.job.pid")/cmdline" 2>/dev/null)" ] && break | |
| 126 | sleep 0.05 | |
| 127 | done | |
| 128 | echo "$said" | |
| 129 | `); | |
| 130 | chmodSync(join(bin, "tr"), 0o755); | |
| 131 | await box.run({ PATH: `${bin}:${process.env["PATH"] ?? ""}` }); | |
| 132 | expect(readFileSync(join(box.dir, ".job.last"), "utf8")).toBe("0"); | |
| 133 | // The up it reaped is now current: its sha was read in the second look. | |
| 134 | expect(readFileSync(join(box.dir, ".compose-sha"), "utf8")).toBe(sha("sha256", compose)); | |
| 135 | }, 60_000); | |
| 136 | ||
| 111 | 137 | test("without jars, an up is what it was", async () => { |
| 112 | 138 | const box = made({ jars: false }); |
| 113 | 139 | await box.run(); |
new above
offline
This passes without the agent.sh fix, because
.job.lastcomes fromrc, not fromidorsha. Assert.compose-shaequals the sha of the compose the box was given (passcomposetomade), and check it fails with the fix reverted.