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

main
ts 84 lines 3.03 KB
Raw
1 import * as emr from "@distilled.cloud/aws/emr-serverless";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /**
7 * Raised when an EMR Serverless application never settles into the expected
8 * state within the bounded polling budget.
9 */
10 export class EmrServerlessStateTimeout extends Data.TaggedError("EmrServerlessStateTimeout")<{
11 readonly applicationId: string;
12 readonly expected: readonly string[];
13 readonly actual: string | undefined;
14 readonly stateDetails: string | undefined;
15 }> {}
16
17 /**
18 * Poll `getApplication` (bounded, spaced 5s, ~2 min budget) until the
19 * application leaves the given transitional state. Explicitly typed so
20 * `Effect.repeat`'s conditional return type does not widen the provider layer
21 * in declaration emit (see PATTERNS §7).
22 */
23 const untilNotInState = <E, R>(
24 self: Effect.Effect<emr.GetApplicationResponse, E, R>,
25 transitional: readonly string[],
26 ): Effect.Effect<emr.Application, E, R> =>
27 Effect.repeat(
28 Effect.map(self, (response) => response.application),
29 {
30 schedule: Schedule.spaced("5 seconds"),
31 until: (application) => !transitional.includes(application.state),
32 times: 24,
33 },
34 );
35
36 /**
37 * Await an application leaving `CREATING`, failing with
38 * `EmrServerlessStateTimeout` if it does not settle into `CREATED` (or an
39 * auto-started `STARTING`/`STARTED`) within the budget.
40 */
41 export const awaitApplicationCreated = Effect.fn("AWS.EMRServerless.awaitApplicationCreated")(
42 function* (applicationId: string) {
43 const application = yield* untilNotInState(emr.getApplication({ applicationId }), ["CREATING"]);
44 const settled = ["CREATED", "STARTING", "STARTED"];
45 if (!settled.includes(application.state)) {
46 return yield* new EmrServerlessStateTimeout({
47 applicationId,
48 expected: settled,
49 actual: application.state,
50 stateDetails: application.stateDetails,
51 });
52 }
53 return application;
54 },
55 );
56
57 /**
58 * Await an application settling into an updatable/deletable state (`CREATED`
59 * or `STOPPED`), draining `STARTING`/`STOPPING` transitions. The caller is
60 * responsible for issuing `stopApplication` first when the application is
61 * `STARTED`.
62 */
63 export const awaitApplicationStopped = Effect.fn("AWS.EMRServerless.awaitApplicationStopped")(
64 function* (applicationId: string) {
65 const application = yield* untilNotInState(emr.getApplication({ applicationId }), [
66 "CREATING",
67 "STARTING",
68 "STOPPING",
69 ]);
70 // TERMINATED is tolerated: a concurrent delete winning the race is a settled
71 // outcome for both the update path (the follow-up call surfaces the truth)
72 // and the delete path (nothing left to delete).
73 const settled = ["CREATED", "STOPPED", "TERMINATED"];
74 if (!settled.includes(application.state)) {
75 return yield* new EmrServerlessStateTimeout({
76 applicationId,
77 expected: settled,
78 actual: application.state,
79 stateDetails: application.stateDetails,
80 });
81 }
82 return application;
83 },
84 );