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

outdatedServerUpdate.ts

87 lines3.3 KB

1import type { EnvironmentId, ServerSelfUpdateInput } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Exit from "effect/Exit";
5import type * as HttpClient from "effect/http/HttpClient";
6import type { Atom } from "effect/reactivity";
7import type * as Socket from "effect/socket/Socket";
8​
9import { describeOutdatedHost, updateOutdatedHost } from "../connection/outdatedHostUpdate.ts";
10import type * as ConnectionResolver from "../connection/resolver.ts";
11import type * as EnvironmentRegistry from "../connection/registry.ts";
12import type * as RelayEnvironmentDiscovery from "../relay/discovery.ts";
13import { createAtomCommandScheduler, createRuntimeCommand } from "./runtime.ts";
14import {
15 serverUpdateFailureMessage,
16 serverUpdateStateAtom,
17 type ServerUpdateStage,
18} from "./server.ts";
19​
20export interface OutdatedServerUpdateTarget {
21 readonly environmentId: EnvironmentId;
22 readonly input: ServerSelfUpdateInput;
23 /** From the host descriptor when known; an outdated host never delivers a server config. */
24 readonly fromVersion?: string;
26​
27export function createOutdatedServerDescriptionCommand<E>(
28 runtime: Atom.AtomRuntime<
29 EnvironmentRegistry.EnvironmentRegistry | ConnectionResolver.ConnectionResolver,
30 E
31 >,
32) {
33 return createRuntimeCommand(runtime, {
34 label: "environment-data:server:describe-outdated-server",
35 execute: (environmentId: EnvironmentId) =>
36 describeOutdatedHost(environmentId).pipe(Effect.timeout("20 seconds")),
37 });
39​
40export function createOutdatedServerUpdateCommand<E>(
41 runtime: Atom.AtomRuntime<
42 | EnvironmentRegistry.EnvironmentRegistry
43 | ConnectionResolver.ConnectionResolver
44 | RelayEnvironmentDiscovery.RelayEnvironmentDiscovery
45 | Socket.WebSocketConstructor
46 | HttpClient.HttpClient,
47 E
48 >,
49) {
50 return createRuntimeCommand(runtime, {
51 label: "environment-data:server:update-outdated-server",
52 scheduler: createAtomCommandScheduler(),
53 concurrency: {
54 mode: "singleFlight",
55 key: ({ environmentId }: OutdatedServerUpdateTarget) => environmentId,
56 },
57 execute: (target: OutdatedServerUpdateTarget, atomRegistry) => {
58 const stateAtom = serverUpdateStateAtom(target.environmentId);
59 const targetVersion = target.input.targetVersion;
60 const fromVersion = target.fromVersion ?? targetVersion;
61 let currentStage: ServerUpdateStage = "downloading";
62 const setStage = (stage: ServerUpdateStage) =>
63 Effect.sync(() => {
64 currentStage = stage;
65 atomRegistry.set(stateAtom, { status: "running", stage, fromVersion, targetVersion });
66 });
67 return setStage(currentStage).pipe(
68 Effect.andThen(updateOutdatedHost(target.environmentId, target.input, setStage)),
69 Effect.onExit((exit) =>
70 Effect.sync(() => {
71 if (Exit.isSuccess(exit) || Cause.hasInterruptsOnly(exit.cause)) {
72 atomRegistry.set(stateAtom, { status: "idle" });
73 return;
74 }
75 atomRegistry.set(stateAtom, {
76 status: "failed",
77 stage: currentStage,
78 fromVersion,
79 targetVersion,
80 message: serverUpdateFailureMessage(Cause.squash(exit.cause)),
81 });
82 }),
83 ),
84 );
85 },
86 });