@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 578 lines 18.3 KB
Raw
1 import * as deadline from "@distilled.cloud/aws/deadline";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags, type Tags } from "alchemy/Tags";
8 import { toWireHours, toWireSeconds } from "alchemy/Util/Duration";
9 import * as Data from "effect/Data";
10 import type * as Duration from "effect/Duration";
11 import * as Effect from "effect/Effect";
12 import type * as Redacted from "effect/Redacted";
13 import * as Schedule from "effect/Schedule";
14 import * as EffectStream from "effect/Stream";
15 import type { Providers } from "../Providers.ts";
16 import {
17 asPlain,
18 deadlineArnOf,
19 fetchDeadlineTags,
20 retryThroughIamPropagation,
21 retryWhileConflict,
22 retryWhileFarmSettling,
23 syncDeadlineTags,
24 } from "./internal.ts";
25
26 export class DeadlineFleetInvalidProps extends Data.TaggedError("DeadlineFleetInvalidProps")<{
27 readonly message: string;
28 readonly cause?: unknown;
29 }> {}
30
31 export class DeadlineFleetUnexpectedResponse extends Data.TaggedError(
32 "DeadlineFleetUnexpectedResponse",
33 )<{
34 readonly message: string;
35 readonly cause?: unknown;
36 }> {}
37
38 export type FleetStatus = deadline.FleetStatus;
39
40 /**
41 * Worker auto-scaling settings for a fleet, with idle time expressed as a
42 * `Duration.Input` (converted to whole seconds on the wire).
43 */
44 export interface FleetAutoScalingConfiguration {
45 /**
46 * Number of idle workers kept on standby to absorb bursts.
47 * @default 0
48 */
49 standbyWorkerCount?: number;
50 /**
51 * How long a worker may sit idle before it is scaled in
52 * (wire: `workerIdleDurationSeconds`).
53 * @default "5 minutes"
54 */
55 workerIdleDuration?: Duration.Input;
56 /**
57 * Maximum number of workers added per minute when scaling out.
58 */
59 scaleOutWorkersPerMinute?: number;
60 }
61
62 /**
63 * Customer-managed fleet configuration (you run the worker hosts).
64 */
65 export interface CustomerManagedFleetConfiguration extends Omit<
66 deadline.CustomerManagedFleetConfiguration,
67 "autoScalingConfiguration"
68 > {
69 /**
70 * Auto-scaling behavior for `EVENT_BASED_AUTO_SCALING` mode.
71 */
72 autoScalingConfiguration?: FleetAutoScalingConfiguration;
73 }
74
75 /**
76 * Persistent EBS volume settings for service-managed EC2 workers, with the
77 * reuse TTL expressed as a `Duration.Input` (converted to whole hours on the
78 * wire).
79 */
80 export interface FleetPersistentVolumeConfiguration extends Omit<
81 deadline.PersistentVolumeConfiguration,
82 "lastUsedTtlHours"
83 > {
84 /**
85 * How long an unused persistent volume is retained for reuse before it is
86 * deleted (wire: `lastUsedTtlHours`).
87 */
88 lastUsedTtl?: Duration.Input;
89 }
90
91 /**
92 * Service-managed EC2 fleet configuration (Deadline provisions instances).
93 */
94 export interface ServiceManagedEc2FleetConfiguration extends Omit<
95 deadline.ServiceManagedEc2FleetConfiguration,
96 "autoScalingConfiguration" | "persistentVolumeConfiguration"
97 > {
98 /**
99 * Auto-scaling behavior for the EC2 workers.
100 */
101 autoScalingConfiguration?: FleetAutoScalingConfiguration;
102 /**
103 * Persistent EBS volume reused across workers.
104 */
105 persistentVolumeConfiguration?: FleetPersistentVolumeConfiguration;
106 }
107
108 /**
109 * Fleet configuration — either `customerManaged` (you run the workers) or
110 * `serviceManagedEc2` (Deadline provisions EC2 instances).
111 */
112 export type FleetConfiguration =
113 | {
114 customerManaged: CustomerManagedFleetConfiguration;
115 serviceManagedEc2?: never;
116 }
117 | {
118 customerManaged?: never;
119 serviceManagedEc2: ServiceManagedEc2FleetConfiguration;
120 };
121
122 /**
123 * Startup script run on each worker host, with the timeout expressed as a
124 * `Duration.Input` (converted to whole seconds on the wire).
125 */
126 export interface FleetHostConfiguration {
127 /**
128 * The script body executed when a worker host starts.
129 */
130 scriptBody: string | Redacted.Redacted<string>;
131 /**
132 * Maximum time the startup script may run before the worker is marked
133 * unhealthy (wire: `scriptTimeoutSeconds`).
134 * @default "5 minutes"
135 */
136 scriptTimeout?: Duration.Input;
137 }
138
139 const toWireAutoScaling = (
140 config: FleetAutoScalingConfiguration | undefined,
141 ): deadline.CustomerManagedAutoScalingConfiguration | undefined => {
142 if (config === undefined) return undefined;
143 const { workerIdleDuration, ...rest } = config;
144 return {
145 ...rest,
146 workerIdleDurationSeconds: toWireSeconds(workerIdleDuration),
147 };
148 };
149
150 const toWirePersistentVolume = (
151 config: FleetPersistentVolumeConfiguration | undefined,
152 ): deadline.PersistentVolumeConfiguration | undefined => {
153 if (config === undefined) return undefined;
154 const { lastUsedTtl, ...rest } = config;
155 return { ...rest, lastUsedTtlHours: toWireHours(lastUsedTtl) };
156 };
157
158 const toWireConfiguration = (config: FleetConfiguration): deadline.FleetConfiguration =>
159 config.customerManaged !== undefined
160 ? {
161 customerManaged: {
162 ...config.customerManaged,
163 autoScalingConfiguration: toWireAutoScaling(
164 config.customerManaged.autoScalingConfiguration,
165 ),
166 },
167 }
168 : {
169 serviceManagedEc2: {
170 ...config.serviceManagedEc2,
171 autoScalingConfiguration: toWireAutoScaling(
172 config.serviceManagedEc2.autoScalingConfiguration,
173 ),
174 persistentVolumeConfiguration: toWirePersistentVolume(
175 config.serviceManagedEc2.persistentVolumeConfiguration,
176 ),
177 },
178 };
179
180 const toWireHostConfiguration = (
181 config: FleetHostConfiguration | undefined,
182 ): deadline.HostConfiguration | undefined => {
183 if (config === undefined) return undefined;
184 const { scriptTimeout, ...rest } = config;
185 return { ...rest, scriptTimeoutSeconds: toWireSeconds(scriptTimeout) };
186 };
187
188 export interface FleetProps {
189 /**
190 * The identifier of the farm the fleet belongs to. Changing it replaces
191 * the fleet.
192 */
193 farmId: string;
194 /**
195 * Display name of the fleet.
196 * @default ${app}-${stage}-${id}
197 */
198 displayName?: string;
199 /**
200 * A description of the fleet.
201 */
202 description?: string;
203 /**
204 * ARN of the IAM role fleet workers assume (trusted by
205 * `credentials.deadline.amazonaws.com`).
206 */
207 roleArn: string;
208 /**
209 * Minimum number of workers the fleet keeps running.
210 * @default 0
211 */
212 minWorkerCount?: number;
213 /**
214 * Maximum number of workers the fleet scales to.
215 */
216 maxWorkerCount: number;
217 /**
218 * Fleet configuration — either `customerManaged` (you run the workers) or
219 * `serviceManagedEc2` (Deadline provisions EC2 instances).
220 */
221 configuration: FleetConfiguration;
222 /**
223 * Script run on each worker host when it starts.
224 */
225 hostConfiguration?: FleetHostConfiguration;
226 /**
227 * Tags to associate with the fleet.
228 */
229 tags?: Record<string, string>;
230 }
231
232 export interface Fleet extends Resource<
233 "AWS.Deadline.Fleet",
234 FleetProps,
235 {
236 /**
237 * The identifier of the farm the fleet belongs to.
238 */
239 farmId: string;
240 /**
241 * Service-assigned unique identifier of the fleet (`fleet-...`).
242 */
243 fleetId: string;
244 /**
245 * ARN of the fleet.
246 */
247 fleetArn: string;
248 /**
249 * The fleet's display name.
250 */
251 displayName: string;
252 /**
253 * Current lifecycle status of the fleet.
254 */
255 status: FleetStatus;
256 /**
257 * Number of workers currently in the fleet.
258 */
259 workerCount: number;
260 /**
261 * The configured minimum worker count.
262 */
263 minWorkerCount: number;
264 /**
265 * The configured maximum worker count.
266 */
267 maxWorkerCount: number;
268 /**
269 * ARN of the fleet's worker role.
270 */
271 roleArn: string;
272 /**
273 * Current tags reported for the fleet.
274 */
275 tags: Record<string, string>;
276 },
277 never,
278 Providers
279 > {}
280
281 /**
282 * An AWS Deadline Cloud fleet — a group of workers (customer-managed hosts
283 * or service-managed EC2 instances) that run render jobs from associated
284 * queues.
285 *
286 * ### Creating Fleets
287 * **Example:** Customer-Managed Fleet
288 * ```typescript
289 * import * as AWS from "@alchemy.run/aws";
290 *
291 * const fleet = yield* AWS.Deadline.Fleet("Workers", {
292 * farmId: farm.farmId,
293 * roleArn: fleetRole.roleArn,
294 * maxWorkerCount: 10,
295 * configuration: {
296 * customerManaged: {
297 * mode: "NO_SCALING",
298 * workerCapabilities: {
299 * vCpuCount: { min: 1 },
300 * memoryMiB: { min: 1024 },
301 * osFamily: "LINUX",
302 * cpuArchitectureType: "x86_64",
303 * },
304 * },
305 * },
306 * });
307 * ```
308 *
309 * **Example:** Service-Managed EC2 Fleet
310 * ```typescript
311 * const fleet = yield* AWS.Deadline.Fleet("Workers", {
312 * farmId: farm.farmId,
313 * roleArn: fleetRole.roleArn,
314 * minWorkerCount: 0,
315 * maxWorkerCount: 5,
316 * configuration: {
317 * serviceManagedEc2: {
318 * instanceCapabilities: {
319 * vCpuCount: { min: 2, max: 8 },
320 * memoryMiB: { min: 4096 },
321 * osFamily: "LINUX",
322 * cpuArchitectureType: "x86_64",
323 * },
324 * instanceMarketOptions: { type: "spot" },
325 * },
326 * },
327 * });
328 * ```
329 *
330 * @resource
331 */
332 export const Fleet = Resource<Fleet>("AWS.Deadline.Fleet");
333
334 const createFleetName = (id: string, props: { displayName?: string | undefined }) =>
335 props.displayName
336 ? Effect.succeed(props.displayName)
337 : createPhysicalName({ id, maxLength: 100 });
338
339 interface FleetState {
340 attrs: Fleet["Attributes"];
341 described: deadline.GetFleetResponse;
342 }
343
344 const readFleetById = Effect.fn(function* (
345 farmId: string,
346 fleetId: string,
347 arnOf: (path: string) => string,
348 ) {
349 const described = yield* deadline
350 .getFleet({ farmId, fleetId })
351 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
352 if (!described) return undefined;
353 const fleetArn = arnOf(`farm/${described.farmId}/fleet/${described.fleetId}`);
354 const state: FleetState = {
355 described,
356 attrs: {
357 farmId: described.farmId,
358 fleetId: described.fleetId,
359 fleetArn,
360 displayName: described.displayName,
361 status: described.status,
362 workerCount: described.workerCount,
363 minWorkerCount: described.minWorkerCount,
364 maxWorkerCount: described.maxWorkerCount,
365 roleArn: described.roleArn,
366 tags: yield* fetchDeadlineTags(fleetArn),
367 },
368 };
369 return state;
370 });
371
372 const findFleetByDisplayName = Effect.fn(function* (
373 farmId: string,
374 displayName: string,
375 arnOf: (path: string) => string,
376 ) {
377 const summaries = yield* deadline.listFleets.items({ farmId }).pipe(
378 EffectStream.runCollect,
379 Effect.map((chunk) => Array.from(chunk)),
380 // The parent farm may itself be gone.
381 Effect.catchTag("ResourceNotFoundException", () =>
382 Effect.succeed([] as deadline.FleetSummary[]),
383 ),
384 );
385 const match = summaries.find((summary) => summary.displayName === displayName);
386 if (!match) return undefined;
387 return yield* readFleetById(farmId, match.fleetId, arnOf);
388 });
389
390 /**
391 * A fleet still transitioning toward the awaited state — retried by the
392 * bounded wait schedules below.
393 */
394 class FleetNotReady extends Data.TaggedError("FleetNotReady")<{
395 readonly fleetId: string;
396 readonly status: string | undefined;
397 }> {}
398
399 /**
400 * A fleet whose asynchronous provisioning converged to a terminal failed
401 * status (`CREATE_FAILED` / `UPDATE_FAILED`).
402 */
403 export class FleetProvisioningFailed extends Data.TaggedError("FleetProvisioningFailed")<{
404 readonly fleetId: string;
405 readonly status: string;
406 readonly message: string | undefined;
407 }> {}
408
409 const retryWhileNotReady = <A, E extends { readonly _tag: string }, R>(
410 self: Effect.Effect<A, E, R>,
411 ): Effect.Effect<A, E, R> =>
412 Effect.retry(self, {
413 while: (e) => e._tag === "FleetNotReady",
414 // Fleet activation is usually well under a minute; stay within the
415 // provider-wide bounded provisioning budget.
416 schedule: Schedule.max([Schedule.spaced("6 seconds"), Schedule.recurs(9)]),
417 });
418
419 const waitForFleetActive = (farmId: string, fleetId: string, arnOf: (path: string) => string) =>
420 retryWhileNotReady(
421 Effect.gen(function* () {
422 const state = yield* readFleetById(farmId, fleetId, arnOf);
423 if (state === undefined) {
424 return yield* new FleetNotReady({ fleetId, status: undefined });
425 }
426 if (
427 state.described.status === "CREATE_FAILED" ||
428 state.described.status === "UPDATE_FAILED"
429 ) {
430 return yield* new FleetProvisioningFailed({
431 fleetId,
432 status: state.described.status,
433 message: state.described.statusMessage,
434 });
435 }
436 if (state.described.status !== "ACTIVE" && state.described.status !== "SUSPENDED") {
437 return yield* new FleetNotReady({ fleetId, status: state.described.status });
438 }
439 return state;
440 }),
441 );
442
443 const waitUntilFleetGone = (farmId: string, fleetId: string) =>
444 retryWhileNotReady(
445 Effect.gen(function* () {
446 const described = yield* deadline
447 .getFleet({ farmId, fleetId })
448 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
449 if (described !== undefined) {
450 return yield* new FleetNotReady({ fleetId, status: described.status });
451 }
452 }),
453 ).pipe(
454 // Exhausted retries: deletion is already converging server-side.
455 Effect.catchTag("FleetNotReady", () => Effect.void),
456 );
457
458 export const FleetProvider = () =>
459 Provider.effect(
460 Fleet,
461 Effect.gen(function* () {
462 return {
463 stables: ["farmId", "fleetId", "fleetArn", "roleArn"],
464 // Keyed by a parent farm — sub-resource list() convention.
465 list: () => Effect.succeed([]),
466 read: Effect.fn(function* ({ id, olds, output }) {
467 const arnOf = yield* deadlineArnOf;
468 const farmId = output?.farmId ?? olds?.farmId;
469 if (farmId === undefined) return undefined;
470 const state = output?.fleetId
471 ? yield* readFleetById(farmId, output.fleetId, arnOf)
472 : yield* findFleetByDisplayName(farmId, yield* createFleetName(id, olds ?? {}), arnOf);
473 if (!state) return undefined;
474 return (yield* hasAlchemyTags(id, state.attrs.tags as Tags))
475 ? state.attrs
476 : Unowned(state.attrs);
477 }),
478 diff: Effect.fn(function* ({ news, olds }) {
479 if (!isResolved(news)) return;
480 if (olds === undefined) return;
481 // The parent farm is fixed at creation.
482 if (olds.farmId !== news.farmId) {
483 return { action: "replace" } as const;
484 }
485 }),
486 reconcile: Effect.fn(function* ({ id, news, output, session }) {
487 if (news === undefined) {
488 return yield* new DeadlineFleetInvalidProps({
489 message: "AWS.Deadline.Fleet requires props",
490 });
491 }
492 const arnOf = yield* deadlineArnOf;
493 const farmId = news.farmId;
494 const displayName = yield* createFleetName(id, news);
495 const internalTags = yield* createInternalTags(id);
496 const desiredTags = { ...internalTags, ...news.tags };
497
498 // Observe.
499 let state = output?.fleetId
500 ? yield* readFleetById(farmId, output.fleetId, arnOf)
501 : yield* findFleetByDisplayName(farmId, displayName, arnOf);
502
503 // Ensure — create if missing, then wait for ACTIVE.
504 if (state === undefined) {
505 const created = yield* deadline
506 .createFleet({
507 farmId,
508 displayName,
509 description: news.description,
510 roleArn: news.roleArn,
511 minWorkerCount: news.minWorkerCount,
512 maxWorkerCount: news.maxWorkerCount,
513 configuration: toWireConfiguration(news.configuration),
514 hostConfiguration: toWireHostConfiguration(news.hostConfiguration),
515 tags: desiredTags,
516 })
517 .pipe(retryThroughIamPropagation, retryWhileFarmSettling);
518 yield* session.note(`Creating fleet ${displayName} (${created.fleetId})...`);
519 state = yield* waitForFleetActive(farmId, created.fleetId, arnOf);
520 }
521
522 // Sync mutable settings — only when drifted from OBSERVED state.
523 const described = state.described;
524 const needsUpdate =
525 displayName !== described.displayName ||
526 (news.description !== undefined &&
527 news.description !== (asPlain(described.description) ?? "")) ||
528 news.roleArn !== described.roleArn ||
529 (news.minWorkerCount !== undefined &&
530 news.minWorkerCount !== described.minWorkerCount) ||
531 news.maxWorkerCount !== described.maxWorkerCount ||
532 news.configuration !== undefined;
533 if (needsUpdate) {
534 yield* retryWhileConflict(
535 deadline
536 .updateFleet({
537 farmId,
538 fleetId: state.attrs.fleetId,
539 displayName,
540 description: news.description,
541 roleArn: news.roleArn,
542 minWorkerCount: news.minWorkerCount,
543 maxWorkerCount: news.maxWorkerCount,
544 configuration: toWireConfiguration(news.configuration),
545 hostConfiguration: toWireHostConfiguration(news.hostConfiguration),
546 })
547 .pipe(retryThroughIamPropagation),
548 );
549 state = yield* waitForFleetActive(farmId, state.attrs.fleetId, arnOf);
550 yield* session.note(`Updated fleet ${displayName}`);
551 }
552
553 // Sync tags — diff against observed cloud tags.
554 yield* syncDeadlineTags(state.attrs.fleetArn, desiredTags);
555
556 yield* session.note(state.attrs.fleetArn);
557 const final = yield* readFleetById(farmId, state.attrs.fleetId, arnOf);
558 if (!final) {
559 return yield* new DeadlineFleetUnexpectedResponse({
560 message: `failed to read reconciled fleet ${displayName}`,
561 });
562 }
563 return final.attrs;
564 }),
565 delete: Effect.fn(function* ({ output }) {
566 yield* retryWhileConflict(
567 deadline.deleteFleet({
568 farmId: output.farmId,
569 fleetId: output.fleetId,
570 }),
571 ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
572 // Fleet deletion drains workers asynchronously; wait until gone so
573 // the parent farm's deletion does not hit a dependency conflict.
574 yield* waitUntilFleetGone(output.farmId, output.fleetId);
575 }),
576 };
577 }),
578 );