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

main
ts 50 lines 2.12 KB
Raw
1 import * as logs from "@distilled.cloud/aws/cloudwatch-logs";
2 import * as Effect from "effect/Effect";
3 import * as Schedule from "effect/Schedule";
4 import * as Stream from "effect/Stream";
5
6 /**
7 * Delete every CloudWatch log group whose name starts with `prefix`.
8 *
9 * For resources whose SERVICE (not alchemy) creates per-resource log groups
10 * on their behalf — RDS log exports (`/aws/rds/instance/<id>/<type>`),
11 * CloudHSM audit logs (`/aws/cloudhsm/<clusterId>`) — and leaves them behind
12 * when the resource is deleted. The owning provider calls this after the
13 * resource is gone so the groups don't leak (cf. Lambda's `/aws/lambda/<fn>`
14 * reap in `Lambda/Function.ts`).
15 *
16 * Callers must pass a prefix that is unique to the resource (end it with `/`
17 * when the id is a path segment) so a sibling's groups are never matched.
18 *
19 * Best-effort and bounded: log cleanup is auxiliary to the already-completed
20 * resource delete, so a missing group is success and any other failure or a
21 * slow API only logs a warning.
22 *
23 * Internal scaffolding — NOT exported from the Logs service `index.ts`.
24 */
25 export const reapLogGroupsByPrefix = (prefix: string) =>
26 logs.describeLogGroups.pages({ logGroupNamePrefix: prefix }).pipe(
27 Stream.flatMap((page) => Stream.fromIterable(page.logGroups ?? [])),
28 Stream.mapEffect(
29 (group) =>
30 group.logGroupName === undefined
31 ? Effect.void
32 : logs.deleteLogGroup({ logGroupName: group.logGroupName }).pipe(
33 Effect.retry({
34 while: (e) =>
35 e._tag === "OperationAbortedException" ||
36 e._tag === "ServiceUnavailableException",
37 schedule: Schedule.exponential("250 millis"),
38 times: 6,
39 }),
40 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
41 ),
42 { concurrency: 4 },
43 ),
44 Stream.runDrain,
45 Effect.timeoutOrElse({
46 duration: "30 seconds",
47 orElse: () => Effect.logWarning(`Timed out reaping log groups ${prefix}*`),
48 }),
49 Effect.catch((e) => Effect.logWarning(`Failed to reap log groups ${prefix}*: ${e._tag}`)),
50 );