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

outdatedServerUpdate.ts

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