@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 275 lines 11.3 KB
Raw
1 import * as Fly from "@alchemy.run/fly";
2 import * as Redis from "@alchemy.run/redis";
3 import { HttpServer, NodeHttpServer } from "alchemy/Http";
4 import { ServerHost } from "alchemy/Runtime/Process";
5 import * as Config from "effect/Config";
6 import * as ConfigProvider from "effect/ConfigProvider";
7 import * as Deferred from "effect/Deferred";
8 import * as Effect from "effect/Effect";
9 import * as Fiber from "effect/Fiber";
10 import { HttpServerRequest } from "effect/http/HttpServerRequest";
11 import * as HttpServerResponse from "effect/http/HttpServerResponse";
12 import * as Option from "effect/Option";
13 import * as Redacted from "effect/Redacted";
14 import * as Scope from "effect/Scope";
15 import * as Stream from "effect/Stream";
16 import { Cache, WorkerSite, scripts, services, type Job } from "./bluegreen-worker-shared.ts";
17
18 export class Worker extends Fly.Service<Worker>()("Worker") {}
19 export interface WorkerOptions {
20 version: string;
21 timeout: "10 seconds" | "30 seconds" | "60 seconds";
22 signal: "SIGTERM" | "SIGINT";
23 runOnly?: boolean;
24 mode?: string;
25 afterSignalMs?: number;
26 workers?: number;
27 }
28 export const workerLayer = (options: WorkerOptions) =>
29 Worker.make(
30 {
31 app: WorkerSite,
32 main: import.meta.url,
33 region: "iad",
34 port: 3000,
35 guest: { cpuKind: "shared", cpus: 1, memoryMb: 256 },
36 env: {
37 VERSION: options.version,
38 MODE: options.mode ?? "drain",
39 AFTER_SIGNAL_MS: String(options.afterSignalMs ?? 1000),
40 WORKERS: String(options.workers ?? 1),
41 RUN_ONLY: options.runOnly ? "1" : "0",
42 },
43 deploy: { strategy: "bluegreen", healthTimeout: "45 seconds" },
44 shutdown: { signal: options.signal, timeout: options.timeout },
45 services: options.runOnly ? [] : services,
46 checks: options.runOnly
47 ? {
48 ready: {
49 type: "http",
50 port: 3000,
51 path: "/health",
52 interval: "2s",
53 timeout: "1s",
54 },
55 }
56 : undefined,
57 },
58 Effect.gen(function* () {
59 yield* Fly.ReadWriteRedis(Cache);
60 const cache = yield* Cache;
61 const boundUrl = yield* cache.url;
62 const stopping = yield* Deferred.make<void>();
63 const { count, runOnly } = globalThis.__ALCHEMY_RUNTIME__
64 ? yield* Effect.all({
65 count: Config.Number("WORKERS"),
66 runOnly: Config.String("RUN_ONLY").pipe(Effect.map((value) => value === "1")),
67 }).pipe(
68 // These controls are plain Machine env, not packed runtime bindings.
69 Effect.provideService(ConfigProvider.ConfigProvider, ConfigProvider.fromEnv()),
70 Effect.orDie,
71 )
72 : { count: options.workers ?? 1, runOnly: options.runOnly ?? false };
73 const ready = new Set<string>();
74 let sharedOpen = true;
75 const url = Effect.gen(function* () {
76 const value = yield* boundUrl;
77 if (!value) return yield* Effect.die(new Error("Redis URL missing"));
78 return Redacted.value(value);
79 });
80 const identify = Effect.gen(function* () {
81 return {
82 machine: yield* Config.String("FLY_MACHINE_ID"),
83 version: yield* Config.String("VERSION"),
84 };
85 }).pipe(Effect.orDie);
86 const connect = url.pipe(
87 Effect.flatMap(Redis.connect),
88 Effect.timeout("5 seconds"),
89 Effect.orDie,
90 );
91 const event = (
92 client: Redis.Connection,
93 name: string,
94 fields: Record<string, unknown> = {},
95 ) =>
96 identify.pipe(
97 Effect.flatMap((identity) =>
98 client.send("EVAL", [
99 scripts.event,
100 0,
101 JSON.stringify({ ...identity, event: name, ...fields }),
102 ]),
103 ),
104 Effect.timeout("5 seconds"),
105 Effect.orDie,
106 );
107 if (globalThis.__ALCHEMY_RUNTIME__) {
108 yield* Effect.addFinalizer(() =>
109 Effect.gen(function* () {
110 const client = yield* connect;
111 yield* event(client, "shared-closed");
112 sharedOpen = false;
113 }).pipe(Effect.scoped),
114 );
115 }
116 const runWorker = (name: string) =>
117 Effect.gen(function* () {
118 const client = yield* connect;
119 const identity = yield* identify;
120 const mode = name === "a" ? yield* Config.String("MODE").pipe(Effect.orDie) : "drain";
121 const delay = yield* Config.Number("AFTER_SIGNAL_MS").pipe(Effect.orDie);
122 const call = (operation: keyof typeof scripts, args: string[]) =>
123 client
124 .send("EVAL", [scripts[operation], 0, ...args])
125 .pipe(Effect.timeout("5 seconds"), Effect.orDie);
126 yield* Effect.addFinalizer(() => event(client, "client-released", { worker: name }));
127 const workScope = yield* Scope.make();
128 yield* Effect.addFinalizer((exit) =>
129 Scope.close(workScope, exit).pipe(
130 Effect.andThen(event(client, "work-closed", { worker: name })),
131 ),
132 );
133 const pollScope = yield* Scope.make();
134 yield* Effect.addFinalizer((exit) => Scope.close(pollScope, exit));
135 const jobs: Fiber.Fiber<void, never>[] = [];
136 let accepting = true;
137 const poll = Effect.gen(function* () {
138 if (!accepting) return;
139 if (name === "a") yield* call("tick", [identity.machine, identity.version]);
140 const response = yield* call("claim", [identity.machine, identity.version]);
141 if (typeof response !== "string") return;
142 const job = JSON.parse(response) as Job;
143 const task = Effect.gen(function* () {
144 let finished = false;
145 yield* Effect.addFinalizer(() =>
146 finished
147 ? Effect.void
148 : call("checkpoint", [job.id, job.job, identity.machine]).pipe(Effect.asVoid),
149 );
150 if (!job.checkpoint && job.kind !== "quick") {
151 yield* Deferred.await(stopping);
152 if (job.kind === "checkpoint" || mode === "job-hang") yield* Effect.never;
153 yield* Effect.sleep(delay);
154 } else yield* Effect.sleep("100 millis");
155 if (!sharedOpen)
156 return yield* Effect.die(new Error("shared dependency closed before job"));
157 yield* call("finish", [job.id, job.job, identity.machine]).pipe(
158 Effect.tap(() =>
159 Effect.sync(() => {
160 finished = true;
161 }),
162 ),
163 Effect.uninterruptible,
164 );
165 }).pipe(Effect.scoped, Effect.interruptible, Effect.forkIn(workScope));
166 jobs.push(yield* task);
167 }).pipe(Effect.uninterruptible);
168 const poller = yield* poll.pipe(
169 Effect.andThen(Effect.sleep("250 millis")),
170 Effect.forever,
171 Effect.forkIn(pollScope),
172 );
173 yield* Effect.addFinalizer(() =>
174 Effect.gen(function* () {
175 yield* event(client, "stop-started", { worker: name });
176 yield* Deferred.succeed(stopping, undefined);
177 if (mode === "stop-hang") yield* Effect.never;
178 if (mode === "stop-delay") yield* Effect.sleep("1 second");
179 accepting = false;
180 yield* Fiber.interrupt(poller);
181 if (mode === "stop-fail") {
182 yield* event(client, "stop-failed", { worker: name });
183 yield* Scope.close(workScope, yield* Effect.exit(Effect.void));
184 return yield* Effect.die(new Error("application stop failure"));
185 }
186 yield* event(client, "stopped", { worker: name });
187 if (mode === "checkpoint")
188 yield* Scope.close(workScope, yield* Effect.exit(Effect.void));
189 yield* Effect.forEach(jobs, Fiber.await, {
190 concurrency: "unbounded",
191 });
192 yield* event(client, "drained", { worker: name });
193 }),
194 );
195 yield* event(client, "worker-ready", { worker: name });
196 ready.add(name);
197 yield* Effect.never;
198 }).pipe(Effect.scoped);
199 const fetch = Effect.gen(function* () {
200 const request = yield* HttpServerRequest;
201 if (request.url === "/health") {
202 return HttpServerResponse.text(ready.size === count ? "ready" : "starting", {
203 status: ready.size === count ? 200 : 503,
204 });
205 }
206 const client = yield* connect;
207 const identity = yield* identify;
208 if (request.url === "/") return yield* HttpServerResponse.json(identity);
209 yield* event(client, "request-started");
210 yield* Effect.addFinalizer(() => event(client, "request-finalized"));
211 const delay = yield* Config.Number("AFTER_SIGNAL_MS").pipe(Effect.orDie);
212 const afterStop = Deferred.await(stopping).pipe(
213 Effect.andThen(Effect.sleep(delay)),
214 Effect.andThen(
215 Effect.sync(() => {
216 if (!sharedOpen) throw new Error("shared dependency closed before response");
217 }),
218 ),
219 Effect.andThen(event(client, "response-finished")),
220 );
221 // Fly's proxy may close a connection that moves no bytes for 60s, and a held
222 // request spans green readiness, promotion, cordon and the old stop grace.
223 // Emit whitespace heartbeats (valid JSON padding / blank stream lines) until
224 // the post-stop tail resolves, as a real long-poll or stream behind Fly would.
225 const heartbeat = (tail: Effect.Effect<string>, beat: string) =>
226 tail.pipe(
227 Effect.forkScoped,
228 Effect.map((fiber) =>
229 Stream.paginate(undefined, () =>
230 Fiber.join(fiber).pipe(
231 Effect.timeoutOption("10 seconds"),
232 Effect.map((last) =>
233 Option.isSome(last)
234 ? ([[last.value], Option.none()] as const)
235 : ([[beat], Option.some(undefined)] as const),
236 ),
237 ),
238 ),
239 ),
240 );
241 const encode = Stream.mapEffect((chunk: string) =>
242 Effect.sync(() => new TextEncoder().encode(chunk)),
243 );
244 if (request.url === "/stream") {
245 const tail = yield* heartbeat(afterStop.pipe(Effect.as("last\n".repeat(32768))), "\n");
246 return HttpServerResponse.stream(
247 Stream.make("first\n".repeat(32768)).pipe(Stream.concat(tail), encode),
248 );
249 }
250 const tail = yield* heartbeat(afterStop.pipe(Effect.as(JSON.stringify(identity))), " ");
251 return HttpServerResponse.stream(Stream.make(" ").pipe(Stream.concat(tail), encode), {
252 contentType: "application/json",
253 });
254 });
255 const host = yield* ServerHost;
256 if (count > 1) yield* host.run(runWorker("b"));
257 if (runOnly) {
258 yield* host.run(
259 Effect.gen(function* () {
260 const server = yield* HttpServer;
261 yield* server.serve(fetch);
262 yield* Effect.never;
263 }).pipe(Effect.provide(NodeHttpServer({ hostname: "::" })), Effect.scoped),
264 );
265 }
266 const run = runWorker("a");
267 return runOnly ? { run } : { run, fetch };
268 }).pipe(Effect.provide(Fly.ReadWriteRedisHttp)),
269 );
270
271 export default workerLayer({
272 version: "default",
273 timeout: "30 seconds",
274 signal: "SIGTERM",
275 });