Public: anyone can read this; the crew can change it.
Previews of closed and merged pulls stay deployed and get redeployed #16595
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16594-previews-of-closed-and-merged-pulls-stay → main 0 commits fork 1 thread · 1 unresolved
3 files
changed +84
−1
src/apps/runtime.ts +6 −1
| @@ -44,7 +44,7 @@ import { appendRunLog } from "../runs/log.ts"; | ||
| 44 | 44 | import { primaryDomain, primaryHosts } from "../core/domains.ts"; |
| 45 | 45 | import { closeAllAwake, closeOrphans } from "../core/meter.ts"; |
| 46 | 46 | import { drainPort } from "./handoff.ts"; |
| 47 | import { appsRoot, envBuild, variantEnv, envHostLabel, isEnvName, previewEnv, type EnvName } from "./data.ts"; | |
| 47 | import { appsRoot, envBuild, envPull, variantEnv, envHostLabel, isEnvName, previewEnv, type EnvName } from "./data.ts"; | |
| 48 | 48 | import { noteProtection } from "../kernel/protection.ts"; |
| 49 | 49 | import { cellsAvailable } from "./contain.ts"; |
| 50 | 50 | import { during } from "../perf/jobs.ts"; |
| @@ -369,6 +369,11 @@ export class AppsRuntime { | ||
| 369 | 369 | const decided = this.prepare(record, repo, pull, head, reason); |
| 370 | 370 | if (reason === "boot") await decided; |
| 371 | 371 | } |
| 372 | // A preview still running for a pull that is not open stops, whatever is on disk (#16594). | |
| 373 | if (only !== "production") | |
| 374 | for (const state of [...this.envs.values()]) | |
| 375 | if (state.app.id === record.id && envPull(state.env) !== undefined && !open.has(state.env)) | |
| 376 | await this.teardown(record, state.env); | |
| 372 | 377 | // Environments on disk for pulls that are no longer open. |
| 373 | 378 | const root = join(appsRoot(this.app.config.dataDir), record.id); |
| 374 | 379 | if (existsSync(root)) { |
src/apps/runtime_sleep.ts +2
| @@ -107,6 +107,8 @@ export function mayRun( | ||
| 107 | 107 | const number = envPull(env); |
| 108 | 108 | const pull = |
| 109 | 109 | number === undefined ? undefined : getPull(rt.app.db, repo.id, number); |
| 110 | // A closed or merged pull has no preview: nothing starts, wakes or redeploys one (#16594). | |
| 111 | if (pull !== undefined && pull.state !== "open") return false; | |
| 110 | 112 | // A combined preview runs under the head it carries, read from git (#9012). |
| 111 | 113 | const head = |
| 112 | 114 | pull === undefined |
test/apps-closed-pull-no-preview.test.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * A closed or merged pull has no running preview (#16594): nothing deploys or | |
| 3 | * wakes one, and a reconcile stops one that is still up. | |
| 4 | */ | |
| 5 | ||
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { existsSync } from "node:fs"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 13 | import { envDir, previewEnv } from "../src/apps/data.ts"; | |
| 14 | import { newApp } from "../src/actions/apps.ts"; | |
| 15 | import { openPull } from "../src/actions/pulls.ts"; | |
| 16 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 17 | import { getPull } from "../src/core/pulls.ts"; | |
| 18 | import { listRuns } from "../src/core/runs.ts"; | |
| 19 | import { createUser } from "../src/core/users.ts"; | |
| 20 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 21 | ||
| 22 | let dataDir: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let runtime: AppsRuntime; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dataDir = await mkdtemp(join(tmpdir(), "forge-closed-preview-")); | |
| 29 | app = createApp({ dataDir, port: 0 }); | |
| 30 | servers = await serve(app); | |
| 31 | runtime = new AppsRuntime(app, { domain: "apps.test", scheme: "http", publicPort: servers.port, forgeUrl: servers.url, concurrency: 2 }); | |
| 32 | await runtime.start(); | |
| 33 | }, 30_000); | |
| 34 | ||
| 35 | afterAll(async () => { | |
| 36 | await runtime.stop(); | |
| 37 | servers.stop(); | |
| 38 | app.close(); | |
| 39 | await rm(dataDir, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | test("a pull that is not open gets no deploy, and a reconcile stops its running preview", async () => { | |
| 43 | const alice = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 44 | if (!alice.ok) throw new Error(alice.error); | |
| 45 | const made = await newApp(app, alice.value, { name: "closedprev", visibility: "public" }); | |
| 46 | if (!made.ok) throw new Error(String(made.error)); | |
| 47 | const { repo, app: record } = made.value; | |
| 48 | const settled = async (env: string) => { | |
| 49 | const deadline = Date.now() + 60_000; | |
| 50 | while (Date.now() < deadline) { | |
| 51 | const run = listRuns(app.db, repo.id, { env, kind: "deploy", limit: 5 }).find((r) => ["ok", "failed", "cancelled"].includes(r.state)); | |
| 52 | if (run) return run; | |
| 53 | await new Promise((r) => setImmediate(r)); | |
| 54 | } | |
| 55 | throw new Error("no deploy settled"); | |
| 56 | }; | |
| 57 | await settled("production"); | |
| 58 | const committed = await commitToBranch(app, alice.value, repo, { branch: "b", base: repo.defaultBranch, message: "c", files: [{ path: "X.md", content: "x\n" }] }); | |
| 59 | expect(committed.ok).toBe(true); | |
| 60 | const opened = await openPull(app, alice.value, repo, { title: "T", baseBranch: repo.defaultBranch, headBranch: "b" }); | |
| 61 | if (!opened.ok) throw new Error(String(opened.error)); | |
| 62 | const env = previewEnv(opened.value.number); | |
| 63 | await settled(env); | |
| 64 | expect(existsSync(envDir(dataDir, record.id, env))).toBe(true); | |
| 65 | ||
| 66 | // The pull closes without its event reaching the runtime (a lost event, a restart). | |
| 67 | app.db.query("UPDATE issue SET state = 'closed' WHERE repo_id = ? AND number = ?").run(repo.id, opened.value.number); | |
| 68 | expect(getPull(app.db, repo.id, opened.value.number)!.state).toBe("closed"); | |
| 69 | ||
| 70 | const head = getPull(app.db, repo.id, opened.value.number)!.headOid; | |
| 71 | expect(runtime.requestDeploy(record, repo, env, head, "redeploy")).toBeUndefined(); | |
| 72 | ||
| 73 | await runtime.reconcileApp(record, repo, "redeploy"); | |
| 74 | expect(existsSync(envDir(dataDir, record.id, env))).toBe(false); | |
| 75 | expect(runtime.status(record).some((e) => e.env === env)).toBe(false); | |
| 76 | }, 60_000); |
new above
offline
Next: log at debug level here with the app, env and pull state. It would show which path keeps asking for a closed pull's preview, which is the open question from the issue.