Skip to content

Commit e34c2a9

Browse files
committed
fix(server): a remote update points the service unit at the runtime it committed
1 parent 348f217 commit e34c2a9

5 files changed

Lines changed: 264 additions & 12 deletions

File tree

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
SERVICE_LAUNCHER_PROTOCOL,
2525
SERVICE_RESTART_PENDING_FILE,
2626
SERVICE_STATE_FILE,
27+
SERVICE_UNIT_ENV,
2728
compareExactServiceVersions,
2829
parseServiceState,
2930
serviceStateActiveVersion,
@@ -37,7 +38,6 @@ const BOOT_SERVICE_UNIT_FILE = `${BOOT_SERVICE_NAME}.service`;
3738
// (com.t3tools.t3code), so launchd and TCC records never collide.
3839
const BOOT_SERVICE_LAUNCHD_LABEL = "com.t3tools.t3code.service";
3940
const BOOT_SERVICE_PLIST_FILE = `${BOOT_SERVICE_LAUNCHD_LABEL}.plist`;
40-
const BOOT_SERVICE_UNIT_ENV = "T3_BOOT_SERVICE_UNIT";
4141
/** File in the logs dir that receives the service's stdout and stderr. `t3 triage` points agents at it. */
4242
export const BOOT_SERVICE_LOG_FILE = "boot-service.log";
4343

