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

main
ts 198 lines 7 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Job } from "@alchemy.run/aws/Glue";
3 import { Role } from "@alchemy.run/aws/IAM";
4 import { Bucket } from "@alchemy.run/aws/S3";
5 import { expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as glue from "@distilled.cloud/aws/glue";
8 import * as s3 from "@distilled.cloud/aws/s3";
9 import * as Output from "alchemy/Output";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12
13 const { test } = Test.make({ providers: AWS.providers() });
14
15 const getJob = (name: string) =>
16 glue.getJob({ JobName: name }).pipe(
17 Effect.map((r) => r.Job),
18 Effect.catchTag("EntityNotFoundException", () => Effect.succeed(undefined)),
19 );
20
21 const jobRole = () =>
22 Role("GlueJobRole", {
23 assumeRolePolicyDocument: {
24 Version: "2012-10-17",
25 Statement: [
26 {
27 Effect: "Allow",
28 Principal: { Service: "glue.amazonaws.com" },
29 Action: ["sts:AssumeRole"],
30 },
31 ],
32 },
33 managedPolicyArns: [
34 "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole",
35 "arn:aws:iam::aws:policy/AmazonS3FullAccess",
36 ],
37 });
38
39 test.provider(
40 "create, update, delete Glue job definition",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 const created = yield* stack.deploy(
46 Effect.gen(function* () {
47 const bucket = yield* Bucket("JobBucket", { forceDestroy: true });
48 const role = yield* jobRole();
49 const job = yield* Job("Etl", {
50 role: role.roleArn,
51 command: {
52 name: "pythonshell",
53 pythonVersion: "3.9",
54 scriptLocation: Output.interpolate`s3://${bucket.bucketName}/scripts/etl.py`,
55 },
56 maxCapacity: 0.0625,
57 glueVersion: "3.0",
58 defaultArguments: { "--job-language": "python" },
59 tags: { Environment: "test" },
60 });
61 return { bucket, role, job };
62 }),
63 );
64
65 expect(created.job.jobName).toBeDefined();
66 expect(created.job.jobArn).toContain(`:job/${created.job.jobName}`);
67
68 // out-of-band verification
69 const observed = yield* getJob(created.job.jobName);
70 expect(observed?.Name).toEqual(created.job.jobName);
71 expect(observed?.Command?.Name).toEqual("pythonshell");
72 expect(observed?.Command?.ScriptLocation).toEqual(
73 `s3://${created.bucket.bucketName}/scripts/etl.py`,
74 );
75 expect(observed?.GlueVersion).toEqual("3.0");
76 expect(observed?.DefaultArguments?.["--job-language"]).toEqual("python");
77
78 // tags (jobs ARE ARN-taggable)
79 const tags = yield* glue.getTags({ ResourceArn: created.job.jobArn });
80 expect(tags.Tags?.["alchemy::id"]).toBeDefined();
81 expect(tags.Tags?.Environment).toEqual("test");
82
83 // update: change default arguments + timeout
84 yield* stack.deploy(
85 Effect.gen(function* () {
86 const bucket = yield* Bucket("JobBucket", { forceDestroy: true });
87 const role = yield* jobRole();
88 const job = yield* Job("Etl", {
89 role: role.roleArn,
90 command: {
91 name: "pythonshell",
92 pythonVersion: "3.9",
93 scriptLocation: Output.interpolate`s3://${bucket.bucketName}/scripts/etl.py`,
94 },
95 maxCapacity: 0.0625,
96 glueVersion: "3.0",
97 description: "curated ETL",
98 defaultArguments: { "--job-language": "python", "--extra": "1" },
99 timeout: "30 minutes",
100 tags: { Environment: "test" },
101 });
102 return { job };
103 }),
104 );
105
106 const reobserved = yield* getJob(created.job.jobName);
107 expect(reobserved?.Description).toEqual("curated ETL");
108 expect(reobserved?.DefaultArguments?.["--extra"]).toEqual("1");
109 expect(reobserved?.Timeout).toEqual(30);
110
111 yield* stack.destroy();
112 const gone = yield* getJob(created.job.jobName);
113 expect(gone).toBeUndefined();
114 }),
115 {
116 tags: ["provider:aws", "provider:aws:glue", "provider:aws:iam", "provider:aws:s3", "live"],
117 },
118 );
119
120 // A live job run is billed (≈1 DPU-minute) and takes ~1-2 minutes. Under
121 // concurrent load startJobRun has failed right after create with
122 // `EntityNotFoundException: Failed to start job run due to missing metadata`
123 // (2026-10-09), so it stays skipped under `--fast`.
124 test.provider.skipIf(!!process.env.FAST)(
125 "run a live Python shell job to success",
126 (stack) =>
127 Effect.gen(function* () {
128 yield* stack.destroy();
129
130 const deployed = yield* stack.deploy(
131 Effect.gen(function* () {
132 const bucket = yield* Bucket("LiveJobBucket", { forceDestroy: true });
133 const role = yield* jobRole();
134 const job = yield* Job("LiveEtl", {
135 role: role.roleArn,
136 command: {
137 name: "pythonshell",
138 pythonVersion: "3.9",
139 scriptLocation: Output.interpolate`s3://${bucket.bucketName}/scripts/noop.py`,
140 },
141 maxCapacity: 0.0625,
142 glueVersion: "3.0",
143 });
144 return { bucket, job };
145 }),
146 );
147
148 // upload a trivial script the job will execute
149 yield* s3.putObject({
150 Bucket: deployed.bucket.bucketName,
151 Key: "scripts/noop.py",
152 Body: new TextEncoder().encode("print('hello from glue')\n"),
153 });
154
155 // Start a run and poll to a terminal state. A run started seconds after
156 // the IAM role is created can FAIL with "should be given assume role
157 // permissions for Glue Service" while the trust propagates — retry the
158 // whole start+await through that window (bounded).
159 const runOnce = Effect.gen(function* () {
160 const { JobRunId } = yield* glue.startJobRun({
161 JobName: deployed.job.jobName,
162 });
163 const finalRun = yield* glue
164 .getJobRun({ JobName: deployed.job.jobName, RunId: JobRunId! })
165 .pipe(
166 Effect.map((r) => r.JobRun),
167 Effect.repeat({
168 schedule: Schedule.spaced("15 seconds"),
169 until: (run) =>
170 run?.JobRunState === "SUCCEEDED" ||
171 run?.JobRunState === "FAILED" ||
172 run?.JobRunState === "TIMEOUT" ||
173 run?.JobRunState === "ERROR",
174 times: 20,
175 }),
176 );
177 yield* Effect.log(`job run state=${finalRun?.JobRunState} error=${finalRun?.ErrorMessage}`);
178 if (
179 finalRun?.JobRunState !== "SUCCEEDED" &&
180 finalRun?.ErrorMessage?.includes("assume role permissions")
181 ) {
182 return yield* Effect.fail("role-not-yet-assumable" as const);
183 }
184 return finalRun;
185 });
186
187 const finalRun = yield* runOnce.pipe(
188 Effect.retry({ schedule: Schedule.spaced("30 seconds"), times: 4 }),
189 );
190 expect(finalRun?.JobRunState).toEqual("SUCCEEDED");
191
192 yield* stack.destroy();
193 }),
194 {
195 tags: ["provider:aws", "provider:aws:glue", "provider:aws:iam", "provider:aws:s3", "live"],
196 timeout: 420_000,
197 },
198 );