@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 120 lines 3.91 KB
Raw
1 import type * as aas from "@distilled.cloud/aws/application-auto-scaling";
2 import type * as Effect from "effect/Effect";
3 import type * as Stream from "effect/Stream";
4 import { consumeBusEvents, type EventRecord } from "../EventBridge/EventSource.ts";
5 import type { ScalableTarget } from "./ScalableTarget.ts";
6
7 /**
8 * The `detail` payload Application Auto Scaling delivers to EventBridge for a
9 * `Application Auto Scaling Scaling Activity State Change` event. Emitted
10 * when a scale-out reaches the scalable target's maximum capacity
11 * (`scaledToMax: true`) — further demand cannot be absorbed until the
12 * target's `maxCapacity` is raised.
13 */
14 export interface ScalingActivityEventDetail {
15 /**
16 * ISO-8601 time the scaling activity started.
17 */
18 startTime: string;
19 /**
20 * ISO-8601 time the scaling activity ended.
21 */
22 endTime: string;
23 /**
24 * Desired capacity after the scaling activity.
25 */
26 newDesiredCapacity: number;
27 /**
28 * Desired capacity before the scaling activity.
29 */
30 oldDesiredCapacity: number;
31 /**
32 * Minimum capacity of the scalable target.
33 */
34 minCapacity: number;
35 /**
36 * Maximum capacity of the scalable target.
37 */
38 maxCapacity: number;
39 /**
40 * Identifier of the scaled resource, e.g. `table/my-table`.
41 */
42 resourceId: string;
43 /**
44 * Scalable dimension of the target, e.g.
45 * `dynamodb:table:WriteCapacityUnits`.
46 */
47 scalableDimension: aas.ScalableDimension;
48 /**
49 * Namespace of the AWS service that provides the resource, e.g. `dynamodb`.
50 */
51 serviceNamespace: aas.ServiceNamespace;
52 /**
53 * Final status of the scaling activity (e.g. `Successful`).
54 */
55 statusCode: aas.ScalingActivityStatusCode;
56 /**
57 * Whether the activity scaled the target out to its maximum size limit.
58 */
59 scaledToMax: boolean;
60 /**
61 * Direction of the scaling activity (`scale-out` or `scale-in`).
62 */
63 direction?: string;
64 }
65
66 /** A scaling-activity EventBridge event delivered to the handler. */
67 export type ScalingActivityEvent = EventRecord<ScalingActivityEventDetail>;
68
69 export interface ScalingActivityEventSourceProps {
70 /**
71 * Logical id prefix for the EventBridge rule. Defaults to the scalable
72 * target's logical id.
73 */
74 id?: string;
75 }
76
77 /**
78 * Deliver Application Auto Scaling scaling-activity state-change events to
79 * the host Function via EventBridge. Application Auto Scaling emits these
80 * events when a scale-out pins a scalable target at its maximum capacity
81 * (`detail.scaledToMax`), e.g. to alert that demand exceeds the configured
82 * ceiling or to raise `maxCapacity` automatically.
83 *
84 * The EventBridge pattern matches every scaling-activity event in the
85 * account; inspect `event.detail.resourceId` / `event.detail.serviceNamespace`
86 * in the handler if multiple targets share the Function.
87 *
88 * ### Reacting to Scaling Activity
89 * **Example:** Alert When a Target Is Pinned at Max Capacity
90 * ```typescript
91 * yield* consumeScalingActivityEvents(target, {}, (events) =>
92 * Stream.runForEach(events, (event) =>
93 * event.detail.scaledToMax
94 * ? Effect.log(
95 * `${event.detail.resourceId} is pinned at max capacity ${event.detail.maxCapacity}`,
96 * )
97 * : Effect.void,
98 * ),
99 * );
100 * ```
101 */
102 export const consumeScalingActivityEvents = <StreamReq = never, Req = never>(
103 target: ScalableTarget,
104 props: ScalingActivityEventSourceProps,
105 process: (
106 events: Stream.Stream<ScalingActivityEvent, never, StreamReq>,
107 ) => Effect.Effect<void, never, Req>,
108 ) =>
109 // The pattern uses only literal `source` + `detail-type` so it round-trips
110 // through both the deploy-time rule and the runtime matcher (Output values
111 // cannot appear in the pattern — they don't resolve inside the deployed
112 // bundle).
113 consumeBusEvents(
114 `${props.id ?? target.LogicalId}-ScalingActivityEvents`,
115 {
116 source: ["aws.application-autoscaling"],
117 "detail-type": ["Application Auto Scaling Scaling Activity State Change"],
118 },
119 process,
120 );