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
| 1 | import * as analytics from "@distilled.cloud/aws/kinesis-analytics-v2"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export type SnapshotStatus = analytics.SnapshotStatus; |
| 12 | |
| 13 | export interface ApplicationSnapshotProps { |
| 14 | /** |
| 15 | * Name of the Managed Service for Apache Flink application to snapshot. |
| 16 | * The application must be `RUNNING` with snapshots enabled. Changing the |
| 17 | * application replaces the snapshot. |
| 18 | */ |
| 19 | applicationName: string; |
| 20 | /** |
| 21 | * Name of the snapshot. Changing the name replaces the snapshot. |
| 22 | * @default ${app}-${id}-${stage}-${instanceId} |
| 23 | */ |
| 24 | snapshotName?: string; |
| 25 | } |
| 26 | |
| 27 | export interface ApplicationSnapshot extends Resource< |
| 28 | "AWS.KinesisAnalyticsV2.ApplicationSnapshot", |
| 29 | ApplicationSnapshotProps, |
| 30 | { |
| 31 | /** |
| 32 | * Name of the application the snapshot belongs to. |
| 33 | */ |
| 34 | applicationName: string; |
| 35 | /** |
| 36 | * Physical name of the snapshot. |
| 37 | */ |
| 38 | snapshotName: string; |
| 39 | /** |
| 40 | * Current status of the snapshot. |
| 41 | */ |
| 42 | snapshotStatus: SnapshotStatus; |
| 43 | /** |
| 44 | * Application version the snapshot was taken from. |
| 45 | */ |
| 46 | applicationVersionId: number; |
| 47 | }, |
| 48 | never, |
| 49 | Providers |
| 50 | > {} |
| 51 | |
| 52 | /** |
| 53 | * A snapshot (Flink savepoint) of a running Managed Service for Apache |
| 54 | * Flink application's state. |
| 55 | * |
| 56 | * Snapshots are immutable — every prop change replaces the snapshot. The |
| 57 | * source application must be `RUNNING` with `snapshotsEnabled: true` when |
| 58 | * the snapshot is created. |
| 59 | * ### Creating Snapshots |
| 60 | * **Example:** Snapshot a running application |
| 61 | * ```typescript |
| 62 | * import * as AWS from "@alchemy.run/aws"; |
| 63 | * |
| 64 | * const app = yield* AWS.KinesisAnalyticsV2.Application("Enrichment", { |
| 65 | * runtimeEnvironment: "FLINK-1_20", |
| 66 | * code: { bucketArn: bucket.bucketArn, fileKey: "jobs/enrichment-1.0.jar" }, |
| 67 | * snapshotsEnabled: true, |
| 68 | * start: true, |
| 69 | * }); |
| 70 | * const snapshot = yield* AWS.KinesisAnalyticsV2.ApplicationSnapshot( |
| 71 | * "Checkpoint", |
| 72 | * { applicationName: app.applicationName }, |
| 73 | * ); |
| 74 | * ``` |
| 75 | * |
| 76 | * @resource |
| 77 | */ |
| 78 | export const ApplicationSnapshot = Resource<ApplicationSnapshot>( |
| 79 | "AWS.KinesisAnalyticsV2.ApplicationSnapshot", |
| 80 | ); |
| 81 | |
| 82 | /** |
| 83 | * The snapshot entered `FAILED` (or vanished) while waiting for `READY`. |
| 84 | */ |
| 85 | export class SnapshotFailed extends Data.TaggedError("SnapshotFailed")<{ |
| 86 | readonly applicationName: string; |
| 87 | readonly snapshotName: string; |
| 88 | readonly status: string; |
| 89 | }> {} |
| 90 | |
| 91 | class SnapshotPending extends Data.TaggedError("SnapshotPending")<{ |
| 92 | readonly status: string; |
| 93 | }> {} |
| 94 | |
| 95 | class SnapshotStillExists extends Data.TaggedError("SnapshotStillExists") {} |
| 96 | |
| 97 | const createSnapshotName = (id: string, props: { snapshotName?: string | undefined }) => |
| 98 | Effect.gen(function* () { |
| 99 | if (props.snapshotName) { |
| 100 | return props.snapshotName; |
| 101 | } |
| 102 | return yield* createPhysicalName({ id, maxLength: 256 }); |
| 103 | }); |
| 104 | |
| 105 | /** |
| 106 | * Retries `ResourceInUseException` (the application is transitioning, e.g. |
| 107 | * an update settling before the snapshot can start) on a bounded schedule. |
| 108 | * Explicit return annotation for the declaration-emit reason documented in |
| 109 | * `Application.ts`. |
| 110 | */ |
| 111 | const retryWhileInUse = <A, E extends { _tag: string }, R>( |
| 112 | self: Effect.Effect<A, E, R>, |
| 113 | ): Effect.Effect<A, E, R> => |
| 114 | Effect.retry(self, { |
| 115 | while: (e) => e._tag === "ResourceInUseException", |
| 116 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(30)]), |
| 117 | }); |
| 118 | |
| 119 | const describeSnapshot = Effect.fn(function* ({ |
| 120 | applicationName, |
| 121 | snapshotName, |
| 122 | }: { |
| 123 | applicationName: string; |
| 124 | snapshotName: string; |
| 125 | }) { |
| 126 | const response = yield* analytics |
| 127 | .describeApplicationSnapshot({ |
| 128 | ApplicationName: applicationName, |
| 129 | SnapshotName: snapshotName, |
| 130 | }) |
| 131 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 132 | return response?.SnapshotDetails; |
| 133 | }); |
| 134 | |
| 135 | // Snapshot creation is proportional to state size; small test jobs finish |
| 136 | // in seconds. 5s × 36 bounds the wait at 180s. |
| 137 | const waitForSnapshotReady = ({ |
| 138 | applicationName, |
| 139 | snapshotName, |
| 140 | }: { |
| 141 | applicationName: string; |
| 142 | snapshotName: string; |
| 143 | }) => |
| 144 | Effect.gen(function* () { |
| 145 | const details = yield* describeSnapshot({ applicationName, snapshotName }); |
| 146 | const status = details?.SnapshotStatus ?? "MISSING"; |
| 147 | if (status === "READY") { |
| 148 | return details!; |
| 149 | } |
| 150 | if (status === "CREATING") { |
| 151 | return yield* new SnapshotPending({ status }); |
| 152 | } |
| 153 | return yield* new SnapshotFailed({ applicationName, snapshotName, status }); |
| 154 | }).pipe( |
| 155 | Effect.retry({ |
| 156 | while: (e: { _tag: string }) => e._tag === "SnapshotPending", |
| 157 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(36)]), |
| 158 | }), |
| 159 | Effect.catchTag("SnapshotPending", (e) => |
| 160 | Effect.fail(new SnapshotFailed({ applicationName, snapshotName, status: e.status })), |
| 161 | ), |
| 162 | ); |
| 163 | |
| 164 | const waitForSnapshotDeleted = ({ |
| 165 | applicationName, |
| 166 | snapshotName, |
| 167 | }: { |
| 168 | applicationName: string; |
| 169 | snapshotName: string; |
| 170 | }) => |
| 171 | Effect.gen(function* () { |
| 172 | const details = yield* describeSnapshot({ applicationName, snapshotName }); |
| 173 | if (details !== undefined) { |
| 174 | return yield* new SnapshotStillExists(); |
| 175 | } |
| 176 | }).pipe( |
| 177 | Effect.retry({ |
| 178 | while: (e: { _tag: string }) => e._tag === "SnapshotStillExists", |
| 179 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(30)]), |
| 180 | }), |
| 181 | ); |
| 182 | |
| 183 | export const ApplicationSnapshotProvider = () => |
| 184 | Provider.effect( |
| 185 | ApplicationSnapshot, |
| 186 | Effect.gen(function* () { |
| 187 | return ApplicationSnapshot.Provider.of({ |
| 188 | stables: ["applicationName", "snapshotName", "applicationVersionId"], |
| 189 | |
| 190 | // Sub-resource keyed by its parent application — snapshots are not |
| 191 | // enumerable account-wide, so `list` is empty and refresh flows |
| 192 | // through `read` on known instances. |
| 193 | list: () => Effect.succeed([]), |
| 194 | |
| 195 | read: Effect.fn(function* ({ id, olds, output }) { |
| 196 | const applicationName = output?.applicationName ?? olds?.applicationName; |
| 197 | if (!applicationName) return undefined; |
| 198 | const snapshotName = output?.snapshotName ?? (yield* createSnapshotName(id, olds ?? {})); |
| 199 | const details = yield* describeSnapshot({ |
| 200 | applicationName, |
| 201 | snapshotName, |
| 202 | }); |
| 203 | if (!details) return undefined; |
| 204 | return { |
| 205 | applicationName, |
| 206 | snapshotName, |
| 207 | snapshotStatus: details.SnapshotStatus, |
| 208 | applicationVersionId: details.ApplicationVersionId, |
| 209 | }; |
| 210 | }), |
| 211 | |
| 212 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 213 | if (!isResolved(news)) return undefined; |
| 214 | // Snapshots are immutable — any identity change replaces. |
| 215 | const oldName = yield* createSnapshotName(id, olds ?? {}); |
| 216 | const newName = yield* createSnapshotName(id, news ?? {}); |
| 217 | if (olds?.applicationName !== news?.applicationName || oldName !== newName) { |
| 218 | return { action: "replace" } as const; |
| 219 | } |
| 220 | }), |
| 221 | |
| 222 | // Existence-only resource: observe → if missing, create → wait for |
| 223 | // READY. Nothing about a snapshot is mutable. |
| 224 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 225 | const applicationName = news.applicationName; |
| 226 | const snapshotName = output?.snapshotName ?? (yield* createSnapshotName(id, news)); |
| 227 | |
| 228 | const observed = yield* describeSnapshot({ |
| 229 | applicationName, |
| 230 | snapshotName, |
| 231 | }); |
| 232 | if (observed === undefined) { |
| 233 | yield* retryWhileInUse( |
| 234 | analytics.createApplicationSnapshot({ |
| 235 | ApplicationName: applicationName, |
| 236 | SnapshotName: snapshotName, |
| 237 | }), |
| 238 | ); |
| 239 | yield* session.note(`Creating snapshot ${snapshotName}...`); |
| 240 | } |
| 241 | const ready = yield* waitForSnapshotReady({ |
| 242 | applicationName, |
| 243 | snapshotName, |
| 244 | }); |
| 245 | |
| 246 | yield* session.note(snapshotName); |
| 247 | return { |
| 248 | applicationName, |
| 249 | snapshotName, |
| 250 | snapshotStatus: ready.SnapshotStatus, |
| 251 | applicationVersionId: ready.ApplicationVersionId, |
| 252 | }; |
| 253 | }), |
| 254 | |
| 255 | delete: Effect.fn(function* ({ output }) { |
| 256 | // DeleteApplicationSnapshot requires the snapshot's creation |
| 257 | // timestamp — observe it fresh. A missing snapshot (or a missing |
| 258 | // application) means the delete already happened. |
| 259 | const details = yield* describeSnapshot({ |
| 260 | applicationName: output.applicationName, |
| 261 | snapshotName: output.snapshotName, |
| 262 | }); |
| 263 | if (!details?.SnapshotCreationTimestamp) { |
| 264 | return; |
| 265 | } |
| 266 | yield* retryWhileInUse( |
| 267 | analytics |
| 268 | .deleteApplicationSnapshot({ |
| 269 | ApplicationName: output.applicationName, |
| 270 | SnapshotName: output.snapshotName, |
| 271 | SnapshotCreationTimestamp: details.SnapshotCreationTimestamp, |
| 272 | }) |
| 273 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)), |
| 274 | ); |
| 275 | yield* waitForSnapshotDeleted({ |
| 276 | applicationName: output.applicationName, |
| 277 | snapshotName: output.snapshotName, |
| 278 | }); |
| 279 | }), |
| 280 | }); |
| 281 | }), |
| 282 | ); |