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
| 1 | import * as batch from "@distilled.cloud/aws/batch"; |
| 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, diffTags } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { AWSEnvironment, type AccountID } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import type { RegionID } from "../Region.ts"; |
| 13 | import { pollBatch, retryBatch } from "./internal.ts"; |
| 14 | |
| 15 | export type JobQueueName = string; |
| 16 | export type JobQueueArn = `arn:aws:batch:${RegionID}:${AccountID}:job-queue/${JobQueueName}`; |
| 17 | |
| 18 | /** |
| 19 | * Raised when a job queue's asynchronous deletion does not complete within |
| 20 | * the provider's poll budget. Reporting success here would let the engine |
| 21 | * delete the queue's compute environments while the association still exists, |
| 22 | * wedging the whole chain. |
| 23 | */ |
| 24 | export class JobQueueDeleteTimeoutError extends Data.TaggedError("JobQueueDeleteTimeoutError")<{ |
| 25 | readonly jobQueueName: string; |
| 26 | readonly status: string | undefined; |
| 27 | readonly statusReason: string | undefined; |
| 28 | readonly message: string; |
| 29 | }> {} |
| 30 | |
| 31 | export interface JobQueueProps { |
| 32 | /** |
| 33 | * Name of the job queue. If omitted, a unique name is generated. |
| 34 | * Up to 128 characters (letters, numbers, hyphens, underscores). |
| 35 | */ |
| 36 | jobQueueName?: string; |
| 37 | /** |
| 38 | * Compute environment ARNs the queue schedules onto, in preference order |
| 39 | * (index 0 is tried first). |
| 40 | */ |
| 41 | computeEnvironments: string[]; |
| 42 | /** |
| 43 | * Queue priority — queues with a higher value are evaluated first when |
| 44 | * they share compute environments. |
| 45 | * @default 1 |
| 46 | */ |
| 47 | priority?: number; |
| 48 | /** |
| 49 | * Whether the queue accepts new job submissions. |
| 50 | * @default "ENABLED" |
| 51 | */ |
| 52 | state?: "ENABLED" | "DISABLED"; |
| 53 | /** |
| 54 | * User-defined tags to apply to the job queue. |
| 55 | */ |
| 56 | tags?: Record<string, string>; |
| 57 | } |
| 58 | |
| 59 | export interface JobQueue extends Resource< |
| 60 | "AWS.Batch.JobQueue", |
| 61 | JobQueueProps, |
| 62 | { |
| 63 | jobQueueName: JobQueueName; |
| 64 | jobQueueArn: JobQueueArn; |
| 65 | state: "ENABLED" | "DISABLED"; |
| 66 | status: string; |
| 67 | priority: number; |
| 68 | computeEnvironments: string[]; |
| 69 | tags: Record<string, string>; |
| 70 | }, |
| 71 | never, |
| 72 | Providers |
| 73 | > {} |
| 74 | |
| 75 | /** |
| 76 | * An AWS Batch job queue. Jobs submitted to the queue are scheduled onto its |
| 77 | * associated compute environments in preference order. |
| 78 | * |
| 79 | * ### Creating Job Queues |
| 80 | * **Example:** Queue on a Fargate Compute Environment |
| 81 | * ```typescript |
| 82 | * const ce = yield* Batch.ComputeEnvironment("JobsCE", {}); |
| 83 | * const queue = yield* Batch.JobQueue("JobsQueue", { |
| 84 | * computeEnvironments: [ce.computeEnvironmentArn], |
| 85 | * }); |
| 86 | * ``` |
| 87 | * |
| 88 | * **Example:** Prioritized queue |
| 89 | * ```typescript |
| 90 | * const critical = yield* Batch.JobQueue("CriticalQueue", { |
| 91 | * priority: 10, |
| 92 | * computeEnvironments: [ce.computeEnvironmentArn], |
| 93 | * }); |
| 94 | * ``` |
| 95 | * |
| 96 | * @resource |
| 97 | */ |
| 98 | export const JobQueue = Resource<JobQueue>("AWS.Batch.JobQueue"); |
| 99 | |
| 100 | const toAttributes = (q: batch.JobQueueDetail, tags: Record<string, string>) => ({ |
| 101 | jobQueueName: q.jobQueueName!, |
| 102 | jobQueueArn: q.jobQueueArn as JobQueueArn, |
| 103 | state: (q.state ?? "ENABLED") as "ENABLED" | "DISABLED", |
| 104 | status: q.status ?? "VALID", |
| 105 | priority: q.priority ?? 1, |
| 106 | computeEnvironments: [...(q.computeEnvironmentOrder ?? [])] |
| 107 | .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) |
| 108 | .flatMap((o) => (o.computeEnvironment ? [o.computeEnvironment] : [])), |
| 109 | tags, |
| 110 | }); |
| 111 | |
| 112 | const observedTagsOf = (q: { tags?: { [key: string]: string | undefined } }) => |
| 113 | Object.fromEntries( |
| 114 | Object.entries(q.tags ?? {}).filter((e): e is [string, string] => typeof e[1] === "string"), |
| 115 | ); |
| 116 | |
| 117 | const toOrder = (computeEnvironments: string[]): batch.ComputeEnvironmentOrder[] => |
| 118 | computeEnvironments.map((computeEnvironment, index) => ({ |
| 119 | order: index + 1, |
| 120 | computeEnvironment, |
| 121 | })); |
| 122 | |
| 123 | export const JobQueueProvider = () => |
| 124 | Provider.effect( |
| 125 | JobQueue, |
| 126 | Effect.gen(function* () { |
| 127 | const toName = (id: string, props: { jobQueueName?: string } = {}) => |
| 128 | props.jobQueueName |
| 129 | ? Effect.succeed(props.jobQueueName) |
| 130 | : createPhysicalName({ id, maxLength: 128 }); |
| 131 | |
| 132 | // A deleted queue lingers as a DELETED record for a while and the name |
| 133 | // is immediately reusable, so a describe-by-name can briefly return |
| 134 | // both the tombstone and the live queue — prefer the live one. |
| 135 | const describeOne = (name: string) => |
| 136 | batch |
| 137 | .describeJobQueues({ jobQueues: [name] }) |
| 138 | .pipe( |
| 139 | Effect.map( |
| 140 | (res) => res.jobQueues?.find((q) => q.status !== "DELETED") ?? res.jobQueues?.[0], |
| 141 | ), |
| 142 | ); |
| 143 | |
| 144 | /** Wait until any CREATING/UPDATING/DELETING transition settles. */ |
| 145 | const awaitSettled = (name: string) => |
| 146 | pollBatch( |
| 147 | describeOne(name), |
| 148 | (q) => |
| 149 | q === undefined || |
| 150 | (q.status !== "CREATING" && q.status !== "UPDATING" && q.status !== "DELETING"), |
| 151 | ); |
| 152 | |
| 153 | const awaitDisabled = (name: string) => |
| 154 | pollBatch( |
| 155 | describeOne(name), |
| 156 | (queue) => |
| 157 | queue === undefined || |
| 158 | queue.status === "DELETED" || |
| 159 | queue.status === "DELETING" || |
| 160 | (queue.state === "DISABLED" && |
| 161 | queue.status !== "CREATING" && |
| 162 | queue.status !== "UPDATING"), |
| 163 | ); |
| 164 | |
| 165 | return { |
| 166 | stables: ["jobQueueName", "jobQueueArn"], |
| 167 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 168 | if (!isResolved(news)) return; |
| 169 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 170 | return { action: "replace" } as const; |
| 171 | } |
| 172 | }), |
| 173 | read: Effect.fn(function* ({ id, olds, output }) { |
| 174 | const name = output?.jobQueueName ?? (yield* toName(id, olds ?? {})); |
| 175 | const q = yield* describeOne(name); |
| 176 | if (!q?.jobQueueArn || q.status === "DELETED") { |
| 177 | return undefined; |
| 178 | } |
| 179 | return toAttributes(q, observedTagsOf(q)); |
| 180 | }), |
| 181 | list: () => |
| 182 | Effect.gen(function* () { |
| 183 | const pages = yield* batch.describeJobQueues.pages({}).pipe(Stream.runCollect); |
| 184 | return Array.from(pages) |
| 185 | .flatMap((page) => page.jobQueues ?? []) |
| 186 | .flatMap((q) => |
| 187 | q.jobQueueArn && q.status !== "DELETED" ? [toAttributes(q, observedTagsOf(q))] : [], |
| 188 | ); |
| 189 | }), |
| 190 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 191 | const { accountId, region } = yield* AWSEnvironment.current; |
| 192 | const name = output?.jobQueueName ?? (yield* toName(id, news)); |
| 193 | const arn = `arn:aws:batch:${region}:${accountId}:job-queue/${name}` as JobQueueArn; |
| 194 | const internalTags = yield* createInternalTags(id); |
| 195 | const desiredTags = { ...internalTags, ...news.tags }; |
| 196 | const desiredState = news.state ?? "ENABLED"; |
| 197 | const desiredPriority = news.priority ?? 1; |
| 198 | const desiredOrder = toOrder(news.computeEnvironments); |
| 199 | |
| 200 | // Observe — cloud state is authoritative. |
| 201 | let queue = yield* describeOne(name); |
| 202 | if (queue?.status === "DELETED") queue = undefined; |
| 203 | |
| 204 | // Ensure — create if missing. The referenced compute environment may |
| 205 | // still be settling to VALID (its own reconcile waits, but adoption |
| 206 | // or manual drift can race) — retry through that window, and treat |
| 207 | // a concurrent create as a race to observe. |
| 208 | const create = retryBatch( |
| 209 | batch.createJobQueue({ |
| 210 | jobQueueName: name, |
| 211 | state: desiredState, |
| 212 | priority: desiredPriority, |
| 213 | computeEnvironmentOrder: desiredOrder, |
| 214 | tags: desiredTags, |
| 215 | }), |
| 216 | (e) => e._tag === "ComputeEnvironmentNotValid", |
| 217 | ).pipe(Effect.catchTag("JobQueueAlreadyExists", () => Effect.void)); |
| 218 | |
| 219 | if (queue?.jobQueueArn && queue.status === "DELETING") { |
| 220 | // An interrupted destroy left the queue mid-deletion — let it |
| 221 | // finish, then fall through to a fresh create below. |
| 222 | queue = yield* awaitSettled(name); |
| 223 | if (queue?.status === "DELETED") queue = undefined; |
| 224 | } |
| 225 | if (!queue?.jobQueueArn) { |
| 226 | yield* create; |
| 227 | queue = yield* awaitSettled(name); |
| 228 | } else if (queue.status !== "VALID" && queue.status !== "INVALID") { |
| 229 | queue = yield* awaitSettled(name); |
| 230 | } |
| 231 | if (queue?.status === "DELETED") { |
| 232 | // The settle above can still land on a tombstone (deletion that |
| 233 | // completed between observe and settle) — one more create pass. |
| 234 | yield* create; |
| 235 | queue = yield* awaitSettled(name); |
| 236 | } |
| 237 | |
| 238 | if (!queue?.jobQueueArn) { |
| 239 | return yield* Effect.die(new Error(`JobQueue ${name} did not settle`)); |
| 240 | } |
| 241 | |
| 242 | // Sync — diff observed against desired, apply only the delta. |
| 243 | const observedOrder = [...(queue.computeEnvironmentOrder ?? [])] |
| 244 | .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) |
| 245 | .map((o) => o.computeEnvironment) |
| 246 | .join(","); |
| 247 | const update: batch.UpdateJobQueueRequest = { jobQueue: name }; |
| 248 | let dirty = false; |
| 249 | if ((queue.state ?? "ENABLED") !== desiredState) { |
| 250 | update.state = desiredState; |
| 251 | dirty = true; |
| 252 | } |
| 253 | if ((queue.priority ?? 1) !== desiredPriority) { |
| 254 | update.priority = desiredPriority; |
| 255 | dirty = true; |
| 256 | } |
| 257 | if (observedOrder !== news.computeEnvironments.join(",")) { |
| 258 | update.computeEnvironmentOrder = desiredOrder; |
| 259 | dirty = true; |
| 260 | } |
| 261 | if (dirty) { |
| 262 | yield* retryBatch( |
| 263 | batch.updateJobQueue(update), |
| 264 | (e) => e._tag === "JobQueueBeingModified", |
| 265 | ); |
| 266 | queue = (yield* awaitSettled(name)) ?? queue; |
| 267 | } |
| 268 | |
| 269 | // Sync tags — diff against OBSERVED cloud tags. |
| 270 | const { upsert, removed } = diffTags(observedTagsOf(queue), desiredTags); |
| 271 | if (upsert.length > 0) { |
| 272 | yield* batch.tagResource({ |
| 273 | resourceArn: arn, |
| 274 | tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 275 | }); |
| 276 | } |
| 277 | if (removed.length > 0) { |
| 278 | yield* batch.untagResource({ resourceArn: arn, tagKeys: removed }); |
| 279 | } |
| 280 | |
| 281 | yield* session.note(arn); |
| 282 | return toAttributes({ ...queue, jobQueueName: name, jobQueueArn: arn }, desiredTags); |
| 283 | }), |
| 284 | delete: Effect.fn(function* ({ output }) { |
| 285 | const name = output.jobQueueName; |
| 286 | const existing = yield* describeOne(name); |
| 287 | if (!existing || existing.status === "DELETED") return; |
| 288 | |
| 289 | // Must be DISABLED before deletion. |
| 290 | if ((existing.state ?? "ENABLED") !== "DISABLED" && existing.status !== "DELETING") { |
| 291 | yield* retryBatch( |
| 292 | batch.updateJobQueue({ jobQueue: name, state: "DISABLED" }), |
| 293 | (e) => e._tag === "JobQueueBeingModified", |
| 294 | ).pipe(Effect.catchTag("JobQueueNotFound", () => Effect.void)); |
| 295 | } |
| 296 | const disabled = yield* awaitDisabled(name); |
| 297 | if (!disabled || disabled.status === "DELETED") return; |
| 298 | |
| 299 | // A resumed destroy can observe DELETING. Do not issue a redundant |
| 300 | // delete; just wait for the relationship to disappear. This keeps |
| 301 | // the downstream compute-environment delete strictly ordered. |
| 302 | if (disabled.status !== "DELETING") { |
| 303 | yield* retryBatch( |
| 304 | batch.deleteJobQueue({ jobQueue: name }), |
| 305 | (e) => e._tag === "JobQueueBeingModified", |
| 306 | ).pipe(Effect.catchTag("JobQueueNotFound", () => Effect.void)); |
| 307 | } |
| 308 | |
| 309 | // Wait until fully gone so downstream compute-environment deletion |
| 310 | // doesn't trip over the lingering association (queue deletion is |
| 311 | // asynchronous and takes up to ~1-2 minutes). An expired poll MUST |
| 312 | // NOT report success — the engine would then delete the queue's |
| 313 | // compute environments mid-teardown and wedge the chain. |
| 314 | const final = yield* pollBatch( |
| 315 | describeOne(name), |
| 316 | (q) => q === undefined || q.status === "DELETED", |
| 317 | ); |
| 318 | if (final && final.status !== "DELETED") { |
| 319 | return yield* new JobQueueDeleteTimeoutError({ |
| 320 | jobQueueName: name, |
| 321 | status: final.status, |
| 322 | statusReason: final.statusReason, |
| 323 | message: `Job queue ${name} was still ${final.status ?? "present"} after the delete poll budget${final.statusReason ? `: ${final.statusReason}` : ""}`, |
| 324 | }); |
| 325 | } |
| 326 | }), |
| 327 | }; |
| 328 | }), |
| 329 | ); |