@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 261 lines 9.33 KB
Raw
1 import * as datasync from "@distilled.cloud/aws/datasync";
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 { createInternalTags } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11 import { readObservedTags, syncTags } from "./internal.ts";
12
13 export interface TaskProps {
14 /**
15 * ARN of the source location data is transferred from. Cannot be changed
16 * after creation (replacement).
17 */
18 sourceLocationArn: string;
19 /**
20 * ARN of the destination location data is transferred to. Cannot be
21 * changed after creation (replacement).
22 */
23 destinationLocationArn: string;
24 /**
25 * Name of the task. If omitted, a unique name is generated from the app,
26 * stage, and logical id.
27 */
28 name?: string;
29 /**
30 * Transfer options — verification mode, overwrite behaviour, metadata
31 * preservation, bandwidth throttle, logging, etc. Updatable in place.
32 */
33 options?: datasync.Options;
34 /**
35 * Filter rules excluding files/folders from the transfer. Updatable in
36 * place.
37 */
38 excludes?: datasync.FilterRule[];
39 /**
40 * Filter rules including only matching files/folders. Updatable in place.
41 */
42 includes?: datasync.FilterRule[];
43 /**
44 * Schedule on which the task runs automatically (cron expression).
45 * Updatable in place.
46 */
47 schedule?: datasync.TaskSchedule;
48 /**
49 * ARN of the CloudWatch log group DataSync publishes task logs to.
50 * Updatable in place.
51 */
52 cloudWatchLogGroupArn?: string;
53 /**
54 * Task execution mode. `BASIC` (default) or `ENHANCED` (higher scale,
55 * S3-only). Cannot be changed after creation (replacement).
56 */
57 taskMode?: datasync.TaskMode;
58 /**
59 * Tags to apply to the task. Merged with internal Alchemy tags.
60 */
61 tags?: Record<string, string>;
62 }
63
64 export interface Task extends Resource<
65 "AWS.DataSync.Task",
66 TaskProps,
67 {
68 /** ARN of the DataSync task. */
69 taskArn: string;
70 /** Current task status (e.g. `AVAILABLE`, `RUNNING`). */
71 taskStatus: datasync.TaskStatus;
72 /** ARN of the source location. */
73 sourceLocationArn: string;
74 /** ARN of the destination location. */
75 destinationLocationArn: string;
76 },
77 {},
78 Providers
79 > {}
80
81 /**
82 * A DataSync task — the transfer definition binding a source location to a
83 * destination location, together with the filters, schedule, and transfer
84 * options that govern each run.
85 *
86 * Creating the task does not move any data; it defines the transfer.
87 * Start a run with `StartTaskExecution` (or attach a `schedule`). Source and
88 * destination locations and the task mode are immutable; everything else
89 * (name, options, filters, schedule, log group, tags) is updated in place.
90 *
91 * ### Creating Tasks
92 * **Example:** S3 → S3 transfer
93 * ```typescript
94 * import * as AWS from "@alchemy.run/aws";
95 *
96 * const task = yield* AWS.DataSync.Task("Backup", {
97 * sourceLocationArn: source.locationArn,
98 * destinationLocationArn: dest.locationArn,
99 * });
100 * ```
101 *
102 * **Example:** With verification and a schedule
103 * ```typescript
104 * const task = yield* AWS.DataSync.Task("Nightly", {
105 * sourceLocationArn: source.locationArn,
106 * destinationLocationArn: dest.locationArn,
107 * options: { VerifyMode: "ONLY_FILES_TRANSFERRED" },
108 * schedule: { ScheduleExpression: "cron(0 2 * * ? *)" },
109 * });
110 * ```
111 *
112 * @resource
113 */
114 export const Task = Resource<Task>("AWS.DataSync.Task");
115
116 const canonical = (value: unknown): string => JSON.stringify(value ?? null);
117
118 export const TaskProvider = () =>
119 Provider.effect(
120 Task,
121 Effect.gen(function* () {
122 const createName = Effect.fn(function* (id: string, props: TaskProps) {
123 return props.name ?? (yield* createPhysicalName({ id, maxLength: 128 }));
124 });
125
126 const describe = Effect.fn(function* (taskArn: string) {
127 return yield* datasync
128 .describeTask({ TaskArn: taskArn })
129 .pipe(Effect.catchTag("TaskNotFound", () => Effect.undefined));
130 });
131
132 const findByName = Effect.fn(function* (name: string) {
133 const tasks = yield* datasync.listTasks.pages({}).pipe(
134 Stream.runCollect,
135 Effect.map((chunk) => Array.from(chunk).flatMap((p) => p.Tasks ?? [])),
136 );
137 return tasks.find((t) => t.Name === name)?.TaskArn;
138 });
139
140 const attrsOf = (t: datasync.DescribeTaskResponse) => ({
141 taskArn: t.TaskArn!,
142 taskStatus: t.Status ?? "AVAILABLE",
143 sourceLocationArn: t.SourceLocationArn!,
144 destinationLocationArn: t.DestinationLocationArn!,
145 });
146
147 return Task.Provider.of({
148 stables: ["taskArn", "sourceLocationArn", "destinationLocationArn"],
149
150 list: () =>
151 Effect.gen(function* () {
152 const tasks = yield* datasync.listTasks.pages({}).pipe(
153 Stream.runCollect,
154 Effect.map((chunk) => Array.from(chunk).flatMap((p) => p.Tasks ?? [])),
155 );
156 return yield* Effect.forEach(tasks, (t) =>
157 Effect.gen(function* () {
158 const full = yield* describe(t.TaskArn!);
159 return full ? attrsOf(full) : undefined;
160 }),
161 ).pipe(
162 Effect.map((items) =>
163 items.filter((i): i is ReturnType<typeof attrsOf> => i !== undefined),
164 ),
165 );
166 }),
167
168 read: Effect.fn(function* ({ id, olds, output }) {
169 const arn = output?.taskArn ?? (yield* findByName(yield* createName(id, olds)));
170 if (arn === undefined) return undefined;
171 const t = yield* describe(arn);
172 return t === undefined ? undefined : attrsOf(t);
173 }),
174
175 diff: Effect.fn(function* ({ news, olds }) {
176 if (!isResolved(news)) return undefined;
177 const replaced =
178 news.sourceLocationArn !== olds.sourceLocationArn ||
179 news.destinationLocationArn !== olds.destinationLocationArn ||
180 (news.taskMode ?? "BASIC") !== (olds.taskMode ?? "BASIC");
181 if (replaced) return { action: "replace" } as const;
182 }),
183
184 reconcile: Effect.fn(function* ({ id, news, output, session }) {
185 const internalTags = yield* createInternalTags(id);
186 const desiredTags = { ...news.tags, ...internalTags };
187 const name = yield* createName(id, news);
188
189 // 1. OBSERVE — output cache, else re-discover by deterministic name.
190 let arn = output?.taskArn ?? (yield* findByName(name));
191 let t = arn ? yield* describe(arn) : undefined;
192
193 // 2. ENSURE — create if missing.
194 if (t === undefined) {
195 const created = yield* datasync
196 .createTask({
197 SourceLocationArn: news.sourceLocationArn,
198 DestinationLocationArn: news.destinationLocationArn,
199 Name: name,
200 Options: news.options,
201 Excludes: news.excludes,
202 Includes: news.includes,
203 Schedule: news.schedule,
204 CloudWatchLogGroupArn: news.cloudWatchLogGroupArn,
205 TaskMode: news.taskMode,
206 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
207 Key,
208 Value,
209 })),
210 })
211 .pipe(
212 // createTask synchronously probes the locations' S3 access,
213 // and a freshly-created location role's IAM policy can lag
214 // (typed `LocationAccessTestFailed`). Retry bounded through
215 // the propagation window.
216 Effect.retry({
217 while: (e) => e._tag === "LocationAccessTestFailed",
218 schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(9)]),
219 }),
220 );
221 arn = created.TaskArn!;
222 t = yield* describe(arn);
223 } else {
224 // 3a. SYNC updatable fields — only on a real delta.
225 const changed =
226 t.Name !== name ||
227 (t.CloudWatchLogGroupArn ?? "") !== (news.cloudWatchLogGroupArn ?? "") ||
228 canonical(t.Options) !== canonical(news.options) ||
229 canonical(t.Excludes) !== canonical(news.excludes) ||
230 canonical(t.Includes) !== canonical(news.includes) ||
231 canonical(t.Schedule) !== canonical(news.schedule);
232 if (changed) {
233 yield* datasync.updateTask({
234 TaskArn: arn!,
235 Name: name,
236 Options: news.options,
237 Excludes: news.excludes,
238 Includes: news.includes,
239 Schedule: news.schedule,
240 CloudWatchLogGroupArn: news.cloudWatchLogGroupArn,
241 });
242 t = yield* describe(arn!);
243 }
244 }
245
246 // 3b. SYNC tags against observed cloud state.
247 const observed = yield* readObservedTags(arn!);
248 yield* syncTags(arn!, observed, desiredTags);
249
250 yield* session.note(arn!);
251 return attrsOf(t!);
252 }),
253
254 delete: Effect.fn(function* ({ output }) {
255 yield* datasync
256 .deleteTask({ TaskArn: output.taskArn })
257 .pipe(Effect.catchTag("TaskNotFound", () => Effect.void));
258 }),
259 });
260 }),
261 );