@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 165 lines 5.7 KB
Raw
1 import type * as apprunner from "@distilled.cloud/aws/apprunner";
2 import * as Effect from "effect/Effect";
3 import type * as Stream from "effect/Stream";
4 import {
5 consumeBusEvents,
6 type EventRecord,
7 type EventRouteProps,
8 } from "../EventBridge/EventSource.ts";
9
10 /**
11 * The `detail` payload App Runner delivers to EventBridge when a service's
12 * lifecycle status changes (e.g. `OPERATION_IN_PROGRESS` -> `RUNNING`).
13 */
14 export interface ServiceStatusChangeDetail {
15 /** ID of the App Runner service whose status changed. */
16 serviceId: string;
17 /** Name of the App Runner service. */
18 serviceName: string;
19 /** The status the service transitioned into (e.g. `RUNNING`, `PAUSED`). */
20 currentStatus: apprunner.ServiceStatus;
21 /** The status the service transitioned out of. */
22 previousStatus?: apprunner.ServiceStatus;
23 }
24
25 /**
26 * The `detail` payload App Runner delivers to EventBridge when an
27 * asynchronous operation (deployment, pause, resume, create, delete,
28 * update) changes status.
29 */
30 export interface OperationStatusChangeDetail {
31 /** ID of the App Runner service the operation ran on. */
32 serviceId: string;
33 /** Name of the App Runner service. */
34 serviceName: string;
35 /** ID of the operation (matches `OperationId` from the initiating call). */
36 operationId?: string;
37 /**
38 * New status of the operation, e.g. `DeploymentCompletedSuccessfully`,
39 * `PauseServiceCompletedSuccessfully`, `DeploymentFailed`.
40 */
41 operationStatus: string;
42 }
43
44 /** A service-status-change EventBridge event delivered to the handler. */
45 export type ServiceStatusChangeEvent = EventRecord<ServiceStatusChangeDetail>;
46
47 /** An operation-status-change EventBridge event delivered to the handler. */
48 export type OperationStatusChangeEvent = EventRecord<OperationStatusChangeDetail>;
49
50 export interface ServiceStatusChangeEventSourceProps extends EventRouteProps {
51 /**
52 * Logical id prefix for the backing EventBridge rule.
53 * @default "AppRunner"
54 */
55 id?: string;
56 /**
57 * Only deliver events whose `currentStatus` is one of these values. Omit
58 * to receive every status transition.
59 */
60 currentStatus?: apprunner.ServiceStatus[];
61 }
62
63 export interface OperationStatusChangeEventSourceProps extends EventRouteProps {
64 /**
65 * Logical id prefix for the backing EventBridge rule.
66 * @default "AppRunner"
67 */
68 id?: string;
69 /**
70 * Only deliver events whose `operationStatus` is one of these values
71 * (e.g. `["DeploymentCompletedSuccessfully", "DeploymentFailed"]`). Omit
72 * to receive every operation transition.
73 */
74 operationStatus?: string[];
75 }
76
77 /**
78 * Deliver App Runner service status changes (`aws.apprunner` /
79 * `"AppRunner Service Status Change"` EventBridge events) to the host
80 * Function — e.g. to alert when a service unexpectedly leaves `RUNNING`.
81 *
82 * The EventBridge pattern matches every App Runner service in the account
83 * and region (the pattern must be literal — `Output` values such as
84 * `service.serviceId` do not resolve inside the deployed bundle); inspect
85 * `event.detail.serviceName` / `event.detail.serviceId` in the handler if
86 * multiple services share the Function. Provide `AWS.Lambda.EventSource` on
87 * the Function effect to implement the subscription.
88 *
89 * ### Reacting To Status Changes
90 * **Example:** Alert when a Service Pauses
91 * ```typescript
92 * yield* AWS.AppRunner.consumeServiceStatusChanges(
93 * { currentStatus: ["PAUSED"] },
94 * (events) =>
95 * Stream.runForEach(events, (event) =>
96 * Effect.log(`${event.detail.serviceName} is paused`),
97 * ),
98 * );
99 * ```
100 */
101 export const consumeServiceStatusChanges = <StreamReq = never, Req = never>(
102 props: ServiceStatusChangeEventSourceProps,
103 process: (
104 events: Stream.Stream<ServiceStatusChangeEvent, never, StreamReq>,
105 ) => Effect.Effect<void, never, Req>,
106 ) =>
107 Effect.gen(function* () {
108 const { id, currentStatus, ...routeProps } = props;
109
110 yield* consumeBusEvents(
111 `${id ?? "AppRunner"}-ServiceStatusChanges`,
112 {
113 source: ["aws.apprunner"],
114 "detail-type": ["AppRunner Service Status Change"],
115 ...(currentStatus && currentStatus.length > 0 ? { detail: { currentStatus } } : {}),
116 },
117 routeProps,
118 process,
119 );
120 });
121
122 /**
123 * Deliver App Runner operation status changes (`aws.apprunner` /
124 * `"AppRunner Service Operation Status Change"` EventBridge events) to the
125 * host Function — e.g. to notify a chat channel when a deployment completes
126 * or fails.
127 *
128 * The EventBridge pattern matches every App Runner service in the account
129 * and region; inspect `event.detail.serviceName` / `event.detail.operationId`
130 * in the handler if multiple services share the Function. Provide
131 * `AWS.Lambda.EventSource` on the Function effect to implement the
132 * subscription.
133 *
134 * ### Reacting To Deployments
135 * **Example:** Alert on Failed Deployments
136 * ```typescript
137 * yield* AWS.AppRunner.consumeOperationStatusChanges(
138 * { operationStatus: ["DeploymentFailed"] },
139 * (events) =>
140 * Stream.runForEach(events, (event) =>
141 * Effect.log(`deployment failed on ${event.detail.serviceName}`),
142 * ),
143 * );
144 * ```
145 */
146 export const consumeOperationStatusChanges = <StreamReq = never, Req = never>(
147 props: OperationStatusChangeEventSourceProps,
148 process: (
149 events: Stream.Stream<OperationStatusChangeEvent, never, StreamReq>,
150 ) => Effect.Effect<void, never, Req>,
151 ) =>
152 Effect.gen(function* () {
153 const { id, operationStatus, ...routeProps } = props;
154
155 yield* consumeBusEvents(
156 `${id ?? "AppRunner"}-OperationStatusChanges`,
157 {
158 source: ["aws.apprunner"],
159 "detail-type": ["AppRunner Service Operation Status Change"],
160 ...(operationStatus && operationStatus.length > 0 ? { detail: { operationStatus } } : {}),
161 },
162 routeProps,
163 process,
164 );
165 });