@@ -101,7 +101,7 @@ export function renderBootServiceUnit(plan: BootServicePlan): string {
101101
"Type=simple",
102102
"WorkingDirectory=%h",
103103
`Environment=T3CODE_HOME=${quoteSystemdValue(plan.baseDir)}`,
104-
`Environment=${BOOT_SERVICE_UNIT_ENV}=${BOOT_SERVICE_UNIT_FILE}`,
104+
`Environment=${SERVICE_UNIT_ENV}=${BOOT_SERVICE_UNIT_FILE}`,
105105
`ExecStart=${plan.program.map(quoteSystemdValue).join(" ")}`,
106106
// Let the launcher mark an explicit stop before it signals the server.
107107
// systemd still SIGKILLs the whole cgroup if graceful shutdown times out.
@@ -170,7 +170,7 @@ export function renderBootServicePlist(
170170
` <string>${escapeXmlText(options.environmentPath)}</string>`,
171171
` <key>T3CODE_HOME</key>`,
172172
` <string>${escapeXmlText(plan.baseDir)}</string>`,
173-
` <key>${BOOT_SERVICE_UNIT_ENV}</key>`,
173+
` <key>${SERVICE_UNIT_ENV}</key>`,
174174
` <string>${BOOT_SERVICE_PLIST_FILE}</string>`,
175175
` </dict>`,
176176
` <key>WorkingDirectory</key>`,
@@ -824,7 +824,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
824824
// launcher reads the state file once at startup and the unit only matters
825825
// on the next start, so that is safe as long as the launcher is not in
826826
// the middle of a remote update, which is the one time it writes the
827-
// state file itself. That case is refused below, before anything is
827+
// state file itself (and, once committed, repoints the unit at the target;
828+
// it leaves a unit that already names a newer version alone). That case
829+
// is refused below, before anything is
828830
// written, from the same read the downgrade check uses; the stop that
829831
// normally serialises against the launcher is skipped on purpose.
830832
const start = options?.start !== false;

‎apps/server/src/cloud/serviceProtocol.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import type { ServerSelfUpdateOutcome } from "@t3tools/contracts";
55
export const SERVICE_LAUNCHER_PROTOCOL = 3 as const;
66
export const SERVICE_LAUNCHER_CONTEXT_ENV = "T3_SERVICE_LAUNCHER_CONTEXT";
77
export const SERVICE_STATE_FILE = "service-state.json";
8+
/** Set by the unit or launch agent to its own file name, so the launcher can
9+
find the definition that started it. */
10+
export const SERVICE_UNIT_ENV = "T3_BOOT_SERVICE_UNIT";
811
/** Written by the launcher just before an explicit stop kills its child, so
912
the child can tell "the service is going away" from "the launcher is about
1013
to start my replacement" while a pending update is recorded. */

‎apps/server/src/serviceLauncher.test.ts‎

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import * as FileSystem from "effect/FileSystem";
99
import * as Path from "effect/Path";
1010
import { vi } from "vite-plus/test";
1111

12+
import { renderBootServicePlist, renderBootServiceUnit } from "./cloud/bootService.ts";
1213
import {
1314
databaseBackupBlocker,
1415
Launcher,
1516
PREPARED_TIMEOUT_MS,
1617
type BackupSpaceProbe,
1718
readServiceState,
19+
repointServiceUnit,
20+
type ServiceUnit,
1821
writeServiceState,
1922
} from "./serviceLauncher.ts";
2023
import {
@@ -107,6 +110,35 @@ it("rejects contradictory service state", () => {
107110
);
108111
});
109112

113+
const unitPlan = (root: string, version: string) => ({
114+
program: [`${root}/runtime/versions/${version}/t3`, "__service-launcher"],
115+
baseDir: root,
116+
logPath: `${root}/logs/boot-service.log`,
117+
unitPath: `${root}/unit/t3code.service`,
118+
});
119+
120+
it("points a unit or launch agent at another runtime and leaves the rest alone", () => {
121+
for (const root of ["/home/user/.t3", "/home/user name/.t3", "/home/100%/.t3"]) {
122+
assert.equal(
123+
repointServiceUnit(renderBootServiceUnit(unitPlan(root, "1.0.0")), "1.1.0"),
124+
renderBootServiceUnit(unitPlan(root, "1.1.0")),
125+
);
126+
}
127+
const root = "/Users/user/.t3 & co";
128+
const plistOptions = {
129+
homeDir: "/Users/user",
130+
environmentPath: `/usr/bin:${root}/runtime/versions/1.0.0`,
131+
};
132+
assert.equal(
133+
repointServiceUnit(renderBootServicePlist(unitPlan(root, "1.0.0"), plistOptions), "1.1.0"),
134+
renderBootServicePlist(unitPlan(root, "1.1.0"), plistOptions),
135+
);
136+
for (const version of ["1.1.0", "1.2.0"]) {
137+
const unit = renderBootServiceUnit(unitPlan(root, version));
138+
assert.equal(repointServiceUnit(unit, "1.1.0"), unit);
139+
}
140+
});
141+
110142
// A pinned runtime is an executable at <versionDir>/t3. The tests stand one up
111143
// as a Node shebang script so the launcher spawns it the way it spawns the
112144
// real single-executable, IPC channel included.
@@ -128,6 +160,28 @@ const writeFakeRuntime = (
128160
return entryPath;
129161
});
130162

163+
// The unit the launcher repoints after a commit, written where its plan says.
164+
const fakeServiceUnit = (
165+
fs: FileSystem.FileSystem,
166+
path: Path.Path,
167+
root: string,
168+
version: string,
169+
) =>
170+
Effect.gen(function* () {
171+
const plan = unitPlan(root, version);
172+
yield* fs.makeDirectory(path.dirname(plan.unitPath), { recursive: true });
173+
yield* fs.writeFileString(plan.unitPath, renderBootServiceUnit(plan));
174+
const unit: ServiceUnit & { reloads: number } = {
175+
path: plan.unitPath,
176+
reloads: 0,
177+
reload: () => {
178+
unit.reloads += 1;
179+
return Promise.resolve();
180+
},
181+
};
182+
return unit;
183+
});
184+
131185
it.layer(NodeServices.layer)("service state persistence", (it) => {
132186
it.effect("durably replaces and strictly reads one state document", () =>
133187
Effect.gen(function* () {
@@ -298,8 +352,15 @@ if (context.update?.status === "pending") {
298352
activeVersion: "1.0.0",
299353
}),
300354
);
355+
const unit = yield* fakeServiceUnit(fs, path, root, "1.0.0");
301356

302-
const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
357+
const launcher = new Launcher(
358+
root,
359+
yield* Effect.promise(() => readServiceState(statePath)),
360+
{
361+
serviceUnit: unit,
362+
},
363+
);
303364
yield* Effect.promise(() =>
304365
launcher.run().then(
305366
() => Promise.reject(new Error("launcher unexpectedly completed")),
@@ -310,6 +371,77 @@ if (context.update?.status === "pending") {
310371
const state = yield* Effect.promise(() => readServiceState(statePath));
311372
assert.equal(state.activeVersion, "1.1.0");
312373
assert.equal(state.update?.status, "committed");
374+
assert.equal(
375+
yield* fs.readFileString(unit.path),
376+
renderBootServiceUnit(unitPlan(root, "1.1.0")),
377+
);
378+
assert.equal(unit.reloads, 1);
379+
}),
380+
);
381+
382+
it.effect("commits even when the unit cannot be repointed", () =>
383+
Effect.gen(function* () {
384+
const fs = yield* FileSystem.FileSystem;
385+
const path = yield* Path.Path;
386+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-unit-" });
387+
const statePath = path.join(root, "runtime", "service-state.json");
388+
const databasePath = path.join(root, "userdata", "state.sqlite");
389+
yield* fs.makeDirectory(path.dirname(databasePath), { recursive: true });
390+
yield* fs.writeFileString(databasePath, "before trial");
391+
const encodedDatabasePath = JSON.stringify(databasePath);
392+
const childSource = `
393+
const context = JSON.parse(process.env.T3_SERVICE_LAUNCHER_CONTEXT);
394+
if (context.update?.status === "pending") {
395+
process.send({ type: "prepared", updateId: context.update.id });
396+
process.on("message", (message) => {
397+
if (message.type === "committed") process.exit(0);
398+
});
399+
} else if (context.update === undefined) {
400+
process.send({ type: "request-update", targetVersion: "1.1.0", dbPath: ${encodedDatabasePath} });
401+
setInterval(() => {}, 1_000);
402+
} else {
403+
process.exit(0);
404+
}
405+
`;
406+
for (const version of ["1.0.0", "1.1.0"]) {
407+
yield* writeFakeRuntime(
408+
fs,
409+
path,
410+
path.join(root, "runtime", "versions", version),
411+
childSource,
412+
);
413+
}
414+
yield* Effect.promise(() =>
415+
writeServiceState(statePath, {
416+
protocol: SERVICE_LAUNCHER_PROTOCOL,
417+
activeVersion: "1.0.0",
418+
}),
419+
);
420+
let reloads = 0;
421+
const launcher = new Launcher(
422+
root,
423+
yield* Effect.promise(() => readServiceState(statePath)),
424+
{
425+
serviceUnit: {
426+
path: path.join(root, "unit", "missing.service"),
427+
reload: () => {
428+
reloads += 1;
429+
return Promise.resolve();
430+
},
431+
},
432+
},
433+
);
434+
yield* Effect.promise(() =>
435+
launcher.run().then(
436+
() => Promise.reject(new Error("launcher unexpectedly completed")),
437+
() => Promise.resolve(),
438+
),
439+
);
440+
441+
const state = yield* Effect.promise(() => readServiceState(statePath));
442+
assert.equal(state.activeVersion, "1.1.0");
443+
assert.equal(state.update?.status, "committed");
444+
assert.equal(reloads, 0);
313445
}),
314446
);
315447

@@ -348,8 +480,15 @@ if (context.update?.status === "pending") {
348480
activeVersion: "1.0.0",
349481
}),
350482
);
483+
const unit = yield* fakeServiceUnit(fs, path, root, "1.0.0");
351484

352-
const launcher = new Launcher(root, yield* Effect.promise(() => readServiceState(statePath)));
485+
const launcher = new Launcher(
486+
root,
487+
yield* Effect.promise(() => readServiceState(statePath)),
488+
{
489+
serviceUnit: unit,
490+
},
491+
);
353492
yield* Effect.promise(() =>
354493
launcher.run().then(
355494
() => Promise.reject(new Error("launcher unexpectedly completed")),
@@ -360,6 +499,11 @@ if (context.update?.status === "pending") {
360499
const state = yield* Effect.promise(() => readServiceState(statePath));
361500
assert.equal(state.activeVersion, "1.0.0");
362501
assert.equal(state.update?.status, "rolled-back");
502+
assert.equal(
503+
yield* fs.readFileString(unit.path),
504+
renderBootServiceUnit(unitPlan(root, "1.0.0")),
505+
);
506+
assert.equal(unit.reloads, 0);
363507
assert.equal(
364508
state.update?.status === "rolled-back" ? state.update.reason : undefined,
365509
"invalid-prepared",

0 commit comments

Comments
 (0)