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

A paused app's service port is released and says so on its page #16787

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/15111-a-paused-app-s-service-port-is-released → main 0 commits fork 2 threads · 2 unresolved

  • Merged. torque/15111-a-paused-app-s-service-port-is-released was merged into main as ba6975c.

Review

Sign in to review this change.
9 files changed +169 −3
docs/APPS.md +6
@@ -2062,6 +2062,12 @@ or archiving the app stops the process and its children and ends the
20622062 restarts. A deploy is refused when another app's service already holds a declared port, naming both (`deployPortRefusal`); an app's own previews and production never clash. Only services live in this forge process hold ports, so a stopped or asleep service holds none, and the answer depends on what is awake.
20632063 Exposing the ports and syncing volumes are not built yet (issue #391).
20642064
2065 Stop parks a service: its ports are free for another app, and the app page and the terminal say
2066 "parked: port 25565 released for other apps" (`src/apps/service_ports.ts`; how-to in
2067 `docs/features/service-parked.md`). A redeploy takes the ports back, or is refused by
2068 `deployPortRefusal` naming the holder, and the app stays parked; the mark clears when a deploy
2069 lands or the environment is removed, and lasts until the forge restarts.
2070
20652071 ## The access log
20662072
20672073 The forge keeps one compact JSON line per request that is not a static asset
docs/features/service-parked.md added +16
@@ -0,0 +1,16 @@
1 ---
2 topic: Your apps
3 row: A stopped service app is parked and its ports are free for others
4 ---
5 # A stopped service app is parked and its ports are free for others
6
7 This page is for app authors. A service declares TCP ports in `forge.json` (`app.ports`). While it runs, its production holds them; no other app's deploy may declare one.
8
9 ## What happens
10
11 - **Stop.** Stopping the service parks it. Its ports are released at once, so another app may deploy with them.
12 - **Where you see it.** The app page shows, under "Deploy and log", and the terminal's Deploys screen shows under the production heading: `parked: port 25565 released for other apps; a redeploy takes it back, or says who holds it`.
13 - **Redeploy.** A redeploy takes the ports back. If another app now holds one, the deploy fails with `forge.json: port 25565 is already held by <app>; <yours> cannot declare it`, and your app stays parked. Stop that app, or change `app.ports`, then deploy again.
14 - **Clears.** The note goes when a deploy lands, or when the environment is removed.
15
16 Only services running in this forge hold ports, so the answer depends on what is up. Two deploys started in the same instant can both pass; the one that cannot bind says so. A parked mark is not kept across a forge restart.
src/apps/runtime_deploy.ts +1
@@ -355,6 +355,7 @@ export async function execute(
355355 deployStartedAt,
356356 };
357357 state.live = live;
358 state.parked = undefined;
358359 rt.awake(state, servingAt);
359360 state.asleep = undefined;
360361 state.lastRequestAt = rt.now();
src/apps/runtime_env.ts +10 −3
@@ -18,6 +18,7 @@ import { envVariant, envPull, type EnvName } from "./data.ts";
1818 import type { AppManifest } from "./manifest.ts";
1919 import type { CellSpec } from "./contain.ts";
2020 import type { ProcessHandle, StartError } from "./process.ts";
21 import type { Parked } from "./service_ports.ts";
2122
2223 export const log = createLog("apps");
2324
@@ -168,9 +169,10 @@ export interface EnvStatus {
168169 readonly error?: string | undefined;
169170 /**
170171 * A running environment whose deploy is still checking itself: its tests
171 * are still running, or ran and failed. Never set while `state` is
172 * anything but `running` — a failed test never stops a deployment that
173 * already answers.
172 * are still running, or ran and failed; or a running service's restart
173 * words; or, on a stopped or failed environment, a parked service's
174 * released ports (`apps/service_ports.ts`). A failed test never stops a
175 * deployment that already answers.
174176 */
175177 readonly note?: string | undefined;
176178 /**
@@ -341,6 +343,11 @@ export interface Environment {
341343 dormant?:
342344 | { since: string; idleSince: string; commitOid: string; prepared?: true }
343345 | undefined;
346 /**
347 * A service stopped by its owner: the ports it gave up (`apps/service_ports.ts`); cleared when it
348 * is live again.
349 */
350 parked?: Parked | undefined;
344351 /** Why the last wake did not happen (the owner's preview limit), for the page a visitor sees. */
345352 wakeRefused?: string | undefined;
346353 /** One wake at a time; every request that arrives meanwhile waits on it. */
src/apps/runtime_status.ts +2
@@ -6,6 +6,7 @@
66
77 import { cellHides, grantsHere } from "../spawn/hosted-mark.ts";
88 import { serviceNote } from "./supervise.ts";
9 import { parkedWords } from "./service_ports.ts";
910 import { VERIFYING_NOTE } from "./env_phrase.ts";
1011 import { join } from "node:path";
1112 import type { AppRecord } from "../core/apps.ts";
@@ -152,6 +153,7 @@ export function describe(
152153 url,
153154 runId: state.lastRunId,
154155 error: state.lastError,
156 ...(state.parked === undefined ? {} : { note: parkedWords(state.parked) }),
155157 };
156158 }
157159
src/apps/runtime_teardown.ts +4
@@ -10,6 +10,7 @@ import { cancelRun } from "../core/runs.ts";
1010 import { removeApp, removeEnv, type EnvName } from "./data.ts";
1111 import type { AppsRuntime } from "./runtime.ts";
1212 import { EnvStop, key } from "./runtime_env.ts";
13 import { heldPorts } from "./service_ports.ts";
1314
1415 export async function teardown(rt: AppsRuntime, record: AppRecord, env: EnvName): Promise<void> {
1516 const k = key(record.id, env);
@@ -29,6 +30,7 @@ export async function teardown(rt: AppsRuntime, record: AppRecord, env: EnvName)
2930 // after its pull closed, checkout, cloned data and all.
3031 const live = state.live;
3132 state.live = undefined;
33 state.parked = undefined;
3234 rt.resting(state);
3335 revokeAppToken(rt.app.db, live?.tokenId);
3436 if (live) await live.handle.stop();
@@ -61,6 +63,8 @@ export async function stopEnv(rt: AppsRuntime, record: AppRecord, env: EnvName):
6163 state.deploying?.abort.abort(new EnvStop("stopped"));
6264 await state.chain.catch(() => {});
6365 const live = state.live;
66 const held = heldPorts(state);
67 if (held.length > 0) state.parked = { ports: held, since: new Date().toISOString() };
6468 state.live = undefined;
6569 rt.resting(state);
6670 state.asleep = undefined;
src/apps/service_ports.ts added +30
@@ -0,0 +1,30 @@
1 /**
2 * Who holds a service's ports. A service app's declared TCP ports (`app.ports`,
3 * `apps/service.ts`) belong to its production environment while that process is up and to
4 * nobody while it is stopped: the owner's Stop parks it (this module's mark and words) and
5 * its ports are free for another app. The refusal when a deploy declares a port another app
6 * holds is `deployPortRefusal` in `service.ts`, the one check; it reads what is live, so a
7 * stopped service holds none. The mark is what the page and the terminal show meanwhile.
8 */
9
10 import type { Environment } from "./runtime_env.ts";
11
12 /** The ports a stopped service gave up, and when. */
13 export interface Parked {
14 readonly ports: readonly number[];
15 readonly since: string;
16 }
17
18 /** The ports a service environment holds right now; none for anything else. */
19 export function heldPorts(state: Pick<Environment, "env" | "live">): readonly number[] {
20 if (state.env !== "production") return [];
21 const service = state.live?.manifest.service;
22 return service == null ? [] : service.ports.map((p) => p.port);
23 }
24
25 /** What the app page and the terminal say about a parked service. */
26 export function parkedWords(parked: Parked): string {
27 const list = parked.ports.join(", ");
28 const s = parked.ports.length === 1 ? "port" : "ports";
29 return `parked: ${s} ${list} released for other apps; a redeploy takes ${parked.ports.length === 1 ? "it" : "them"} back, or says who holds ${parked.ports.length === 1 ? "it" : "one"}`;
30 }
test/service-parked.test.ts added +94
@@ -0,0 +1,94 @@
1 /**
2 * A paused service's ports (`apps/service_ports.ts`): Stop parks it and the page and the terminal
3 * say so; a redeploy with another app on the port is refused by the one check
4 * (`deployPortRefusal`, `apps/service.ts`) and stays parked; a redeploy with the port free
5 * clears it; removing the environment clears it too.
6 */
7 import { afterAll, beforeAll, expect, spyOn, test } from "bun:test";
8 import type { AppRecord } from "../src/core/apps.ts";
9 import type { Repo } from "../src/core/repos.ts";
10 import type { ServiceSpec } from "../src/apps/service.ts";
11 import { AppsRuntime } from "../src/apps/runtime.ts";
12 import { key, type Environment } from "../src/apps/runtime_env.ts";
13 import { describe as statusOf } from "../src/apps/runtime_status.ts";
14 import { heldPorts, parkedWords } from "../src/apps/service_ports.ts";
15 import { deploySettled, startAppsForge, startHello, stopAppsForge, type AppsForge } from "./helpers/apps-forge.ts";
16
17 let forge: AppsForge;
18 let repo: Repo;
19 let record: AppRecord;
20 let rt: AppsRuntime;
21
22 const service: ServiceSpec = { ports: [{ port: 25565, protocol: "tcp" as const }], alwaysOn: true, placement: null, volumes: [] };
23 const production = (): Environment => {
24 const state = rt.envs.get(key(record.id, "production"));
25 if (state === undefined) throw new Error("no production");
26 return state;
27 };
28
29 beforeAll(async () => {
30 forge = await startAppsForge();
31 rt = forge.runtime;
32 ({ repo, record } = await startHello(forge));
33 });
34 afterAll(() => stopAppsForge(forge));
35
36 test("heldPorts: a running service's production holds its ports; a preview and a web app hold none", () => {
37 const live = production().live!;
38 const as = (env: Environment["env"], svc: ServiceSpec | null) =>
39 heldPorts({ env, live: { ...live, manifest: { ...live.manifest, service: svc } } });
40 expect(as("production", service)).toEqual([25565]);
41 expect(as("pull-1", service)).toEqual([]);
42 expect(as("production", null)).toEqual([]);
43 });
44
45 test("pause, clash, resume: parked stays set through a refused deploy and clears on the next that lands", async () => {
46 const state = production();
47 const commit = state.live!.commitOid;
48 // Hello is a web app; make it a service for the stop, as a service's live manifest would be.
49 state.live = { ...state.live!, manifest: { ...state.live!.manifest, service } };
50 await rt.stopEnv(record, "production");
51 expect(state.live).toBeUndefined();
52 expect(state.parked?.ports).toEqual([25565]);
53 expect(statusOf(rt, state).note).toBe(parkedWords(state.parked!));
54 expect(statusOf(rt, state).note).toContain("port 25565 released for other apps");
55 // The terminal shows the same `note`: test/tui-deploys-service-note.test.ts.
56
57 // Another app's production takes the port; the redeploy declares it again.
58 const holderKey = key("mc-id", "production");
59 rt.envs.set(holderKey, {
60 app: { id: "mc-id", slug: "mc" },
61 env: "production",
62 live: { manifest: { service } },
63 } as unknown as Environment);
64 const declare = spyOn(rt, "heldFor").mockImplementation(async (_r, _e, own) => ({ ...own, service }));
65 const run = rt.requestDeploy(record, repo, "production", commit, "test");
66 expect(run).toBeDefined();
67 const refused = await deploySettled(forge.app, repo.id, (r) => r.id === run!.id);
68 expect(refused.state).toBe("failed");
69 expect(state.lastError).toBe("forge.json: port 25565 is already held by mc; hello cannot declare it");
70 expect(state.live).toBeUndefined();
71 expect(state.parked?.ports).toEqual([25565]);
72 expect(statusOf(rt, state).state).toBe("failed");
73 expect(statusOf(rt, state).note).toContain("parked");
74
75 // The holder lets go; the same declaration is now accepted, and the process that comes up clears
76 // it.
77 rt.envs.delete(holderKey);
78 declare.mockRestore();
79 const again = rt.requestDeploy(record, repo, "production", commit, "test");
80 const landed = await deploySettled(forge.app, repo.id, (r) => r.id === again!.id);
81 expect(landed.state).toBe("ok");
82 expect(state.parked).toBeUndefined();
83 expect(state.lastError).toBeUndefined();
84 expect(statusOf(rt, state).note).toBeUndefined();
85 });
86
87 test("removing the environment clears the parked mark", async () => {
88 const state = production();
89 state.live = { ...state.live!, manifest: { ...state.live!.manifest, service } };
90 await rt.stopEnv(record, "production");
91 expect(state.parked).toBeDefined();
92 await rt.teardown(record, "production");
93 expect(state.parked).toBeUndefined();
94 });
test/tui-deploys-service-note.test.ts +6
@@ -6,6 +6,7 @@ import { join } from "node:path";
66 import { createRepo } from "../src/actions/repos.ts";
77 import { createApp, type App as Forge } from "../src/app.ts";
88 import { AppsRuntime } from "../src/apps/runtime.ts";
9 import { parkedWords } from "../src/apps/service_ports.ts";
910 import { createApp as createAppRecord } from "../src/core/apps.ts";
1011 import type { Repo } from "../src/core/repos.ts";
1112 import type { User } from "../src/core/users.ts";
@@ -69,3 +70,8 @@ test("the terminal shows a service's restart note, and nothing when there is non
6970 note = "service restarted 2 times after exit code 2";
7071 expect(await frame()).toContain("service restarted 2 times after exit code 2");
7172 });
73
74 test("the terminal shows a parked service's words, as the app page does", async () => {
75 note = parkedWords({ ports: [25565], since: "2026-11-30T00:00:00.000Z" });
76 expect(await frame()).toContain("port 25565 released for other apps");
77 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs