@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, 04:45 UTC

main
ts 124 lines 4.28 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * The `detail` payload Amazon EMR Serverless delivers to EventBridge.
11 * Job-run state-change events describe the run's transition; application
12 * state-change events describe the application's lifecycle. Fields not
13 * shared by every event kind are optional (the schema grows over time).
14 */
15 export interface JobRunEventDetail {
16 /** The id of the EMR Serverless application the event is about. */
17 applicationId?: string;
18 /** Job-run events: the id of the job run. */
19 jobRunId?: string;
20 /** The state transitioned to, e.g. `RUNNING`, `SUCCESS`, `FAILED`. */
21 state?: string;
22 /** Why the state changed (e.g. the failure reason). */
23 stateDetails?: string;
24 /** Additional event fields (the schema grows over time). */
25 [key: string]: unknown;
26 }
27
28 /** An EMR Serverless EventBridge event delivered to the handler. */
29 export type JobRunEvent = EventRecord<JobRunEventDetail>;
30
31 /** Which EMR Serverless notifications to subscribe to. */
32 export type JobRunEventKind = "job-run-state-change" | "application-state-change";
33
34 const DETAIL_TYPES: Record<JobRunEventKind, string> = {
35 "job-run-state-change": "EMR Serverless Job Run State Change",
36 "application-state-change": "EMR Serverless Application State Change",
37 };
38
39 export interface JobRunEventSourceProps extends EventRouteProps {
40 /**
41 * Logical id for the backing EventBridge rule.
42 * @default "EMRServerlessJobRunEvents"
43 */
44 id?: string;
45 /**
46 * Which notifications to subscribe to: job-run state changes and/or
47 * application state changes.
48 * @default ["job-run-state-change"]
49 */
50 kinds?: readonly JobRunEventKind[];
51 /**
52 * Restrict to events about specific applications (matched against the
53 * event detail's `applicationId`).
54 */
55 applicationIds?: readonly string[];
56 /**
57 * Restrict to specific states (matched against the event detail's
58 * `state`), e.g. `["FAILED"]` to alert only on failures.
59 */
60 states?: readonly string[];
61 }
62
63 /**
64 * Event source connecting Amazon EMR Serverless notifications to the
65 * hosting compute. EMR Serverless publishes job-run state changes (most
66 * importantly a job dropping into `FAILED`) and application state changes
67 * to the account's default EventBridge bus (source `aws.emr-serverless`);
68 * this subscribes the host Function to those events so it can alert on
69 * failed jobs or chain follow-up work when a job succeeds.
70 *
71 * EMR Serverless publishes to EventBridge automatically — no additional
72 * resource is created besides the EventBridge rule targeting the host.
73 * Provide the host-specific implementation layer (e.g.
74 * `AWS.Lambda.EventSource`) on the Function effect.
75 *
76 * ### Consuming Job Run Events
77 * **Example:** Alert On Failed Jobs
78 * ```typescript
79 * import * as AWS from "@alchemy.run/aws";
80 *
81 * export default AlertFunction.make(
82 * { main: import.meta.url },
83 * Effect.gen(function* () {
84 * yield* AWS.EMRServerless.consumeJobRunEvents(
85 * { states: ["FAILED"] },
86 * (events) =>
87 * Stream.runForEach(events, (event) =>
88 * Effect.logError(
89 * `job ${event.detail.jobRunId} failed: ${event.detail.stateDetails}`,
90 * ),
91 * ),
92 * );
93 * return {};
94 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
95 * );
96 * ```
97 */
98 export const consumeJobRunEvents = <StreamReq = never, Req = never>(
99 props: JobRunEventSourceProps,
100 process: (
101 events: Stream.Stream<JobRunEvent, never, StreamReq>,
102 ) => Effect.Effect<void, never, Req>,
103 ) =>
104 consumeBusEvents(
105 props.id ?? "EMRServerlessJobRunEvents",
106 {
107 source: ["aws.emr-serverless"],
108 "detail-type": (props.kinds ?? (["job-run-state-change"] as const)).map(
109 (kind) => DETAIL_TYPES[kind],
110 ),
111 ...(props.applicationIds !== undefined || props.states !== undefined
112 ? {
113 detail: {
114 ...(props.applicationIds !== undefined
115 ? { applicationId: [...props.applicationIds] }
116 : {}),
117 ...(props.states !== undefined ? { state: [...props.states] } : {}),
118 },
119 }
120 : {}),
121 },
122 { description: props.description, state: props.state },
123 process,
124 );