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

main
ts 180 lines 6.2 KB
Raw
1 import * as databrew from "@distilled.cloud/aws/databrew";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { AWSEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 cleanMap,
14 databrewArn,
15 fetchObservedTags,
16 retryWhileConflict,
17 syncTags,
18 } from "./internal.ts";
19
20 export interface ScheduleProps {
21 /**
22 * Name of the schedule. If omitted, a unique name is generated. Changing
23 * the name replaces the schedule.
24 * @default a generated physical name
25 */
26 scheduleName?: string;
27 /**
28 * The cron expression (in AWS cron format, evaluated in UTC) that
29 * determines when the associated jobs run, e.g.
30 * `cron(0 12 * * ? *)` for every day at noon UTC.
31 */
32 cronExpression: string;
33 /**
34 * Names of the DataBrew jobs the schedule starts.
35 */
36 jobNames?: string[];
37 /**
38 * Tags to apply to the schedule. Merged with internal Alchemy tags.
39 */
40 tags?: Record<string, string>;
41 }
42
43 export interface Schedule extends Resource<
44 "AWS.DataBrew.Schedule",
45 ScheduleProps,
46 {
47 /** Name of the schedule. */
48 scheduleName: string;
49 /** ARN of the schedule. */
50 scheduleArn: string;
51 },
52 {},
53 Providers
54 > {}
55
56 /**
57 * An AWS Glue DataBrew schedule — a cron expression that starts one or more
58 * DataBrew jobs at recurring times. The schedule definition is free; only
59 * the job runs it triggers are billed.
60 * ### Creating Schedules
61 * **Example:** Nightly Job Schedule
62 * ```typescript
63 * import * as AWS from "@alchemy.run/aws";
64 *
65 * const schedule = yield* AWS.DataBrew.Schedule("Nightly", {
66 * cronExpression: "cron(0 3 * * ? *)",
67 * jobNames: [job.jobName],
68 * });
69 * ```
70 *
71 * **Example:** Schedule Without Jobs (attach later)
72 * ```typescript
73 * const schedule = yield* AWS.DataBrew.Schedule("Standing", {
74 * cronExpression: "cron(0 12 ? * MON-FRI *)",
75 * });
76 * ```
77 *
78 * @resource
79 */
80 export const Schedule = Resource<Schedule>("AWS.DataBrew.Schedule");
81
82 export const ScheduleProvider = () =>
83 Provider.effect(
84 Schedule,
85 Effect.gen(function* () {
86 const createName = Effect.fn(function* (
87 id: string,
88 props: { scheduleName?: string | undefined },
89 ) {
90 return props.scheduleName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
91 });
92
93 const observe = Effect.fn(function* (name: string) {
94 return yield* databrew
95 .describeSchedule({ Name: name })
96 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
97 });
98
99 return Schedule.Provider.of({
100 stables: ["scheduleName", "scheduleArn"],
101
102 list: () =>
103 Effect.gen(function* () {
104 const { accountId, region } = yield* AWSEnvironment.current;
105 const pages = yield* databrew.listSchedules.pages({}).pipe(Stream.runCollect);
106 return Array.from(pages)
107 .flatMap((page) => page.Schedules ?? [])
108 .map((s) => ({
109 scheduleName: s.Name,
110 scheduleArn: s.ResourceArn ?? databrewArn(region, accountId, "schedule", s.Name),
111 }));
112 }),
113
114 read: Effect.fn(function* ({ id, olds, output }) {
115 const { accountId, region } = yield* AWSEnvironment.current;
116 const name = output?.scheduleName ?? (yield* createName(id, olds ?? {}));
117 const schedule = yield* observe(name);
118 if (schedule === undefined) return undefined;
119 const arn = schedule.ResourceArn ?? databrewArn(region, accountId, "schedule", name);
120 const attrs = { scheduleName: name, scheduleArn: arn };
121 const tags = cleanMap(schedule.Tags);
122 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
123 }),
124
125 diff: Effect.fn(function* ({ id, news, olds }) {
126 if (!isResolved(news)) return undefined;
127 const oldName = yield* createName(id, olds);
128 const newName = yield* createName(id, news);
129 if (oldName !== newName) return { action: "replace" } as const;
130 // cronExpression and jobNames are UpdateSchedule-able
131 }),
132
133 reconcile: Effect.fn(function* ({ id, news, output, session }) {
134 const { accountId, region } = yield* AWSEnvironment.current;
135 const name = output?.scheduleName ?? (yield* createName(id, news));
136 const internalTags = yield* createInternalTags(id);
137 const desiredTags = { ...news.tags, ...internalTags };
138
139 // 1. OBSERVE
140 const schedule = yield* observe(name);
141
142 // 2. ENSURE / 3. SYNC — UpdateSchedule is a full PUT
143 if (schedule === undefined) {
144 yield* databrew
145 .createSchedule({
146 Name: name,
147 CronExpression: news.cronExpression,
148 JobNames: news.jobNames,
149 Tags: desiredTags,
150 })
151 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
152 } else if (
153 schedule.CronExpression !== news.cronExpression ||
154 JSON.stringify(schedule.JobNames ?? []) !== JSON.stringify(news.jobNames ?? [])
155 ) {
156 yield* databrew.updateSchedule({
157 Name: name,
158 CronExpression: news.cronExpression,
159 JobNames: news.jobNames,
160 });
161 }
162
163 const arn = schedule?.ResourceArn ?? databrewArn(region, accountId, "schedule", name);
164
165 // 3b. SYNC TAGS against observed cloud tags
166 const observedTags = yield* fetchObservedTags(arn);
167 yield* syncTags(arn, observedTags, desiredTags);
168
169 yield* session.note(name);
170 return { scheduleName: name, scheduleArn: arn };
171 }),
172
173 delete: Effect.fn(function* ({ output }) {
174 yield* retryWhileConflict(databrew.deleteSchedule({ Name: output.scheduleName })).pipe(
175 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
176 );
177 }),
178 });
179 }),
180 );