fix(server): supervise background service updates
This commit is contained in:
11 files changed
+896
-189
No files matched your search
@@ -2,11 +2,14 @@ import * as Console from "effect/Console";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Terminal from "effect/Terminal";
|
||||
import { Command, GlobalFlag, Prompt } from "effect/unstable/cli";
|
||||
import { Command, Flag, GlobalFlag, Prompt } from "effect/unstable/cli";
|
||||
import { FetchHttpClient } from "effect/unstable/http";
|
||||
|
||||
import packageJson from "../../package.json" with { type: "json" };
|
||||
import * as BootService from "../cloud/bootService.ts";
|
||||
import { applySystemdSelfUpdatePlan } from "../cloud/systemdSelfUpdate.ts";
|
||||
import type * as ServerConfig from "../config.ts";
|
||||
import { validateMigrationIdentities } from "../persistence/Migrations.ts";
|
||||
import * as ProcessRunner from "../processRunner.ts";
|
||||
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
|
||||
|
||||
@@ -142,6 +145,38 @@ const serviceStatusCommand = Command.make("status", projectLocationFlags).pipe(
|
||||
),
|
||||
);
|
||||
|
||||
/** Internal entrypoint launched by systemd-run outside t3code.service. */
|
||||
const serviceApplyUpdateCommand = Command.make("_apply-update", {
|
||||
plan: Flag.string("plan"),
|
||||
}).pipe(
|
||||
Command.withHidden,
|
||||
Command.withHandler(({ plan }) =>
|
||||
applySystemdSelfUpdatePlan(plan).pipe(
|
||||
Effect.provide(Layer.mergeAll(ProcessRunner.layer, FetchHttpClient.layer)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
/** Target-artifact preflight used before the running server is stopped. */
|
||||
const serviceValidateUpdateCommand = Command.make("_validate-update", {
|
||||
database: Flag.string("database"),
|
||||
}).pipe(
|
||||
Command.withHidden,
|
||||
Command.withHandler(({ database }) =>
|
||||
Effect.gen(function* () {
|
||||
// Keep node:sqlite behind the same lazy bundle boundary as normal server
|
||||
// startup; most service commands never open the database.
|
||||
const NodeSqliteClient = yield* Effect.promise(
|
||||
() => import("../persistence/NodeSqliteClient.ts"),
|
||||
);
|
||||
yield* validateMigrationIdentities().pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide(NodeSqliteClient.layer({ filename: database, readonly: true })),
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
export const offerServiceDuringOnboarding = Effect.gen(function* () {
|
||||
const service = yield* BootService.BootService;
|
||||
const { supported, installed, current } = yield* service.status;
|
||||
@@ -195,5 +230,7 @@ export const serviceCommand = Command.make("service").pipe(
|
||||
serviceUninstallCommand,
|
||||
serviceUpdateCommand,
|
||||
serviceStatusCommand,
|
||||
serviceApplyUpdateCommand,
|
||||
serviceValidateUpdateCommand,
|
||||
]),
|
||||
);
|
||||
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as TestClock from "effect/testing/TestClock";
|
||||
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
|
||||
|
||||
@@ -23,36 +24,15 @@ import {
|
||||
renderBootServiceUnit,
|
||||
} from "./bootService.ts";
|
||||
import * as SelfUpdate from "./selfUpdate.ts";
|
||||
import {
|
||||
SYSTEMD_SELF_UPDATE_DIRECTORY,
|
||||
SYSTEMD_SELF_UPDATE_PLAN_FILE,
|
||||
SYSTEMD_SELF_UPDATE_UNIT,
|
||||
SystemdSelfUpdatePlan,
|
||||
} from "./systemdSelfUpdate.ts";
|
||||
|
||||
const NODE_PATH = "/usr/local/bin/node";
|
||||
|
||||
const eventuallyFileString = Effect.fn("test.eventuallyFileString")(function* (
|
||||
filePath: string,
|
||||
expected: string,
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
for (let iteration = 0; iteration < 1_000; iteration += 1) {
|
||||
const contents = yield* fs.readFileString(filePath);
|
||||
if (contents === expected) {
|
||||
return;
|
||||
}
|
||||
// The rollback performs real filesystem I/O on a detached fiber, which
|
||||
// advancing TestClock does not await.
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
return yield* Effect.die(new Error(`Expected file contents were not observed at ${filePath}.`));
|
||||
});
|
||||
|
||||
const eventuallyTrue = Effect.fn("test.eventuallyTrue")(function* (predicate: () => boolean) {
|
||||
for (let iteration = 0; iteration < 1_000; iteration += 1) {
|
||||
if (predicate()) {
|
||||
return;
|
||||
}
|
||||
yield* Effect.yieldNow;
|
||||
}
|
||||
return yield* Effect.die(new Error("Expected condition was not observed."));
|
||||
});
|
||||
|
||||
interface RecordedCommand {
|
||||
readonly command: string;
|
||||
readonly args: ReadonlyArray<string>;
|
||||
@@ -445,7 +425,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
assert.deepEqual(result, { targetVersion: "0.0.29", method: "respawn" });
|
||||
assert.deepEqual(
|
||||
context.commands.map((entry) => entry.command),
|
||||
[NODE_PATH, "npm", NODE_PATH],
|
||||
[NODE_PATH, "npm", NODE_PATH, NODE_PATH],
|
||||
);
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
);
|
||||
@@ -506,6 +486,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
[
|
||||
`npm install --prefix ${context.path.join(context.baseDir, "runtime/versions/0.0.29")} --no-fund --no-audit [email protected]`,
|
||||
`${NODE_PATH} ${pinnedEntry} --version`,
|
||||
`${NODE_PATH} ${pinnedEntry} service _validate-update --database ${context.path.join(context.baseDir, "userdata/state.sqlite")}`,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -521,9 +502,17 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
);
|
||||
|
||||
it.effect("rewrites the systemd unit and restarts the boot service", () =>
|
||||
it.effect("stages the systemd unit and hands activation to a transient service", () =>
|
||||
Effect.gen(function* () {
|
||||
const context = yield* makeContext({ bootService: true });
|
||||
const unitPath = context.path.join(
|
||||
context.home,
|
||||
".config",
|
||||
"systemd",
|
||||
"user",
|
||||
BOOT_SERVICE_UNIT_FILE,
|
||||
);
|
||||
const previousUnit = yield* context.fs.readFileString(unitPath);
|
||||
const result = yield* context.service.update({ targetVersion: "0.0.29" });
|
||||
assert.deepEqual(result, { targetVersion: "0.0.29", method: "boot-service" });
|
||||
|
||||
@@ -531,46 +520,55 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
context.baseDir,
|
||||
"runtime/versions/0.0.29/node_modules/t3/dist/bin.mjs",
|
||||
);
|
||||
const unit = yield* context.fs.readFileString(
|
||||
context.path.join(context.home, ".config", "systemd", "user", "t3code.service"),
|
||||
// The RPC process does not rewrite the unit it is currently running
|
||||
// under. The independent transient service applies the staged unit.
|
||||
assert.equal(yield* context.fs.readFileString(unitPath), previousUnit);
|
||||
const planPath = context.path.join(
|
||||
context.baseDir,
|
||||
"userdata",
|
||||
SYSTEMD_SELF_UPDATE_DIRECTORY,
|
||||
SYSTEMD_SELF_UPDATE_PLAN_FILE,
|
||||
);
|
||||
const plan = yield* context.fs
|
||||
.readFileString(planPath)
|
||||
.pipe(
|
||||
Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(SystemdSelfUpdatePlan))),
|
||||
);
|
||||
assert.equal(plan.previousUnit, previousUnit);
|
||||
assert.include(plan.nextUnit, `ExecStart=${NODE_PATH} ${pinnedEntry} serve`);
|
||||
assert.equal(
|
||||
plan.runtimeStatePath,
|
||||
context.path.join(context.baseDir, "userdata/server-runtime.json"),
|
||||
);
|
||||
assert.include(unit, `ExecStart=${NODE_PATH} ${pinnedEntry} serve`);
|
||||
assert.deepEqual(
|
||||
context.commands.map((entry) => entry.command),
|
||||
["npm", NODE_PATH, "systemctl"],
|
||||
["npm", NODE_PATH, NODE_PATH, "systemd-run"],
|
||||
);
|
||||
assert.deepEqual(context.commands[2]?.args, ["--user", "daemon-reload"]);
|
||||
|
||||
// Restart waits until after the update acknowledgement can flush.
|
||||
yield* TestClock.adjust(Duration.seconds(10));
|
||||
assert.deepEqual(context.commands[3], {
|
||||
command: "systemctl",
|
||||
args: ["--user", "restart", "--no-block", "t3code.service"],
|
||||
});
|
||||
assert.include(context.commands[3]?.args ?? [], `--unit=${SYSTEMD_SELF_UPDATE_UNIT}`);
|
||||
assert.include(context.commands[3]?.args ?? [], planPath);
|
||||
assert.lengthOf(context.spawns, 0);
|
||||
// systemd replaces the process; the server must not exit itself.
|
||||
assert.equal(context.exitCount(), 0);
|
||||
|
||||
// The queued restart returns while this process is still shutting
|
||||
// down; the lock must stay held so a second update cannot rewrite the
|
||||
// unit mid-teardown.
|
||||
// The lock stays held after a successful handoff because the transient
|
||||
// service is now responsible for replacing this process.
|
||||
const concurrentError = yield* context.service
|
||||
.update({ targetVersion: "0.0.30" })
|
||||
.pipe(Effect.flip);
|
||||
assert.include(concurrentError.reason, "already in progress");
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("restores the previous unit and permits a retry when systemd restart fails", () =>
|
||||
it.effect("leaves the live unit untouched and permits a retry when the handoff fails", () =>
|
||||
Effect.gen(function* () {
|
||||
let failRestart = true;
|
||||
let failHandoff = true;
|
||||
const context = yield* makeContext({
|
||||
bootService: true,
|
||||
failWhen: (command, args) => {
|
||||
if (command !== "systemctl" || args[1] !== "restart" || !failRestart) {
|
||||
if (command !== "systemd-run" || !failHandoff) {
|
||||
return false;
|
||||
}
|
||||
failRestart = false;
|
||||
assert.include(args, `--unit=${SYSTEMD_SELF_UPDATE_UNIT}`);
|
||||
failHandoff = false;
|
||||
return true;
|
||||
},
|
||||
});
|
||||
@@ -583,29 +581,20 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
);
|
||||
const previousUnit = yield* context.fs.readFileString(unitPath);
|
||||
|
||||
const first = yield* context.service.update({ targetVersion: "0.0.29" });
|
||||
assert.deepEqual(first, { targetVersion: "0.0.29", method: "boot-service" });
|
||||
yield* TestClock.adjust(Duration.seconds(10));
|
||||
yield* eventuallyFileString(unitPath, previousUnit);
|
||||
yield* eventuallyTrue(() => context.commands.at(-1)?.args[1] === "daemon-reload");
|
||||
assert.deepEqual(
|
||||
context.commands.slice(-2).map((entry) => entry.args),
|
||||
[
|
||||
["--user", "restart", "--no-block", BOOT_SERVICE_UNIT_FILE],
|
||||
["--user", "daemon-reload"],
|
||||
],
|
||||
);
|
||||
const first = yield* context.service.update({ targetVersion: "0.0.29" }).pipe(Effect.flip);
|
||||
assert.include(first.reason, "Systemd rejected the update handoff");
|
||||
assert.equal(yield* context.fs.readFileString(unitPath), previousUnit);
|
||||
|
||||
const retry = yield* context.service.update({ targetVersion: "0.0.30" });
|
||||
assert.deepEqual(retry, { targetVersion: "0.0.30", method: "boot-service" });
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("restores the previous systemd unit when daemon-reload fails", () =>
|
||||
it.effect("rejects a target that cannot validate the current database", () =>
|
||||
Effect.gen(function* () {
|
||||
const context = yield* makeContext({
|
||||
bootService: true,
|
||||
failWhen: (command) => command === "systemctl",
|
||||
failWhen: (command, args) => command === NODE_PATH && args.includes("_validate-update"),
|
||||
});
|
||||
const unitPath = context.path.join(
|
||||
context.home,
|
||||
@@ -617,16 +606,15 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
|
||||
const previousUnit = yield* context.fs.readFileString(unitPath);
|
||||
|
||||
const error = yield* context.service.update({ targetVersion: "0.0.29" }).pipe(Effect.flip);
|
||||
assert.include(error.reason, "Reloading systemd units failed");
|
||||
assert.include(error.reason, "not compatible with this server's database");
|
||||
assert.equal(yield* context.fs.readFileString(unitPath), previousUnit);
|
||||
assert.deepEqual(
|
||||
context.commands.map((entry) => entry.command),
|
||||
["npm", NODE_PATH, "systemctl", "systemctl"],
|
||||
["npm", NODE_PATH, NODE_PATH],
|
||||
);
|
||||
|
||||
yield* TestClock.adjust(Duration.seconds(10));
|
||||
assert.lengthOf(context.spawns, 0);
|
||||
assert.equal(context.exitCount(), 0);
|
||||
}).pipe(Effect.provide(TestClock.layer())),
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -24,8 +24,8 @@ import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Ref from "effect/Ref";
|
||||
|
||||
import packageJson from "../../package.json" with { type: "json" };
|
||||
import * as ServerConfig from "../config.ts";
|
||||
import { writeFileStringAtomically } from "../atomicWrite.ts";
|
||||
import * as ProcessRunner from "../processRunner.ts";
|
||||
import {
|
||||
BOOT_SERVICE_UNIT_ENV,
|
||||
@@ -34,6 +34,14 @@ import {
|
||||
renderBootServiceUnit,
|
||||
} from "./bootService.ts";
|
||||
import { ensurePinnedRuntimeInstalled, removePinnedRuntimeInstallation } from "./pinnedRuntime.ts";
|
||||
import {
|
||||
SYSTEMD_SELF_UPDATE_DIRECTORY,
|
||||
SYSTEMD_SELF_UPDATE_PLAN_FILE,
|
||||
SYSTEMD_SELF_UPDATE_RECEIPT_FILE,
|
||||
SYSTEMD_SELF_UPDATE_UNIT,
|
||||
SystemdSelfUpdatePlan,
|
||||
writeSystemdSelfUpdatePlan,
|
||||
} from "./systemdSelfUpdate.ts";
|
||||
|
||||
/**
|
||||
* Lets a connected client replace this server with another published `t3`
|
||||
@@ -221,11 +229,6 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
|
||||
Effect.andThen(restart),
|
||||
Effect.forkDetach({ startImmediately: true }),
|
||||
);
|
||||
const writeUnitAtomically = (filePath: string, contents: string) =>
|
||||
writeFileStringAtomically({ filePath, contents }).pipe(
|
||||
Effect.provideService(FileSystem.FileSystem, fs),
|
||||
Effect.provideService(Path.Path, path),
|
||||
);
|
||||
|
||||
const update: ServerSelfUpdate["Service"]["update"] = Effect.fn(
|
||||
"cloud.server_self_update.update",
|
||||
@@ -301,6 +304,33 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
|
||||
);
|
||||
}
|
||||
|
||||
// Run this command from the target artifact while the known-good server
|
||||
// is still alive. Older artifacts without the compatibility check, or a
|
||||
// build whose migration identities disagree with this database, cannot
|
||||
// be activated remotely.
|
||||
const databasePreflight = yield* runner
|
||||
.run({
|
||||
command: host.execPath,
|
||||
args: [
|
||||
runtimePaths.entryPath,
|
||||
"service",
|
||||
"_validate-update",
|
||||
"--database",
|
||||
serverConfig.dbPath,
|
||||
],
|
||||
timeout: PREFLIGHT_TIMEOUT,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError((cause) =>
|
||||
failWith(`Could not verify t3@${targetVersion} against this database.`, cause),
|
||||
),
|
||||
);
|
||||
if (databasePreflight.code !== 0) {
|
||||
return yield* failWith(
|
||||
`The installed t3@${targetVersion} is not compatible with this server's database (exit code ${String(databasePreflight.code)}).`,
|
||||
);
|
||||
}
|
||||
|
||||
if (activeMethod === "boot-service") {
|
||||
const homeDir = env.HOME ?? "";
|
||||
const unitPath = path.join(homeDir, ".config", "systemd", "user", BOOT_SERVICE_UNIT_FILE);
|
||||
@@ -311,98 +341,63 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
|
||||
);
|
||||
// Same shape bootService.install writes, so host lifecycle commands
|
||||
// still recognize the unit as current.
|
||||
const unit = renderBootServiceUnit({
|
||||
const nextUnit = renderBootServiceUnit({
|
||||
nodePath: host.execPath,
|
||||
t3EntryPath: runtimePaths.entryPath,
|
||||
baseDir: serverConfig.baseDir,
|
||||
logPath: path.join(serverConfig.logsDir, "boot-service.log"),
|
||||
unitPath,
|
||||
});
|
||||
yield* writeUnitAtomically(unitPath, unit).pipe(
|
||||
Effect.mapError((cause) => failWith("Could not update the systemd unit.", cause)),
|
||||
);
|
||||
|
||||
const reloadSystemd = Effect.fn("cloud.server_self_update.reload_systemd")(function* () {
|
||||
const reload = yield* runner
|
||||
.run({ command: "systemctl", args: ["--user", "daemon-reload"] })
|
||||
.pipe(Effect.mapError((cause) => failWith("Could not reload systemd units.", cause)));
|
||||
if (reload.code !== 0) {
|
||||
return yield* failWith(
|
||||
`Reloading systemd units failed (exit code ${String(reload.code)}).`,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
yield* reloadSystemd().pipe(
|
||||
Effect.catch((reloadError) =>
|
||||
writeUnitAtomically(unitPath, previousUnit).pipe(
|
||||
Effect.mapError((rollbackCause) =>
|
||||
failWith("Could not restore the previous systemd unit.", {
|
||||
reloadError,
|
||||
rollbackCause,
|
||||
}),
|
||||
),
|
||||
// Systemd should still have the old unit in memory after the
|
||||
// failed reload, but retry after restoring in case it applied a
|
||||
// partial update before returning an error.
|
||||
Effect.andThen(reloadSystemd().pipe(Effect.ignore)),
|
||||
Effect.andThen(Effect.fail(reloadError)),
|
||||
),
|
||||
),
|
||||
);
|
||||
yield* Effect.logInfo("Server self-update installed; restarting boot service.", {
|
||||
const updateDirectory = path.join(serverConfig.stateDir, SYSTEMD_SELF_UPDATE_DIRECTORY);
|
||||
const planPath = path.join(updateDirectory, SYSTEMD_SELF_UPDATE_PLAN_FILE);
|
||||
const plan = new SystemdSelfUpdatePlan({
|
||||
version: 1,
|
||||
fromVersion: packageJson.version,
|
||||
targetVersion,
|
||||
currentPid: process.pid,
|
||||
unitPath,
|
||||
previousUnit,
|
||||
nextUnit,
|
||||
runtimeStatePath: serverConfig.serverRuntimeStatePath,
|
||||
receiptPath: path.join(updateDirectory, SYSTEMD_SELF_UPDATE_RECEIPT_FILE),
|
||||
});
|
||||
// Restart after the acknowledgement has had time to cross any relay
|
||||
// hop. --no-block queues the restart job and exits before systemd
|
||||
// stops this unit: a blocking restart's SIGTERM reaches the systemctl
|
||||
// child (it shares this service's cgroup), which read as a restart
|
||||
// failure and rolled the new unit back while the old server finished
|
||||
// shutting down. With the handoff race gone, a non-zero exit or spawn
|
||||
// error means systemd genuinely rejected the job while this process is
|
||||
// still alive, so restoring the previous unit below stays correct.
|
||||
yield* scheduleRestart(
|
||||
Effect.gen(function* () {
|
||||
const restart = yield* runner
|
||||
.run({
|
||||
command: "systemctl",
|
||||
args: ["--user", "restart", "--no-block", BOOT_SERVICE_UNIT_FILE],
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError((cause) =>
|
||||
failWith("Could not restart the systemd boot service.", cause),
|
||||
),
|
||||
);
|
||||
if (restart.code !== 0) {
|
||||
return yield* failWith(
|
||||
`Restarting the systemd boot service failed (exit code ${String(restart.code)}).`,
|
||||
);
|
||||
}
|
||||
}).pipe(
|
||||
Effect.catch((restartError) =>
|
||||
writeUnitAtomically(unitPath, previousUnit).pipe(
|
||||
Effect.andThen(reloadSystemd()),
|
||||
Effect.mapError((rollbackError) =>
|
||||
failWith("Could not restore the previous systemd unit.", {
|
||||
restartError,
|
||||
rollbackError,
|
||||
}),
|
||||
),
|
||||
Effect.andThen(Effect.fail(restartError)),
|
||||
),
|
||||
),
|
||||
Effect.catch((error) =>
|
||||
Effect.logError("Server self-update could not restart the boot service.").pipe(
|
||||
Effect.annotateLogs({ targetVersion, error: error.reason }),
|
||||
// Permit a retry only after the failed handoff was rolled
|
||||
// back. A queued restart returns while this process is still
|
||||
// shutting down; releasing the lock then would let a second
|
||||
// update rewrite the unit mid-teardown.
|
||||
Effect.andThen(Ref.set(inFlight, false)),
|
||||
),
|
||||
),
|
||||
),
|
||||
yield* writeSystemdSelfUpdatePlan(planPath, plan).pipe(
|
||||
Effect.provideService(FileSystem.FileSystem, fs),
|
||||
Effect.provideService(Path.Path, path),
|
||||
Effect.mapError((cause) => failWith("Could not stage the systemd update plan.", cause)),
|
||||
);
|
||||
|
||||
// The transient unit has its own cgroup, so it survives the restart of
|
||||
// t3code.service and remains responsible for readiness and rollback.
|
||||
const handoff = yield* runner
|
||||
.run({
|
||||
command: "systemd-run",
|
||||
args: [
|
||||
"--user",
|
||||
"--collect",
|
||||
"--service-type=exec",
|
||||
`--unit=${SYSTEMD_SELF_UPDATE_UNIT}`,
|
||||
host.execPath,
|
||||
host.cliEntryPath,
|
||||
"service",
|
||||
"_apply-update",
|
||||
"--plan",
|
||||
planPath,
|
||||
],
|
||||
timeout: PREFLIGHT_TIMEOUT,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError((cause) => failWith("Could not hand the update to systemd.", cause)),
|
||||
);
|
||||
if (handoff.code !== 0) {
|
||||
return yield* failWith(
|
||||
`Systemd rejected the update handoff (exit code ${String(handoff.code)}).`,
|
||||
);
|
||||
}
|
||||
yield* Effect.logInfo("Server self-update staged; systemd will activate it.", {
|
||||
targetVersion,
|
||||
planPath,
|
||||
});
|
||||
} else {
|
||||
// Spawn the shim before acknowledging the RPC so ENOENT/EACCES and
|
||||
// other launch failures leave this server alive and return a useful
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
|
||||
|
||||
import * as ProcessRunner from "../processRunner.ts";
|
||||
import {
|
||||
SystemdSelfUpdateActivationError,
|
||||
SystemdSelfUpdatePlan,
|
||||
SystemdSelfUpdateReceipt,
|
||||
activateSystemdSelfUpdatePlan,
|
||||
} from "./systemdSelfUpdate.ts";
|
||||
|
||||
interface RecordedCommand {
|
||||
readonly command: string;
|
||||
readonly args: ReadonlyArray<string>;
|
||||
}
|
||||
|
||||
const makeRunnerLayer = (commands: Array<RecordedCommand>) =>
|
||||
Layer.succeed(
|
||||
ProcessRunner.ProcessRunner,
|
||||
ProcessRunner.ProcessRunner.of({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
commands.push({ command: input.command, args: input.args });
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
code: ChildProcessSpawner.ExitCode(0),
|
||||
timedOut: false,
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
};
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
it.layer(NodeServices.layer)("systemd self-update activation", (it) => {
|
||||
const makeFixture = Effect.fn("test.makeSystemdUpdateFixture")(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-systemd-update-test-" });
|
||||
const unitPath = path.join(directory, "t3code.service");
|
||||
const receiptPath = path.join(directory, "receipt.json");
|
||||
const previousUnit = "old unit\n";
|
||||
yield* fs.writeFileString(unitPath, previousUnit);
|
||||
return {
|
||||
fs,
|
||||
unitPath,
|
||||
receiptPath,
|
||||
previousUnit,
|
||||
plan: new SystemdSelfUpdatePlan({
|
||||
version: 1,
|
||||
fromVersion: "0.0.28",
|
||||
targetVersion: "0.0.29",
|
||||
currentPid: 123,
|
||||
unitPath,
|
||||
previousUnit,
|
||||
nextUnit: "new unit\n",
|
||||
runtimeStatePath: path.join(directory, "server-runtime.json"),
|
||||
receiptPath,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
const readReceipt = Effect.fn("test.readSystemdUpdateReceipt")(function* (receiptPath: string) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
return yield* fs
|
||||
.readFileString(receiptPath)
|
||||
.pipe(
|
||||
Effect.flatMap(Schema.decodeUnknownEffect(Schema.fromJsonString(SystemdSelfUpdateReceipt))),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("activates and verifies the new service from the helper", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeFixture();
|
||||
const commands: Array<RecordedCommand> = [];
|
||||
const readinessVersions: Array<string> = [];
|
||||
|
||||
yield* activateSystemdSelfUpdatePlan(
|
||||
fixture.plan,
|
||||
(input) => Effect.sync(() => readinessVersions.push(input.expectedVersion)),
|
||||
{ restartDelay: Duration.zero },
|
||||
).pipe(Effect.provide(makeRunnerLayer(commands)));
|
||||
|
||||
assert.equal(yield* fixture.fs.readFileString(fixture.unitPath), "new unit\n");
|
||||
assert.deepEqual(readinessVersions, ["0.0.29"]);
|
||||
assert.deepEqual(
|
||||
commands.map(({ args }) => args),
|
||||
[
|
||||
["--user", "daemon-reload"],
|
||||
["--user", "restart", "t3code.service"],
|
||||
],
|
||||
);
|
||||
assert.equal((yield* readReceipt(fixture.receiptPath)).phase, "healthy");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("restores and verifies the previous service when the target never becomes ready", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeFixture();
|
||||
const commands: Array<RecordedCommand> = [];
|
||||
const readinessVersions: Array<string> = [];
|
||||
|
||||
yield* activateSystemdSelfUpdatePlan(
|
||||
fixture.plan,
|
||||
(input) =>
|
||||
Effect.sync(() => readinessVersions.push(input.expectedVersion)).pipe(
|
||||
Effect.andThen(
|
||||
input.expectedVersion === fixture.plan.targetVersion
|
||||
? Effect.fail(
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: "target did not become ready",
|
||||
}),
|
||||
)
|
||||
: Effect.void,
|
||||
),
|
||||
),
|
||||
{ restartDelay: Duration.zero },
|
||||
).pipe(Effect.provide(makeRunnerLayer(commands)));
|
||||
|
||||
assert.equal(yield* fixture.fs.readFileString(fixture.unitPath), fixture.previousUnit);
|
||||
assert.deepEqual(readinessVersions, ["0.0.29", "0.0.28"]);
|
||||
assert.deepEqual(
|
||||
commands.map(({ args }) => args),
|
||||
[
|
||||
["--user", "daemon-reload"],
|
||||
["--user", "restart", "t3code.service"],
|
||||
["--user", "daemon-reload"],
|
||||
["--user", "reset-failed", "t3code.service"],
|
||||
["--user", "restart", "t3code.service"],
|
||||
],
|
||||
);
|
||||
const receipt = yield* readReceipt(fixture.receiptPath);
|
||||
assert.equal(receipt.phase, "rolled-back");
|
||||
assert.equal(receipt.detail, "target did not become ready");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("records a recovery failure instead of claiming the update completed", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeFixture();
|
||||
const commands: Array<RecordedCommand> = [];
|
||||
const error = yield* activateSystemdSelfUpdatePlan(
|
||||
fixture.plan,
|
||||
(input) =>
|
||||
Effect.fail(
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: `${input.expectedVersion} did not become ready`,
|
||||
}),
|
||||
),
|
||||
{ restartDelay: Duration.zero },
|
||||
).pipe(Effect.provide(makeRunnerLayer(commands)), Effect.flip);
|
||||
|
||||
assert.include(error.reason, "previous server could not be restored");
|
||||
assert.equal((yield* readReceipt(fixture.receiptPath)).phase, "recovery-failed");
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,310 @@
|
||||
import { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Duration from "effect/Duration";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schedule from "effect/Schedule";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";
|
||||
|
||||
import { writeFileStringAtomically } from "../atomicWrite.ts";
|
||||
import * as ProcessRunner from "../processRunner.ts";
|
||||
import { readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
|
||||
import { BOOT_SERVICE_UNIT_FILE } from "./bootService.ts";
|
||||
|
||||
export const SYSTEMD_SELF_UPDATE_UNIT = "t3code-self-update.service";
|
||||
export const SYSTEMD_SELF_UPDATE_DIRECTORY = "self-update";
|
||||
export const SYSTEMD_SELF_UPDATE_PLAN_FILE = "plan.json";
|
||||
export const SYSTEMD_SELF_UPDATE_RECEIPT_FILE = "receipt.json";
|
||||
|
||||
const DEFAULT_RESTART_DELAY = Duration.seconds(2);
|
||||
const DEFAULT_READINESS_TIMEOUT = Duration.seconds(60);
|
||||
const DEFAULT_READINESS_INTERVAL = Duration.millis(500);
|
||||
const READINESS_REQUEST_TIMEOUT = Duration.seconds(2);
|
||||
const WELL_KNOWN_ENVIRONMENT_PATH = "/.well-known/t3/environment";
|
||||
|
||||
export class SystemdSelfUpdatePlan extends Schema.Class<SystemdSelfUpdatePlan>(
|
||||
"SystemdSelfUpdatePlan",
|
||||
)({
|
||||
version: Schema.Literal(1),
|
||||
fromVersion: Schema.String,
|
||||
targetVersion: Schema.String,
|
||||
currentPid: Schema.Int,
|
||||
unitPath: Schema.String,
|
||||
previousUnit: Schema.String,
|
||||
nextUnit: Schema.String,
|
||||
runtimeStatePath: Schema.String,
|
||||
receiptPath: Schema.String,
|
||||
}) {}
|
||||
|
||||
export const SystemdSelfUpdateReceipt = Schema.Struct({
|
||||
version: Schema.Literal(1),
|
||||
targetVersion: Schema.String,
|
||||
phase: Schema.Literals(["prepared", "activating", "healthy", "rolled-back", "recovery-failed"]),
|
||||
updatedAt: Schema.String,
|
||||
detail: Schema.optional(Schema.String),
|
||||
});
|
||||
export type SystemdSelfUpdateReceipt = typeof SystemdSelfUpdateReceipt.Type;
|
||||
|
||||
export class SystemdSelfUpdateActivationError extends Schema.TaggedErrorClass<SystemdSelfUpdateActivationError>()(
|
||||
"SystemdSelfUpdateActivationError",
|
||||
{
|
||||
reason: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return this.reason;
|
||||
}
|
||||
}
|
||||
|
||||
const decodePlan = Schema.decodeUnknownEffect(Schema.fromJsonString(SystemdSelfUpdatePlan));
|
||||
const encodePlan = Schema.encodeEffect(Schema.fromJsonString(SystemdSelfUpdatePlan));
|
||||
const encodeReceipt = Schema.encodeEffect(Schema.fromJsonString(SystemdSelfUpdateReceipt));
|
||||
|
||||
const provideFileServices = <A, E>(
|
||||
effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>,
|
||||
fs: FileSystem.FileSystem,
|
||||
path: Path.Path,
|
||||
) =>
|
||||
effect.pipe(
|
||||
Effect.provideService(FileSystem.FileSystem, fs),
|
||||
Effect.provideService(Path.Path, path),
|
||||
);
|
||||
|
||||
const writeAtomically = (
|
||||
filePath: string,
|
||||
contents: string,
|
||||
fs: FileSystem.FileSystem,
|
||||
path: Path.Path,
|
||||
) => provideFileServices(writeFileStringAtomically({ filePath, contents }), fs, path);
|
||||
|
||||
export const writeSystemdSelfUpdatePlan = Effect.fn("systemdSelfUpdate.writePlan")(function* (
|
||||
planPath: string,
|
||||
plan: SystemdSelfUpdatePlan,
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const encoded = yield* encodePlan(plan);
|
||||
yield* writeAtomically(planPath, `${encoded}\n`, fs, path);
|
||||
yield* writeReceipt(plan, "prepared");
|
||||
});
|
||||
|
||||
const writeReceipt = Effect.fn("systemdSelfUpdate.writeReceipt")(function* (
|
||||
plan: SystemdSelfUpdatePlan,
|
||||
phase: SystemdSelfUpdateReceipt["phase"],
|
||||
detail?: string,
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const now = yield* DateTime.now;
|
||||
const receipt = {
|
||||
version: 1,
|
||||
targetVersion: plan.targetVersion,
|
||||
phase,
|
||||
updatedAt: DateTime.formatIso(now),
|
||||
...(detail === undefined ? {} : { detail }),
|
||||
} satisfies SystemdSelfUpdateReceipt;
|
||||
const encoded = yield* encodeReceipt(receipt);
|
||||
yield* writeAtomically(plan.receiptPath, `${encoded}\n`, fs, path);
|
||||
});
|
||||
|
||||
const runSystemctl = Effect.fn("systemdSelfUpdate.runSystemctl")(function* (
|
||||
args: ReadonlyArray<string>,
|
||||
failureDescription: string,
|
||||
) {
|
||||
const runner = yield* ProcessRunner.ProcessRunner;
|
||||
const result = yield* runner.run({ command: "systemctl", args }).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: failureDescription,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (result.code !== 0) {
|
||||
return yield* new SystemdSelfUpdateActivationError({
|
||||
reason: `${failureDescription} (exit code ${String(result.code)}).`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const installUnit = Effect.fn("systemdSelfUpdate.installUnit")(function* (
|
||||
plan: SystemdSelfUpdatePlan,
|
||||
contents: string,
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
yield* writeAtomically(plan.unitPath, contents, fs, path).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: "Could not write the systemd boot service unit.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
yield* runSystemctl(["--user", "daemon-reload"], "Could not reload systemd units");
|
||||
});
|
||||
|
||||
const restartBootService = runSystemctl(
|
||||
["--user", "restart", BOOT_SERVICE_UNIT_FILE],
|
||||
"Could not restart the systemd boot service",
|
||||
);
|
||||
|
||||
export interface SystemdSelfUpdateReadinessInput {
|
||||
readonly expectedVersion: string;
|
||||
readonly previousPid: number;
|
||||
readonly runtimeStatePath: string;
|
||||
}
|
||||
|
||||
export const waitForSystemdSelfUpdateReadiness = Effect.fn("systemdSelfUpdate.waitForReadiness")(
|
||||
function* (
|
||||
input: SystemdSelfUpdateReadinessInput,
|
||||
options: {
|
||||
readonly timeout?: Duration.Input;
|
||||
readonly interval?: Duration.Input;
|
||||
} = {},
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const client = yield* HttpClient.HttpClient;
|
||||
const attempt = Effect.gen(function* () {
|
||||
const state = yield* provideFileServices(
|
||||
readPersistedServerRuntimeState(input.runtimeStatePath),
|
||||
fs,
|
||||
path,
|
||||
).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: "Could not read the restarted server's runtime state.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (Option.isNone(state) || state.value.pid === input.previousPid) {
|
||||
return yield* new SystemdSelfUpdateActivationError({
|
||||
reason: "The restarted server has not published fresh runtime state yet.",
|
||||
});
|
||||
}
|
||||
|
||||
const request = HttpClientRequest.get(
|
||||
new URL(WELL_KNOWN_ENVIRONMENT_PATH, state.value.origin).toString(),
|
||||
);
|
||||
const descriptor = yield* client.execute(request).pipe(
|
||||
Effect.timeout(READINESS_REQUEST_TIMEOUT),
|
||||
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
||||
Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: "The restarted server is not ready yet.",
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (descriptor.serverVersion !== input.expectedVersion) {
|
||||
return yield* new SystemdSelfUpdateActivationError({
|
||||
reason: `The restarted server reported t3@${descriptor.serverVersion}, expected t3@${input.expectedVersion}.`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const ready = yield* attempt.pipe(
|
||||
Effect.retry(Schedule.spaced(options.interval ?? DEFAULT_READINESS_INTERVAL)),
|
||||
Effect.timeoutOption(options.timeout ?? DEFAULT_READINESS_TIMEOUT),
|
||||
);
|
||||
if (Option.isNone(ready)) {
|
||||
return yield* new SystemdSelfUpdateActivationError({
|
||||
reason: `The restarted server did not become ready as t3@${input.expectedVersion}.`,
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export const activateSystemdSelfUpdatePlan = Effect.fn("systemdSelfUpdate.activatePlan")(function* <
|
||||
R,
|
||||
>(
|
||||
plan: SystemdSelfUpdatePlan,
|
||||
waitForReadiness: (
|
||||
input: SystemdSelfUpdateReadinessInput,
|
||||
) => Effect.Effect<void, SystemdSelfUpdateActivationError, R>,
|
||||
options: { readonly restartDelay?: Duration.Input } = {},
|
||||
) {
|
||||
const activate = Effect.gen(function* () {
|
||||
yield* writeReceipt(plan, "activating").pipe(Effect.ignore);
|
||||
yield* Effect.sleep(options.restartDelay ?? DEFAULT_RESTART_DELAY);
|
||||
yield* installUnit(plan, plan.nextUnit);
|
||||
yield* restartBootService;
|
||||
yield* waitForReadiness({
|
||||
expectedVersion: plan.targetVersion,
|
||||
previousPid: plan.currentPid,
|
||||
runtimeStatePath: plan.runtimeStatePath,
|
||||
});
|
||||
yield* writeReceipt(plan, "healthy").pipe(Effect.ignore);
|
||||
});
|
||||
|
||||
yield* activate.pipe(
|
||||
Effect.catch((activationError) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.logError("Systemd self-update failed; restoring the previous version.").pipe(
|
||||
Effect.annotateLogs({
|
||||
targetVersion: plan.targetVersion,
|
||||
error: activationError.reason,
|
||||
}),
|
||||
);
|
||||
const recovery = Effect.gen(function* () {
|
||||
yield* installUnit(plan, plan.previousUnit);
|
||||
yield* runSystemctl(
|
||||
["--user", "reset-failed", BOOT_SERVICE_UNIT_FILE],
|
||||
"Could not reset the failed systemd boot service",
|
||||
);
|
||||
yield* restartBootService;
|
||||
yield* waitForReadiness({
|
||||
expectedVersion: plan.fromVersion,
|
||||
previousPid: plan.currentPid,
|
||||
runtimeStatePath: plan.runtimeStatePath,
|
||||
});
|
||||
yield* writeReceipt(plan, "rolled-back", activationError.reason).pipe(Effect.ignore);
|
||||
});
|
||||
|
||||
yield* recovery.pipe(
|
||||
Effect.catch((recoveryError) =>
|
||||
writeReceipt(plan, "recovery-failed", recoveryError.reason).pipe(
|
||||
Effect.ignore,
|
||||
Effect.andThen(
|
||||
Effect.fail(
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: "The update failed and the previous server could not be restored.",
|
||||
cause: { activationError, recoveryError },
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
export const applySystemdSelfUpdatePlan = Effect.fn("systemdSelfUpdate.applyPlan")(function* (
|
||||
planPath: string,
|
||||
) {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const plan = yield* fs.readFileString(planPath).pipe(
|
||||
Effect.flatMap(decodePlan),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SystemdSelfUpdateActivationError({
|
||||
reason: `Could not read the staged update plan at ${planPath}.`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
yield* activateSystemdSelfUpdatePlan(plan, waitForSystemdSelfUpdateReadiness);
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
|
||||
import { MigrationIdentityMismatchError, validateMigrationIdentities } from "./Migrations.ts";
|
||||
import * as NodeSqliteClient from "./NodeSqliteClient.ts";
|
||||
|
||||
const layer = it.layer(Layer.mergeAll(NodeSqliteClient.layerMemory()));
|
||||
|
||||
layer("migration identity validation", (it) => {
|
||||
const createTrackingTable = Effect.fn("test.createMigrationTrackingTable")(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* sql`
|
||||
CREATE TABLE IF NOT EXISTS effect_sql_migrations (
|
||||
migration_id integer PRIMARY KEY NOT NULL,
|
||||
name varchar(255) NOT NULL,
|
||||
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`;
|
||||
yield* sql`DELETE FROM effect_sql_migrations`;
|
||||
});
|
||||
|
||||
it.effect("rejects a reused migration ID before migrations run", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* createTrackingTable();
|
||||
yield* sql`
|
||||
INSERT INTO effect_sql_migrations (migration_id, name)
|
||||
VALUES (35, 'DifferentMigration')
|
||||
`;
|
||||
|
||||
const error = yield* validateMigrationIdentities().pipe(Effect.flip);
|
||||
assert.instanceOf(error, MigrationIdentityMismatchError);
|
||||
assert.equal(error.migrationId, 35);
|
||||
assert.equal(error.expectedName, "ProjectionThreadTitleRegeneration");
|
||||
assert.equal(error.actualName, "DifferentMigration");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("allows migration IDs this build does not know about", () =>
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* createTrackingTable();
|
||||
yield* sql`
|
||||
INSERT INTO effect_sql_migrations (migration_id, name)
|
||||
VALUES (999, 'FutureMigration')
|
||||
`;
|
||||
|
||||
yield* validateMigrationIdentities();
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -11,6 +11,8 @@
|
||||
import * as Migrator from "effect/unstable/sql/Migrator";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
|
||||
// Import all migrations statically
|
||||
import Migration0001 from "./Migrations/001_OrchestrationEvents.ts";
|
||||
@@ -112,6 +114,59 @@ export const makeMigrationLoader = (throughId?: number) =>
|
||||
*/
|
||||
const run = Migrator.make({});
|
||||
|
||||
export class MigrationIdentityMismatchError extends Schema.TaggedErrorClass<MigrationIdentityMismatchError>()(
|
||||
"MigrationIdentityMismatchError",
|
||||
{
|
||||
migrationId: Schema.Int,
|
||||
expectedName: Schema.String,
|
||||
actualName: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Migration ${String(this.migrationId)} is recorded as '${this.actualName}', but this t3 build expects '${this.expectedName}'.`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuse to run a build whose migration history disagrees with the database.
|
||||
* Unknown newer IDs are intentionally allowed so a compatible older build can
|
||||
* inspect a database created by a newer release without rewriting its history.
|
||||
*/
|
||||
export const validateMigrationIdentities = Effect.fn("validateMigrationIdentities")(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const tables = yield* sql<{ readonly name: string }>`
|
||||
SELECT name
|
||||
FROM sqlite_master
|
||||
WHERE type = 'table' AND name = 'effect_sql_migrations'
|
||||
`;
|
||||
if (tables.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const expectedNames = new Map<number, string>(
|
||||
migrationEntries.map(([migrationId, name]) => [migrationId, name]),
|
||||
);
|
||||
const persisted = yield* sql<{
|
||||
readonly migration_id: number;
|
||||
readonly name: string;
|
||||
}>`
|
||||
SELECT migration_id, name
|
||||
FROM effect_sql_migrations
|
||||
ORDER BY migration_id
|
||||
`;
|
||||
|
||||
for (const migration of persisted) {
|
||||
const expectedName = expectedNames.get(migration.migration_id);
|
||||
if (expectedName !== undefined && expectedName !== migration.name) {
|
||||
return yield* new MigrationIdentityMismatchError({
|
||||
migrationId: migration.migration_id,
|
||||
expectedName,
|
||||
actualName: migration.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export interface RunMigrationsOptions {
|
||||
readonly toMigrationInclusive?: number | undefined;
|
||||
}
|
||||
@@ -129,6 +184,7 @@ export interface RunMigrationsOptions {
|
||||
export const runMigrations = Effect.fn("runMigrations")(function* ({
|
||||
toMigrationInclusive,
|
||||
}: RunMigrationsOptions = {}) {
|
||||
yield* validateMigrationIdentities();
|
||||
const executedMigrations = yield* run({ loader: makeMigrationLoader(toMigrationInclusive) });
|
||||
const migrations = executedMigrations.map(([id, name]) => `${id}_${name}`);
|
||||
yield* migrations.length === 0
|
||||
|
||||
@@ -66,10 +66,15 @@ flowchart TD
|
||||
H --> I[Run version preflight]
|
||||
I -->|bad code or version| J[Remove candidate runtime and keep current server]
|
||||
I -->|cannot run preflight| J2[Keep candidate and current server]
|
||||
I -->|passes| K{Handoff method}
|
||||
K -->|boot-service| L[Rewrite and restart T3 systemd unit]
|
||||
I -->|passes| I2[Run target database compatibility check]
|
||||
I2 -->|fails| J2
|
||||
I2 -->|passes| K{Handoff method}
|
||||
K -->|boot-service| L[Stage plan and start transient systemd helper]
|
||||
K -->|respawn| M[Start delayed replacement and exit current process]
|
||||
L --> N[Reconnect with fresh backoff]
|
||||
L --> L2[Helper activates unit and verifies target]
|
||||
L2 -->|target fails| L3[Restore and verify previous unit]
|
||||
L2 -->|target ready| N[Reconnect with fresh backoff]
|
||||
L3 --> N
|
||||
M --> N
|
||||
N --> O[Replacement publishes ready at target version]
|
||||
```
|
||||
@@ -84,8 +89,10 @@ The update service permits one update at a time. It installs `t3@<version>` unde
|
||||
successfully. Boot-service setup and self-update share the same process-wide installation lock, so
|
||||
they cannot mutate a pinned runtime concurrently.
|
||||
|
||||
Before any restart, the current Node executable runs the replacement with `--version`. A failed
|
||||
install, failed preflight, or wrong reported version leaves the current server running.
|
||||
Before any restart, the current Node executable runs the replacement with `--version`, then asks
|
||||
the target artifact to validate its migration identities against the live database in read-only
|
||||
mode. A failed install, version preflight, or compatibility check leaves the current server running.
|
||||
Targets old enough to lack the compatibility command are not eligible for remote activation.
|
||||
|
||||
Candidate cleanup is narrower than "any failed preflight". The candidate runtime is removed only when
|
||||
the preflight process actually completes and reports a bad exit code or the wrong version: that is
|
||||
@@ -107,10 +114,14 @@ authorization; it does not uninstall the host service.
|
||||
|
||||
## Process Handoff
|
||||
|
||||
For `boot-service`, the server atomically rewrites the T3-managed user unit to point at the verified
|
||||
runtime and reloads systemd. It acknowledges the handoff, then restarts the unit after the same
|
||||
short grace period used by foreground respawn. A rejected deferred restart restores the previous
|
||||
unit and is logged by the still-running process.
|
||||
For `boot-service`, the request process writes a typed plan but leaves its live unit untouched. It
|
||||
starts a fixed-name `t3code-self-update.service` transient unit with `systemd-run --user`; the
|
||||
transient unit has a separate cgroup and acts as the cross-process update lock. After a short grace
|
||||
period it atomically installs the candidate unit, reloads systemd, performs a blocking restart, and
|
||||
probes the public environment descriptor for the exact target version. If activation or readiness
|
||||
fails, the helper restores the previous unit, clears systemd's failed state, restarts it, and proves
|
||||
the previous version is ready. The plan and latest receipt live under the environment state
|
||||
directory's `self-update` folder for diagnosis.
|
||||
|
||||
For `respawn`, the server starts a detached, delayed replacement that replays the original CLI
|
||||
arguments. It then acknowledges the request and schedules the current process to exit. The delays
|
||||
@@ -118,10 +129,10 @@ give the acknowledgement time to cross direct or relayed connections before the
|
||||
|
||||
Progress-capable servers emit `downloading` before installing the pinned runtime and `installing`
|
||||
before preflight and handoff. A terminal stream event acknowledges that restart is scheduled. The
|
||||
client then enters `resuming`, waits for the replacement lifecycle stream to publish `ready` with
|
||||
the target version, and only then completes the operation. It watches for the intentional
|
||||
disconnect's first backoff state and requests one fresh retry, which clears historical backoff debt
|
||||
without adding a separate reconnect loop.
|
||||
client then enters `resuming`, waits for the intentional disconnect before accepting a new lifecycle
|
||||
event, and requests one fresh retry to clear historical backoff debt. The first new `ready` event
|
||||
must report the target version. A verified rollback therefore becomes an immediate wrong-version
|
||||
error instead of looking like a two-minute timeout.
|
||||
|
||||
## Release Invariant
|
||||
|
||||
@@ -134,7 +145,9 @@ the hosted web deployment depends on that release. See [Release Checklist](../op
|
||||
- Capability contract: `packages/contracts/src/environment.ts`
|
||||
- Update RPC contract: `packages/contracts/src/server.ts` and `packages/contracts/src/rpc.ts`
|
||||
- Capability detection and handoff: `apps/server/src/cloud/selfUpdate.ts`
|
||||
- Systemd activation, health check, and rollback: `apps/server/src/cloud/systemdSelfUpdate.ts`
|
||||
- Host service commands: `apps/server/src/cli/service.ts`
|
||||
- Pinned runtime installation: `apps/server/src/cloud/pinnedRuntime.ts`
|
||||
- Migration identity validation: `apps/server/src/persistence/Migrations.ts`
|
||||
- Client version comparison: `apps/web/src/versionSkew.ts`
|
||||
- Shared update action: `apps/web/src/components/ServerUpdateAction.tsx`
|
||||
@@ -59,6 +59,10 @@ Keep the web or desktop app open while the server restarts. The update completes
|
||||
replacement server reports the requested version and is ready to accept commands. The warning and
|
||||
progress rail then disappear.
|
||||
|
||||
When a background-service replacement cannot become ready, T3 Code restores and verifies the
|
||||
previous server version automatically. The update remains failed so you can retry after reading the
|
||||
reported reason, but the remote server should remain reachable.
|
||||
|
||||
If a step fails:
|
||||
|
||||
1. Retry the offered action once.
|
||||
|
||||
@@ -34,6 +34,8 @@ import {
|
||||
resolveServerUpdateProgressResult,
|
||||
serverUpdateStateForProgressEvent,
|
||||
serverUpdateStateForServerVersion,
|
||||
ServerUpdateWrongVersionError,
|
||||
validateResumedServerVersion,
|
||||
} from "./server.ts";
|
||||
|
||||
const CONFIG = {
|
||||
@@ -152,6 +154,27 @@ describe("server state projection", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.effect("reports the rollback version as soon as the server resumes", () =>
|
||||
Effect.gen(function* () {
|
||||
const error = yield* validateResumedServerVersion({
|
||||
environmentId: TARGET.environmentId,
|
||||
targetVersion: "0.0.31",
|
||||
actualVersion: "0.0.30",
|
||||
}).pipe(Effect.flip);
|
||||
|
||||
expect(error).toBeInstanceOf(ServerUpdateWrongVersionError);
|
||||
expect(error.message).toBe("The server resumed on [email protected] instead of [email protected].");
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("accepts the requested version after reconnect", () =>
|
||||
validateResumedServerVersion({
|
||||
environmentId: TARGET.environmentId,
|
||||
targetVersion: "0.0.31",
|
||||
actualVersion: "0.0.31",
|
||||
}),
|
||||
);
|
||||
|
||||
it("keeps active update state and hides stale failures after a version change", () => {
|
||||
const running = {
|
||||
status: "running" as const,
|
||||
|
||||
@@ -86,6 +86,43 @@ export class ServerUpdateResumeTimeoutError extends Schema.TaggedErrorClass<Serv
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerUpdateRestartTimeoutError extends Schema.TaggedErrorClass<ServerUpdateRestartTimeoutError>()(
|
||||
"ServerUpdateRestartTimeoutError",
|
||||
{
|
||||
environmentId: Schema.String,
|
||||
targetVersion: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `The server accepted t3@${this.targetVersion} but did not restart.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class ServerUpdateWrongVersionError extends Schema.TaggedErrorClass<ServerUpdateWrongVersionError>()(
|
||||
"ServerUpdateWrongVersionError",
|
||||
{
|
||||
environmentId: Schema.String,
|
||||
targetVersion: Schema.String,
|
||||
actualVersion: Schema.String,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `The server resumed on t3@${this.actualVersion} instead of t3@${this.targetVersion}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export const validateResumedServerVersion = Effect.fn("server.validateResumedUpdateVersion")(
|
||||
function* (input: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly targetVersion: string;
|
||||
readonly actualVersion: string;
|
||||
}) {
|
||||
if (input.actualVersion !== input.targetVersion) {
|
||||
return yield* new ServerUpdateWrongVersionError(input);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export class ServerUpdateProgressIncompleteError extends Schema.TaggedErrorClass<ServerUpdateProgressIncompleteError>()(
|
||||
"ServerUpdateProgressIncompleteError",
|
||||
{
|
||||
@@ -446,6 +483,7 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
atomRegistry.get(configValueAtom(target.environmentId))?.environment.serverVersion ??
|
||||
targetVersion;
|
||||
let currentStage: ServerUpdateStage = "downloading";
|
||||
let transportAlreadyDisconnected = false;
|
||||
atomRegistry.set(stateAtom, {
|
||||
status: "running",
|
||||
stage: currentStage,
|
||||
@@ -500,9 +538,14 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
),
|
||||
Effect.exit,
|
||||
);
|
||||
const terminalResult = yield* Ref.get(terminal);
|
||||
transportAlreadyDisconnected =
|
||||
Option.isSome(terminalResult) &&
|
||||
Exit.isFailure(streamExit) &&
|
||||
isLegacyUpdateHandoffLoss(streamExit.cause);
|
||||
return yield* resolveServerUpdateProgressResult(
|
||||
targetVersion,
|
||||
yield* Ref.get(terminal),
|
||||
terminalResult,
|
||||
streamExit,
|
||||
);
|
||||
})
|
||||
@@ -521,6 +564,7 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
// Older servers can tear down the transport before their
|
||||
// unary acknowledgement arrives. Treat only that transport
|
||||
// loss as a handoff, then prove it by waiting for target ready.
|
||||
transportAlreadyDisconnected = true;
|
||||
return { targetVersion, method: selfUpdateMethod };
|
||||
}
|
||||
return yield* Effect.failCause(exit.cause);
|
||||
@@ -537,26 +581,40 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
}),
|
||||
);
|
||||
|
||||
// The update restart is intentional. As soon as the supervisor sees
|
||||
// that first failed connection, discard any prior backoff debt and
|
||||
// retry immediately instead of carrying an old 16-second delay.
|
||||
yield* environmentRegistry.stateChanges(target.environmentId).pipe(
|
||||
Stream.filter((state) => state.phase === "backoff"),
|
||||
Stream.take(1),
|
||||
Stream.runDrain,
|
||||
Effect.andThen(environmentRegistry.retryNow(target.environmentId)),
|
||||
Effect.timeoutOption(Duration.seconds(30)),
|
||||
Effect.ignore,
|
||||
Effect.forkChild,
|
||||
);
|
||||
// Subscribe before following lifecycle again so a cached ready event
|
||||
// from the still-running old process cannot satisfy the update. The
|
||||
// helper waits before restarting, giving this listener time to attach.
|
||||
if (!transportAlreadyDisconnected) {
|
||||
const disconnected = yield* environmentRegistry.stateChanges(target.environmentId).pipe(
|
||||
Stream.filter((state) => state.phase === "backoff"),
|
||||
Stream.take(1),
|
||||
Stream.runHead,
|
||||
Effect.timeoutOption(Duration.seconds(30)),
|
||||
Effect.map(Option.flatten),
|
||||
);
|
||||
if (Option.isNone(disconnected)) {
|
||||
return yield* new ServerUpdateRestartTimeoutError({
|
||||
environmentId: target.environmentId,
|
||||
targetVersion,
|
||||
});
|
||||
}
|
||||
// The update restart is intentional. Discard any prior backoff debt
|
||||
// instead of carrying an old 16-second delay into recovery.
|
||||
yield* environmentRegistry.retryNow(target.environmentId);
|
||||
} else {
|
||||
// Legacy servers may close the RPC before this listener can attach.
|
||||
// If the supervisor is still waiting, clear that delay; if it has
|
||||
// already reconnected, do not interrupt the healthy replacement.
|
||||
const state = yield* environmentRegistry.state(target.environmentId);
|
||||
if (state.phase === "backoff") {
|
||||
yield* environmentRegistry.retryNow(target.environmentId);
|
||||
}
|
||||
}
|
||||
|
||||
const resumed = yield* environmentRegistry
|
||||
.followStream(target.environmentId, subscribe(WS_METHODS.subscribeServerLifecycle, {}))
|
||||
.pipe(
|
||||
Stream.filter(
|
||||
(event) =>
|
||||
event.type === "ready" && event.payload.environment.serverVersion === targetVersion,
|
||||
),
|
||||
Stream.filter((event) => event.type === "ready"),
|
||||
Stream.runHead,
|
||||
Effect.timeoutOption(Duration.seconds(120)),
|
||||
Effect.map(Option.flatten),
|
||||
@@ -567,6 +625,11 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
targetVersion,
|
||||
});
|
||||
}
|
||||
yield* validateResumedServerVersion({
|
||||
environmentId: target.environmentId,
|
||||
targetVersion,
|
||||
actualVersion: resumed.value.payload.environment.serverVersion,
|
||||
});
|
||||
|
||||
atomRegistry.set(stateAtom, IDLE_SERVER_UPDATE_STATE);
|
||||
return result;
|
||||
|
||||
Reference in new issue
Block a user