feat(cli): add t3 service restart and make t3 update repoint the service eagerly #11702
Mergedjuliusmarmingemerged 4 commits into
main fromrefs/pull/11702/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/cli/service.ts line 179OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -159,3 +164,16 @@159164 ),160165 );161166167+// Run by `t3 update` through the newly installed executable, so the version168+// written into the unit is one that has proven it runs. `--no-start` writes169+// the files and leaves the current service running for a later restart.170+const serviceReconcileCommand = Command.make("__reconcile", {171+ ...serviceReconcileFlags,172+ noStart: Flag.boolean("no-start").pipe(Flag.withDefault(false)),173+}).pipe(174+ Command.withDescription("Point the service unit at this t3, optionally without restarting."),175+ Command.unlisted,176+ Command.withHandler((flags) =>177+ runServiceCommand(178+ flags,179+ reconcileService({ allowDowngrade: flags.allowDowngrade, start: !flags.noStart }).pipe(apps/server/src/cloud/bootService.ts line 543Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -523,4 +523,12 @@523523 {524524 readonly install: (options?: {525525 readonly allowDowngrade?: boolean;526+ /**527+ * Write the unit for this version but leave the service on whatever it528+ * is running now. `t3 update` uses this when the user declines the529+ * restart, so a later `t3 service restart` lands on the new version.530+ */531+ readonly start?: boolean;526532 }) => Effect.Effect<BootServicePlan, BootServiceError>;533+ /** Stop and start the installed service on the version its unit names. */534+ readonly restart: Effect.Effect<boolean, BootServiceError>;apps/server/src/cloud/bootService.ts line 825Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -795,4 +804,8 @@795804 const installed = yield* fs796805 .exists(unitPath)797806 .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));798- if (installed) {807+ // Leaving the service running while its files change is only safe when808+ // the running launcher is not asked to act on them: it reads the state809+ // file once at startup, and the unit only matters on the next start.810+ const start = options?.start !== false;811+ if (installed && start) {apps/server/src/cloud/bootService.ts line 902Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -838,12 +851,13 @@838851 );839852 yield* writeDurably(unitPath, manager.render(plan));840853841- yield* runSteps(manager.activate);854+ if (start) yield* runSteps(manager.activate);842855 }).pipe(843856 Effect.tapError(() =>844- installed ? runSteps(manager.restart).pipe(Effect.ignore) : Effect.void,857+ installed && start ? runSteps(manager.restart).pipe(Effect.ignore) : Effect.void,845858 ),846859 );847860 return plan;848861 });849862863+ const restart: BootService["Service"]["restart"] = Effect.gen(function* () {apps/server/src/cli/update.ts line 532OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -519,12 +519,14 @@519519 targetEntryPath: runtime.entryPath,520520 });521521522- // The new executable owns the service switch: it verifies itself, writes its523- // own version into the unit, and restarts the service on it.522+ // The new executable owns the service switch: it verifies itself and writes523+ // its own version into the unit. The unit is rewritten either way so a524+ // later `t3 service restart` lands on the new version; only the restart525+ // itself waits for the user's answer.524526 let serviceUpdated = false;525- if (restartService) {527+ if (serviceInstalled && !serviceCurrent) {526528 const result = yield* runner.run({527529 command: runtime.entryPath,528530 args: [529531 "service",530- "update",532+ "__reconcile",
- apps/server/src/cli/update.ts line 541Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -519,7 +519,9 @@519519 targetEntryPath: runtime.entryPath,520520 });521521522- // The new executable owns the service switch: it verifies itself, writes its523- // own version into the unit, and restarts the service on it.522+ // The new executable owns the service switch: it verifies itself and writes523+ // its own version into the unit. The unit is rewritten either way so a524+ // later `t3 service restart` lands on the new version; only the restart525+ // itself waits for the user's answer.524526 let serviceUpdated = false;525- if (restartService) {527+ if (serviceInstalled && !serviceCurrent) {
- apps/server/src/cloud/bootService.ts line 30Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -27,3 +27,4 @@2727 } from "./pinnedRuntime.ts";2828 import {2929 SERVICE_LAUNCHER_PROTOCOL,30+ SERVICE_RESTART_PENDING_FILE,
- apps/server/src/cloud/bootService.ts lines 817 to 823Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -795,4 +814,10 @@795814 const installed = yield* fs796815 .exists(unitPath)797816 .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));798- if (installed) {817+ // With start=false the service keeps running while its files change. The818+ // launcher reads the state file once at startup and the unit only matters819+ // on the next start, so that is safe as long as the launcher is not in820+ // the middle of a remote update, which is the one time it writes the821+ // state file itself. That case is refused below, before anything is822+ // written, from the same read the downgrade check uses; the stop that823+ // normally serialises against the launcher is skipped on purpose.apps/server/src/cloud/bootService.ts line 874OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -838,4 +865,10 @@838865 );839866 yield* writeDurably(unitPath, manager.render(plan));840867841- yield* runSteps(manager.activate);868+ if (start) {869+ yield* runSteps(manager.activate);870+ yield* fs.remove(restartPendingPath, { force: true });871+ } else if (installed) {872+ // The running service is now behind its unit until someone restarts873+ // it; the marker is what status and install use to remember that.874+ yield* fs.writeFileString(restartPendingPath, `${input.cliVersion}\n`, { mode: 0o600 });
- apps/server/src/serviceLauncher.ts line 375OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -365,5 +368,8 @@365368 async #recover(): Promise<void> {366369 // A fresh launcher means servers are running again: any stop marker from367370 // a previous explicit stop is stale and must not make a future update368- // handoff release its tunnel.371+ // handoff release its tunnel. A fresh launcher is also, by definition,372+ // the one the unit names, so a restart deferred by `t3 update` is done373+ // no matter who restarted the service.369374 await NodeFSP.rm(stopMarkerPath(this.#baseDir), { force: true }).catch(() => undefined);375+ await NodeFSP.rm(restartPendingPath(this.#baseDir), { force: true }).catch(() => undefined);
juliusmarminge merged thisd8655ed
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed