Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopAppUpdate.ts

231 lines8.5 KB

1import {
2 ServerSelfUpdateError,
3 type DesktopUpdateState,
4 type DesktopUpdateStatusReport,
5 type ServerSelfUpdateProgressStage,
6 type ServerSelfUpdateResult,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Crypto from "effect/Crypto";
10import * as Duration from "effect/Duration";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14import * as Ref from "effect/Ref";
15import * as Result from "effect/Result";
16import * as Stream from "effect/Stream";
17​
18import { ServerConfig } from "../config.ts";
19import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts";
20​
21/** Backstop for a desktop updater that hangs without ever reporting a
22 terminal outcome. Generous: it covers a slow download of a full build. */
23const DESKTOP_UPDATE_TIMEOUT = Duration.minutes(20);
24const DESKTOP_INSTALL_TIMEOUT = Duration.minutes(2);
25​
26/** Progress stage a desktop update state maps to, or null when the state
27 carries no progress worth streaming. */
28function desktopUpdateProgressStage(
29 state: DesktopUpdateState,
30): ServerSelfUpdateProgressStage | null {
31 switch (state.status) {
32 case "checking":
33 case "available":
34 case "downloading":
35 return "downloading";
36 case "downloaded":
37 return "installing";
38 default:
39 return null;
40 }
42​
43export class DesktopAppUpdate extends Context.Service<
44 DesktopAppUpdate,
45 {
46 /** True when this server was spawned by a desktop app that can be
47 driven over the telemetry control channel. */
48 readonly available: boolean;
49 /** Checks and downloads through the desktop app, then returns a token
50 while this server is still connected. `commit` starts installation. */
51 readonly run: (
52 reportProgress: (
53 stage: ServerSelfUpdateProgressStage,
54 ) => Effect.Effect<void, ServerSelfUpdateError>,
55 ) => Effect.Effect<ServerSelfUpdateResult, ServerSelfUpdateError>;
56 /** Starts the prepared install. Success stops this server, so this effect
57 returns only when installation fails or times out. */
58 readonly commit: (
59 requestId: string,
60 onHandoffAccepted?: () => Effect.Effect<void>,
61 ) => Effect.Effect<never, ServerSelfUpdateError>;
62 }
63>()("t3/desktopUpdate/DesktopAppUpdate") {}
64​
65export const make = Effect.fn("desktopUpdate.desktopAppUpdate.make")(function* () {
66 const config = yield* ServerConfig;
67 const crypto = yield* Crypto.Crypto;
68 const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver;
69 const inFlight = yield* Ref.make(false);
70​
71 const available = config.mode === "desktop" && config.desktopTelemetryControlFd !== undefined;
72 const failWith = (reason: string, cause?: unknown) =>
73 cause === undefined
74 ? new ServerSelfUpdateError({ reason })
75 : new ServerSelfUpdateError({ reason, cause });
76​
77 const consumeReports = (
78 requestId: string,
79 changes: Stream.Stream<DesktopUpdateStatusReport>,
80 reportProgress: (
81 stage: ServerSelfUpdateProgressStage,
82 ) => Effect.Effect<void, ServerSelfUpdateError>,
83 ) =>
84 Effect.gen(function* () {
85 const lastStage = yield* Ref.make<ServerSelfUpdateProgressStage | null>(null);
86 const emitStage = (
87 stage: ServerSelfUpdateProgressStage | null,
88 ): Effect.Effect<void, ServerSelfUpdateError> =>
89 stage === null
90 ? Effect.void
91 : Ref.get(lastStage).pipe(
92 Effect.flatMap((previous) =>
93 previous === stage
94 ? Effect.void
95 : Ref.set(lastStage, stage).pipe(Effect.andThen(reportProgress(stage))),
96 ),
97 );
98​
99 const terminal = yield* changes.pipe(
100 Stream.filter((report) => report.requestId === requestId),
101 Stream.mapEffect(
102 (
103 report,
104 ): Effect.Effect<Option.Option<DesktopUpdateStatusReport>, ServerSelfUpdateError> =>
105 report.outcome === undefined
106 ? emitStage(desktopUpdateProgressStage(report.state)).pipe(
107 Effect.as(Option.none<DesktopUpdateStatusReport>()),
108 )
109 : Effect.succeed(Option.some(report)),
110 ),
111 Stream.filterMap(
112 Option.match({
113 onNone: () => Result.failVoid,
114 onSome: Result.succeed,
115 }),
116 ),
117 Stream.runHead,
118 );
119 if (Option.isNone(terminal)) {
120 return yield* failWith("The desktop app stopped reporting its update.");
121 }
122​
123 const report = terminal.value;
124 if (report.outcome === "ready-to-install") {
125 yield* emitStage("installing");
126 const targetVersion =
127 report.state.downloadedVersion ??
128 report.state.availableVersion ??
129 report.state.currentVersion;
130 yield* Effect.logInfo("Desktop app update prepared for install.", {
131 targetVersion,
132 });
133 yield* Ref.set(inFlight, false);
134 return { targetVersion, method: "desktop-app" as const, desktopUpdateToken: requestId };
135 }
136 if (report.outcome === "up-to-date") {
137 return yield* failWith(
138 `The T3 Code desktop app on this machine is already up to date on ${report.state.currentVersion}.`,
139 );
140 }
141 return yield* failWith(
142 report.reason ?? report.state.message ?? "The desktop app update failed.",
143 );
144 });
145​
146 const run: DesktopAppUpdate["Service"]["run"] = Effect.fn("desktopUpdate.desktopAppUpdate.run")(
147 function* (reportProgress) {
148 if (!available) {
149 return yield* failWith(
150 "This server was not started by the T3 Code desktop app, so it cannot drive a desktop update.",
151 );
152 }
153 if (yield* Ref.getAndSet(inFlight, true)) {
154 return yield* failWith("A desktop app update is already in progress.");
155 }
156​
157 return yield* Effect.scoped(
158 Effect.gen(function* () {
159 const requestId = yield* crypto.randomUUIDv4.pipe(
160 Effect.mapError((error) =>
161 failWith("Could not generate a desktop update request id.", error),
162 ),
163 );
164 // Subscribe before sending the request so a fast first report
165 // cannot be missed.
166 const { changes } = yield* receiver.desktopUpdates;
167 yield* receiver
168 .requestDesktopUpdate(requestId)
169 .pipe(
170 Effect.mapError((error) =>
171 failWith("Could not reach the T3 Code desktop app on this machine.", error),
172 ),
173 );
174 return yield* consumeReports(requestId, changes, reportProgress).pipe(
175 Effect.onInterrupt(() => receiver.cancelDesktopUpdate(requestId).pipe(Effect.ignore)),
176 );
177 }),
178 ).pipe(
179 Effect.timeout(DESKTOP_UPDATE_TIMEOUT),
180 Effect.catchTags({
181 TimeoutError: () => failWith("The desktop app did not finish the update in time."),
182 }),
183 Effect.onError(() => Ref.set(inFlight, false)),
184 );
185 },
186 );
187​
188 const commit: DesktopAppUpdate["Service"]["commit"] = Effect.fn(
189 "desktopUpdate.desktopAppUpdate.commit",
190 )(function* (requestId, onHandoffAccepted = () => Effect.void) {
191 if (!available) {
192 return yield* failWith("This server cannot commit a desktop app update.");
193 }
194 const terminal = yield* Effect.scoped(
195 Effect.gen(function* () {
196 const { latest, changes } = yield* receiver.desktopUpdates;
197 const reports = Option.match(latest, {
198 onNone: () => changes,
199 onSome: (report) => Stream.concat(Stream.make(report), changes),
200 });
201 yield* Effect.uninterruptible(
202 receiver.commitDesktopUpdate(requestId).pipe(
203 Effect.mapError((error) => failWith("Could not reach the T3 Code desktop app.", error)),
204 Effect.tap(() => onHandoffAccepted()),
205 ),
206 );
207 return yield* reports.pipe(
208 Stream.filter((report) => report.requestId === requestId && report.outcome === "failed"),
209 Stream.runHead,
210 Effect.timeout(DESKTOP_INSTALL_TIMEOUT),
211 Effect.catchTags({
212 TimeoutError: () =>
213 failWith("The desktop app did not report an install result in time."),
214 }),
215 );
216 }),
217 );
218 if (Option.isNone(terminal)) {
219 return yield* failWith("The desktop app stopped reporting the install.");
220 }
221 return yield* failWith(
222 terminal.value.reason ??
223 terminal.value.state.message ??
224 "The desktop app failed to install the update.",
225 );
226 });
227​
228 return DesktopAppUpdate.of({ available, run, commit });
229});
230​
231export const layer = Layer.effect(DesktopAppUpdate, make());