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

main
ts 342 lines 13.1 KB
Raw
1 import * as Glue from "@alchemy.run/aws/Glue";
2 import { Role } from "@alchemy.run/aws/IAM";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import { Bucket } from "@alchemy.run/aws/S3";
5 import * as Output from "alchemy/Output";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import { HttpServerRequest } from "effect/http/HttpServerRequest";
9 import * as HttpServerResponse from "effect/http/HttpServerResponse";
10 import * as Layer from "effect/Layer";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import path from "pathe";
14
15 const main = path.resolve(import.meta.dirname, "handler.ts");
16
17 export class GlueTestFunction extends Lambda.Function<Lambda.Function>()("GlueTestFunction") {}
18
19 export default GlueTestFunction.make(
20 {
21 main,
22 functionUrl: true,
23 timeout: Duration.seconds(90),
24 },
25 Effect.gen(function* () {
26 const bucket = yield* Bucket("GlueBindingsBucket", { forceDestroy: true });
27
28 const role = yield* Role("GlueBindingsRole", {
29 assumeRolePolicyDocument: {
30 Version: "2012-10-17",
31 Statement: [
32 {
33 Effect: "Allow",
34 Principal: { Service: "glue.amazonaws.com" },
35 Action: ["sts:AssumeRole"],
36 },
37 ],
38 },
39 managedPolicyArns: [
40 "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole",
41 "arn:aws:iam::aws:policy/AmazonS3FullAccess",
42 ],
43 });
44
45 const database = yield* Glue.Database("BindingsDatabase", {});
46
47 const table = yield* Glue.Table("BindingsEvents", {
48 databaseName: database.databaseName,
49 storageDescriptor: {
50 location: Output.interpolate`s3://${bucket.bucketName}/events/`,
51 columns: [
52 { name: "id", type: "string" },
53 { name: "amount", type: "double" },
54 ],
55 },
56 partitionKeys: [{ name: "dt", type: "string" }],
57 parameters: { classification: "json" },
58 });
59
60 const job = yield* Glue.Job("BindingsJob", {
61 role: role.roleArn,
62 command: {
63 name: "pythonshell",
64 pythonVersion: "3.9",
65 scriptLocation: Output.interpolate`s3://${bucket.bucketName}/scripts/noop.py`,
66 },
67 maxCapacity: 0.0625,
68 glueVersion: "3.0",
69 timeout: "10 minutes",
70 });
71
72 const crawler = yield* Glue.Crawler("BindingsCrawler", {
73 role: role.roleArn,
74 databaseName: database.databaseName,
75 targets: {
76 s3Targets: [{ path: Output.interpolate`s3://${bucket.bucketName}/crawl/` }],
77 },
78 });
79
80 // Event sources: the deploys prove the EventBridge rule + invoke
81 // permission wiring for both Glue detail types.
82 yield* Glue.consumeJobEvents({ states: ["FAILED", "TIMEOUT"] }, (events) =>
83 Stream.runForEach(events, (event) => Effect.log(`glue job event: ${event.detail.jobName}`)),
84 );
85 yield* Glue.consumeCrawlerEvents({ states: ["Succeeded"] }, (events) =>
86 Stream.runForEach(events, (event) =>
87 Effect.log(`glue crawler event: ${event.detail.crawlerName}`),
88 ),
89 );
90
91 // Job-scoped bindings.
92 const startJobRun = yield* Glue.StartJobRun(job);
93 const getJobRun = yield* Glue.GetJobRun(job);
94 const getJobRuns = yield* Glue.GetJobRuns(job);
95 const batchStopJobRun = yield* Glue.BatchStopJobRun(job);
96 const getJobBookmark = yield* Glue.GetJobBookmark(job);
97 const resetJobBookmark = yield* Glue.ResetJobBookmark(job);
98
99 // Crawler-scoped bindings.
100 const startCrawler = yield* Glue.StartCrawler(crawler);
101 const stopCrawler = yield* Glue.StopCrawler(crawler);
102 const getCrawler = yield* Glue.GetCrawler(crawler);
103
104 // Catalog bindings.
105 const getTables = yield* Glue.GetTables(database);
106 const getTable = yield* Glue.GetTable(table);
107 const getPartitions = yield* Glue.GetPartitions(table);
108 const getPartition = yield* Glue.GetPartition(table);
109 const createPartition = yield* Glue.CreatePartition(table);
110 const batchCreatePartition = yield* Glue.BatchCreatePartition(table);
111 const batchGetPartition = yield* Glue.BatchGetPartition(table);
112 const updatePartition = yield* Glue.UpdatePartition(table);
113 const batchUpdatePartition = yield* Glue.BatchUpdatePartition(table);
114 const deletePartition = yield* Glue.DeletePartition(table);
115 const batchDeletePartition = yield* Glue.BatchDeletePartition(table);
116
117 const bound = {
118 startJobRun,
119 getJobRun,
120 getJobRuns,
121 batchStopJobRun,
122 getJobBookmark,
123 resetJobBookmark,
124 startCrawler,
125 stopCrawler,
126 getCrawler,
127 getTables,
128 getTable,
129 getPartitions,
130 getPartition,
131 createPartition,
132 batchCreatePartition,
133 batchGetPartition,
134 updatePartition,
135 batchUpdatePartition,
136 deletePartition,
137 batchDeletePartition,
138 };
139
140 return {
141 fetch: Effect.gen(function* () {
142 const request = yield* HttpServerRequest;
143 const url = new URL(request.originalUrl);
144 const pathname = url.pathname;
145
146 if (request.method === "GET" && pathname === "/bindings") {
147 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
148 }
149
150 // List the job's runs (empty on a fresh job) — proves GetJobRuns.
151 if (request.method === "GET" && pathname === "/job-runs") {
152 const { JobRuns } = yield* getJobRuns({ MaxResults: 10 });
153 return yield* HttpServerResponse.json({
154 count: (JobRuns ?? []).length,
155 });
156 }
157
158 // Bookmarks: a job that has never run with bookmarks enabled has no
159 // entry — both operations surface the typed EntityNotFoundException.
160 if (request.method === "GET" && pathname === "/job-bookmark") {
161 const bookmark = yield* getJobBookmark().pipe(
162 Effect.map((r) => r.JobBookmarkEntry ?? "none"),
163 Effect.catchTag("EntityNotFoundException", () => Effect.succeed("none" as const)),
164 );
165 const reset = yield* resetJobBookmark().pipe(
166 Effect.map(() => "reset" as const),
167 Effect.catchTag("EntityNotFoundException", () => Effect.succeed("none" as const)),
168 );
169 return yield* HttpServerResponse.json({ bookmark, reset });
170 }
171
172 // Start a real (cheap pythonshell) run, read it back, then stop it —
173 // proves StartJobRun + GetJobRun + BatchStopJobRun end to end.
174 if (request.method === "POST" && pathname === "/job-run") {
175 const { JobRunId } = yield* startJobRun({}).pipe(
176 Effect.retry({
177 while: (e): boolean => e._tag === "GlueRoleNotAssumable",
178 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(6)]),
179 }),
180 );
181 const { JobRun } = yield* getJobRun({ RunId: JobRunId! });
182 const stopped = yield* batchStopJobRun({ JobRunIds: [JobRunId!] });
183 return yield* HttpServerResponse.json({
184 runId: JobRunId,
185 state: JobRun?.JobRunState,
186 stopSubmitted: (stopped.SuccessfulSubmissions ?? []).length,
187 stopErrors: (stopped.Errors ?? []).length,
188 });
189 }
190
191 // GetCrawler reads the crawler's live state — proves the op + grant.
192 if (request.method === "GET" && pathname === "/crawler") {
193 const { Crawler } = yield* getCrawler();
194 return yield* HttpServerResponse.json({
195 name: Crawler?.Name,
196 state: Crawler?.State,
197 });
198 }
199
200 // StopCrawler on an idle crawler surfaces the typed
201 // CrawlerNotRunningException — proves the op + IAM grant cheaply.
202 if (request.method === "POST" && pathname === "/crawler-stop") {
203 const result = yield* stopCrawler().pipe(
204 Effect.map(() => "stopped" as const),
205 Effect.catchTag("CrawlerNotRunningException", () =>
206 Effect.succeed("not-running" as const),
207 ),
208 Effect.catchTag("CrawlerStoppingException", () => Effect.succeed("stopping" as const)),
209 );
210 return yield* HttpServerResponse.json({ result });
211 }
212
213 // Start a crawl (empty S3 prefix) and immediately cancel it —
214 // proves StartCrawler + StopCrawler live.
215 if (request.method === "POST" && pathname === "/crawler-run") {
216 const started = yield* startCrawler().pipe(
217 Effect.map(() => "started" as const),
218 Effect.catchTag("CrawlerRunningException", () =>
219 Effect.succeed("already-running" as const),
220 ),
221 );
222 const stopped = yield* stopCrawler().pipe(
223 Effect.map(() => "stopped" as const),
224 Effect.catchTag("CrawlerNotRunningException", () =>
225 Effect.succeed("not-running" as const),
226 ),
227 Effect.catchTag("CrawlerStoppingException", () => Effect.succeed("stopping" as const)),
228 );
229 return yield* HttpServerResponse.json({ started, stopped });
230 }
231
232 // Catalog reads — proves GetTable + GetTables.
233 if (request.method === "GET" && pathname === "/table") {
234 const { Table } = yield* getTable();
235 const { TableList } = yield* getTables();
236 return yield* HttpServerResponse.json({
237 name: Table?.Name,
238 columns: (Table?.StorageDescriptor?.Columns ?? []).map((c) => c.Name),
239 partitionKeys: (Table?.PartitionKeys ?? []).map((k) => k.Name),
240 tables: (TableList ?? []).map((t) => t.Name),
241 });
242 }
243
244 // Full partition lifecycle — proves CreatePartition, GetPartition,
245 // UpdatePartition, BatchCreatePartition, GetPartitions,
246 // DeletePartition.
247 if (request.method === "POST" && pathname === "/partitions") {
248 yield* createPartition({
249 PartitionInput: { Values: ["2026-01-01"] },
250 }).pipe(Effect.catchTag("AlreadyExistsException", () => Effect.succeed({})));
251 const created = yield* getPartition({
252 PartitionValues: ["2026-01-01"],
253 });
254 yield* updatePartition({
255 PartitionValueList: ["2026-01-01"],
256 PartitionInput: {
257 Values: ["2026-01-01"],
258 Parameters: { updated: "true" },
259 },
260 });
261 const updated = yield* getPartition({
262 PartitionValues: ["2026-01-01"],
263 });
264 const batch = yield* batchCreatePartition({
265 PartitionInputList: [{ Values: ["2026-01-02"] }, { Values: ["2026-01-03"] }],
266 });
267 // Bulk update the batch-created partitions' parameters.
268 const batchUpdated = yield* batchUpdatePartition({
269 Entries: ["2026-01-02", "2026-01-03"].map((dt) => ({
270 PartitionValueList: [dt],
271 PartitionInput: {
272 Values: [dt],
273 Parameters: { bulkUpdated: "true" },
274 },
275 })),
276 });
277 // Bulk read all three back (one miss to prove UnprocessedKeys).
278 const bulk = yield* batchGetPartition({
279 PartitionsToGet: [
280 { Values: ["2026-01-01"] },
281 { Values: ["2026-01-02"] },
282 { Values: ["2026-01-03"] },
283 ],
284 });
285 const all = yield* getPartitions();
286 // Bulk delete two, single-delete the third.
287 const batchDeleted = yield* batchDeletePartition({
288 PartitionsToDelete: [{ Values: ["2026-01-02"] }, { Values: ["2026-01-03"] }],
289 });
290 yield* deletePartition({ PartitionValues: ["2026-01-01"] }).pipe(
291 Effect.catchTag("EntityNotFoundException", () => Effect.void),
292 );
293 const after = yield* getPartitions();
294 return yield* HttpServerResponse.json({
295 created: created.Partition?.Values,
296 updatedParam: updated.Partition?.Parameters?.updated,
297 batchErrors: (batch.Errors ?? []).length,
298 batchUpdateErrors: (batchUpdated.Errors ?? []).length,
299 bulkRead: (bulk.Partitions ?? []).length,
300 bulkUpdatedParams: (bulk.Partitions ?? []).filter(
301 (p) => p.Parameters?.bulkUpdated === "true",
302 ).length,
303 count: (all.Partitions ?? []).length,
304 batchDeleteErrors: (batchDeleted.Errors ?? []).length,
305 remaining: (after.Partitions ?? []).length,
306 });
307 }
308
309 return yield* HttpServerResponse.json(
310 { error: "Not found", method: request.method, pathname },
311 { status: 404 },
312 );
313 }).pipe(Effect.orDie),
314 };
315 }).pipe(
316 Effect.provide(
317 Layer.mergeAll(
318 Lambda.EventSource,
319 Glue.StartJobRunHttp,
320 Glue.GetJobRunHttp,
321 Glue.GetJobRunsHttp,
322 Glue.BatchStopJobRunHttp,
323 Glue.GetJobBookmarkHttp,
324 Glue.ResetJobBookmarkHttp,
325 Glue.StartCrawlerHttp,
326 Glue.StopCrawlerHttp,
327 Glue.GetCrawlerHttp,
328 Glue.GetTablesHttp,
329 Glue.GetTableHttp,
330 Glue.GetPartitionsHttp,
331 Glue.GetPartitionHttp,
332 Glue.CreatePartitionHttp,
333 Glue.BatchCreatePartitionHttp,
334 Glue.BatchGetPartitionHttp,
335 Glue.UpdatePartitionHttp,
336 Glue.BatchUpdatePartitionHttp,
337 Glue.DeletePartitionHttp,
338 Glue.BatchDeletePartitionHttp,
339 ),
340 ),
341 ),
342 );