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 deadline from "@distilled.cloud/aws/deadline"; |
| 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, type Tags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as EffectStream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | asPlain, |
| 15 | deadlineArnOf, |
| 16 | fetchDeadlineTags, |
| 17 | reapDeadlineLogGroups, |
| 18 | retryWhileConflict, |
| 19 | retryWhileFarmSettling, |
| 20 | syncDeadlineTags, |
| 21 | } from "./internal.ts"; |
| 22 | |
| 23 | export class QueueInvalidProps extends Data.TaggedError("QueueInvalidProps")<{ |
| 24 | readonly message: string; |
| 25 | readonly cause?: unknown; |
| 26 | }> {} |
| 27 | |
| 28 | export class QueueUnexpectedResponse extends Data.TaggedError("QueueUnexpectedResponse")<{ |
| 29 | readonly message: string; |
| 30 | readonly cause?: unknown; |
| 31 | }> {} |
| 32 | |
| 33 | export type QueueStatus = deadline.QueueStatus; |
| 34 | export type DefaultQueueBudgetAction = deadline.DefaultQueueBudgetAction; |
| 35 | |
| 36 | export interface QueueProps { |
| 37 | /** |
| 38 | * The identifier of the farm the queue belongs to. Changing it replaces |
| 39 | * the queue. |
| 40 | */ |
| 41 | farmId: string; |
| 42 | /** |
| 43 | * Display name of the queue. |
| 44 | * @default ${app}-${stage}-${id} |
| 45 | */ |
| 46 | displayName?: string; |
| 47 | /** |
| 48 | * A description of the queue. |
| 49 | */ |
| 50 | description?: string; |
| 51 | /** |
| 52 | * Action taken on jobs in the queue when its associated budget is |
| 53 | * exhausted (`NONE`, `STOP_SCHEDULING_AND_COMPLETE_TASKS`, |
| 54 | * `STOP_SCHEDULING_AND_CANCEL_TASKS`). |
| 55 | * @default "NONE" |
| 56 | */ |
| 57 | defaultBudgetAction?: DefaultQueueBudgetAction; |
| 58 | /** |
| 59 | * S3 bucket and root prefix used for job attachments. |
| 60 | */ |
| 61 | jobAttachmentSettings?: deadline.JobAttachmentSettings; |
| 62 | /** |
| 63 | * ARN of the IAM role workers assume while running this queue's jobs. |
| 64 | */ |
| 65 | roleArn?: string; |
| 66 | /** |
| 67 | * The OS user/group jobs run as on workers. |
| 68 | */ |
| 69 | jobRunAsUser?: deadline.JobRunAsUser; |
| 70 | /** |
| 71 | * Names of file system locations (from storage profiles) that must be |
| 72 | * present on a worker for it to pick up this queue's jobs. |
| 73 | */ |
| 74 | requiredFileSystemLocationNames?: string[]; |
| 75 | /** |
| 76 | * Storage profile IDs allowed to be used with the queue. |
| 77 | */ |
| 78 | allowedStorageProfileIds?: string[]; |
| 79 | /** |
| 80 | * How the queue orders and schedules its jobs. |
| 81 | */ |
| 82 | schedulingConfiguration?: deadline.SchedulingConfiguration; |
| 83 | /** |
| 84 | * Tags to associate with the queue. |
| 85 | */ |
| 86 | tags?: Record<string, string>; |
| 87 | } |
| 88 | |
| 89 | export interface Queue extends Resource< |
| 90 | "AWS.Deadline.Queue", |
| 91 | QueueProps, |
| 92 | { |
| 93 | /** |
| 94 | * The identifier of the farm the queue belongs to. |
| 95 | */ |
| 96 | farmId: string; |
| 97 | /** |
| 98 | * Service-assigned unique identifier of the queue (`queue-...`). |
| 99 | */ |
| 100 | queueId: string; |
| 101 | /** |
| 102 | * ARN of the queue. |
| 103 | */ |
| 104 | queueArn: string; |
| 105 | /** |
| 106 | * The queue's display name. |
| 107 | */ |
| 108 | displayName: string; |
| 109 | /** |
| 110 | * Current scheduling status of the queue. |
| 111 | */ |
| 112 | status: QueueStatus; |
| 113 | /** |
| 114 | * The configured default budget action. |
| 115 | */ |
| 116 | defaultBudgetAction: DefaultQueueBudgetAction; |
| 117 | /** |
| 118 | * ARN of the queue's job role, when configured. |
| 119 | */ |
| 120 | roleArn: string | undefined; |
| 121 | /** |
| 122 | * Current tags reported for the queue. |
| 123 | */ |
| 124 | tags: Record<string, string>; |
| 125 | }, |
| 126 | never, |
| 127 | Providers |
| 128 | > {} |
| 129 | |
| 130 | /** |
| 131 | * An AWS Deadline Cloud queue — accepts render jobs within a farm and |
| 132 | * schedules them onto associated fleets. |
| 133 | * |
| 134 | * ### Creating Queues |
| 135 | * **Example:** Basic Queue |
| 136 | * ```typescript |
| 137 | * import * as AWS from "@alchemy.run/aws"; |
| 138 | * |
| 139 | * const farm = yield* AWS.Deadline.Farm("RenderFarm", {}); |
| 140 | * const queue = yield* AWS.Deadline.Queue("RenderQueue", { |
| 141 | * farmId: farm.farmId, |
| 142 | * }); |
| 143 | * ``` |
| 144 | * |
| 145 | * **Example:** Queue with Job Attachments and Role |
| 146 | * ```typescript |
| 147 | * const queue = yield* AWS.Deadline.Queue("RenderQueue", { |
| 148 | * farmId: farm.farmId, |
| 149 | * roleArn: queueRole.roleArn, |
| 150 | * defaultBudgetAction: "STOP_SCHEDULING_AND_COMPLETE_TASKS", |
| 151 | * jobAttachmentSettings: { |
| 152 | * s3BucketName: bucket.bucketName, |
| 153 | * rootPrefix: "attachments/", |
| 154 | * }, |
| 155 | * }); |
| 156 | * ``` |
| 157 | * |
| 158 | * @resource |
| 159 | */ |
| 160 | export const Queue = Resource<Queue>("AWS.Deadline.Queue"); |
| 161 | |
| 162 | const createQueueName = (id: string, props: { displayName?: string | undefined }) => |
| 163 | props.displayName |
| 164 | ? Effect.succeed(props.displayName) |
| 165 | : createPhysicalName({ id, maxLength: 100 }); |
| 166 | |
| 167 | interface QueueState { |
| 168 | attrs: Queue["Attributes"]; |
| 169 | described: deadline.GetQueueResponse; |
| 170 | } |
| 171 | |
| 172 | const readQueueById = Effect.fn(function* ( |
| 173 | farmId: string, |
| 174 | queueId: string, |
| 175 | arnOf: (path: string) => string, |
| 176 | ) { |
| 177 | const described = yield* deadline |
| 178 | .getQueue({ farmId, queueId }) |
| 179 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 180 | if (!described) return undefined; |
| 181 | const queueArn = arnOf(`farm/${described.farmId}/queue/${described.queueId}`); |
| 182 | const state: QueueState = { |
| 183 | described, |
| 184 | attrs: { |
| 185 | farmId: described.farmId, |
| 186 | queueId: described.queueId, |
| 187 | queueArn, |
| 188 | displayName: described.displayName, |
| 189 | status: described.status, |
| 190 | defaultBudgetAction: described.defaultBudgetAction, |
| 191 | roleArn: described.roleArn, |
| 192 | tags: yield* fetchDeadlineTags(queueArn), |
| 193 | }, |
| 194 | }; |
| 195 | return state; |
| 196 | }); |
| 197 | |
| 198 | const findQueueByDisplayName = Effect.fn(function* ( |
| 199 | farmId: string, |
| 200 | displayName: string, |
| 201 | arnOf: (path: string) => string, |
| 202 | ) { |
| 203 | const summaries = yield* deadline.listQueues.items({ farmId }).pipe( |
| 204 | EffectStream.runCollect, |
| 205 | Effect.map((chunk) => Array.from(chunk)), |
| 206 | // The parent farm may itself be gone. |
| 207 | Effect.catchTag("ResourceNotFoundException", () => |
| 208 | Effect.succeed([] as deadline.QueueSummary[]), |
| 209 | ), |
| 210 | ); |
| 211 | const match = summaries.find((summary) => summary.displayName === displayName); |
| 212 | if (!match) return undefined; |
| 213 | return yield* readQueueById(farmId, match.queueId, arnOf); |
| 214 | }); |
| 215 | |
| 216 | /** |
| 217 | * A queue still present after delete was initiated — retried by the bounded |
| 218 | * wait-until-gone schedule. |
| 219 | */ |
| 220 | class QueueStillExists extends Data.TaggedError("QueueStillExists")<{ |
| 221 | readonly queueId: string; |
| 222 | }> {} |
| 223 | |
| 224 | const retryWhileStillExists = <A, E extends { readonly _tag: string }, R>( |
| 225 | self: Effect.Effect<A, E, R>, |
| 226 | ): Effect.Effect<A, E, R> => |
| 227 | Effect.retry(self, { |
| 228 | while: (e) => e._tag === "QueueStillExists", |
| 229 | schedule: Schedule.max([Schedule.spaced("6 seconds"), Schedule.recurs(9)]), |
| 230 | }); |
| 231 | |
| 232 | const stringSetDelta = (observed: readonly string[], desired: readonly string[]) => ({ |
| 233 | toAdd: desired.filter((value) => !observed.includes(value)), |
| 234 | toRemove: observed.filter((value) => !desired.includes(value)), |
| 235 | }); |
| 236 | |
| 237 | export const QueueProvider = () => |
| 238 | Provider.effect( |
| 239 | Queue, |
| 240 | Effect.gen(function* () { |
| 241 | return { |
| 242 | stables: ["farmId", "queueId", "queueArn"], |
| 243 | // Keyed by a parent farm; cannot be enumerated account-wide without |
| 244 | // iterating every farm — treated as a sub-resource per the factory |
| 245 | // list() convention. |
| 246 | list: () => Effect.succeed([]), |
| 247 | read: Effect.fn(function* ({ id, olds, output }) { |
| 248 | const arnOf = yield* deadlineArnOf; |
| 249 | const farmId = output?.farmId ?? olds?.farmId; |
| 250 | if (farmId === undefined) return undefined; |
| 251 | const state = output?.queueId |
| 252 | ? yield* readQueueById(farmId, output.queueId, arnOf) |
| 253 | : yield* findQueueByDisplayName(farmId, yield* createQueueName(id, olds ?? {}), arnOf); |
| 254 | if (!state) return undefined; |
| 255 | return (yield* hasAlchemyTags(id, state.attrs.tags as Tags)) |
| 256 | ? state.attrs |
| 257 | : Unowned(state.attrs); |
| 258 | }), |
| 259 | diff: Effect.fn(function* ({ news, olds }) { |
| 260 | if (!isResolved(news)) return; |
| 261 | if (olds === undefined) return; |
| 262 | // The parent farm is fixed at creation. |
| 263 | if (olds.farmId !== news.farmId) { |
| 264 | return { action: "replace" } as const; |
| 265 | } |
| 266 | }), |
| 267 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 268 | if (news === undefined) { |
| 269 | return yield* new QueueInvalidProps({ message: "AWS.Deadline.Queue requires props" }); |
| 270 | } |
| 271 | const arnOf = yield* deadlineArnOf; |
| 272 | const farmId = news.farmId; |
| 273 | const displayName = yield* createQueueName(id, news); |
| 274 | const internalTags = yield* createInternalTags(id); |
| 275 | const desiredTags = { ...internalTags, ...news.tags }; |
| 276 | |
| 277 | // Observe. |
| 278 | let state = output?.queueId |
| 279 | ? yield* readQueueById(farmId, output.queueId, arnOf) |
| 280 | : yield* findQueueByDisplayName(farmId, displayName, arnOf); |
| 281 | |
| 282 | // Ensure. |
| 283 | if (state === undefined) { |
| 284 | const created = yield* retryWhileFarmSettling( |
| 285 | deadline.createQueue({ |
| 286 | farmId, |
| 287 | displayName, |
| 288 | description: news.description, |
| 289 | defaultBudgetAction: news.defaultBudgetAction, |
| 290 | jobAttachmentSettings: news.jobAttachmentSettings, |
| 291 | roleArn: news.roleArn, |
| 292 | jobRunAsUser: news.jobRunAsUser, |
| 293 | requiredFileSystemLocationNames: news.requiredFileSystemLocationNames, |
| 294 | allowedStorageProfileIds: news.allowedStorageProfileIds, |
| 295 | schedulingConfiguration: news.schedulingConfiguration, |
| 296 | tags: desiredTags, |
| 297 | }), |
| 298 | ); |
| 299 | yield* session.note(`Created queue ${displayName} (${created.queueId})`); |
| 300 | state = yield* readQueueById(farmId, created.queueId, arnOf); |
| 301 | if (state === undefined) { |
| 302 | return yield* new QueueUnexpectedResponse({ |
| 303 | message: `failed to read created queue ${displayName}`, |
| 304 | }); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Sync mutable settings — compute the delta from OBSERVED state. |
| 309 | const described = state.described; |
| 310 | const fslDelta = stringSetDelta( |
| 311 | described.requiredFileSystemLocationNames ?? [], |
| 312 | news.requiredFileSystemLocationNames ?? [], |
| 313 | ); |
| 314 | const spDelta = stringSetDelta( |
| 315 | described.allowedStorageProfileIds ?? [], |
| 316 | news.allowedStorageProfileIds ?? [], |
| 317 | ); |
| 318 | const needsUpdate = |
| 319 | displayName !== described.displayName || |
| 320 | (news.description !== undefined && |
| 321 | news.description !== (asPlain(described.description) ?? "")) || |
| 322 | (news.defaultBudgetAction !== undefined && |
| 323 | news.defaultBudgetAction !== described.defaultBudgetAction) || |
| 324 | (news.roleArn !== undefined && news.roleArn !== described.roleArn) || |
| 325 | news.jobAttachmentSettings !== undefined || |
| 326 | news.jobRunAsUser !== undefined || |
| 327 | news.schedulingConfiguration !== undefined || |
| 328 | fslDelta.toAdd.length > 0 || |
| 329 | fslDelta.toRemove.length > 0 || |
| 330 | spDelta.toAdd.length > 0 || |
| 331 | spDelta.toRemove.length > 0; |
| 332 | if (needsUpdate) { |
| 333 | yield* retryWhileFarmSettling( |
| 334 | deadline.updateQueue({ |
| 335 | farmId, |
| 336 | queueId: state.attrs.queueId, |
| 337 | displayName, |
| 338 | description: news.description, |
| 339 | defaultBudgetAction: news.defaultBudgetAction, |
| 340 | jobAttachmentSettings: news.jobAttachmentSettings, |
| 341 | roleArn: news.roleArn, |
| 342 | jobRunAsUser: news.jobRunAsUser, |
| 343 | requiredFileSystemLocationNamesToAdd: |
| 344 | fslDelta.toAdd.length > 0 ? fslDelta.toAdd : undefined, |
| 345 | requiredFileSystemLocationNamesToRemove: |
| 346 | fslDelta.toRemove.length > 0 ? fslDelta.toRemove : undefined, |
| 347 | allowedStorageProfileIdsToAdd: spDelta.toAdd.length > 0 ? spDelta.toAdd : undefined, |
| 348 | allowedStorageProfileIdsToRemove: |
| 349 | spDelta.toRemove.length > 0 ? spDelta.toRemove : undefined, |
| 350 | schedulingConfiguration: news.schedulingConfiguration, |
| 351 | }), |
| 352 | ); |
| 353 | yield* session.note(`Updated queue ${displayName}`); |
| 354 | } |
| 355 | |
| 356 | // Sync tags — diff against observed cloud tags. |
| 357 | yield* syncDeadlineTags(state.attrs.queueArn, desiredTags); |
| 358 | |
| 359 | yield* session.note(state.attrs.queueArn); |
| 360 | const final = yield* readQueueById(farmId, state.attrs.queueId, arnOf); |
| 361 | if (!final) { |
| 362 | return yield* new QueueUnexpectedResponse({ |
| 363 | message: `failed to read reconciled queue ${displayName}`, |
| 364 | }); |
| 365 | } |
| 366 | return final.attrs; |
| 367 | }), |
| 368 | delete: Effect.fn(function* ({ output }) { |
| 369 | yield* retryWhileConflict( |
| 370 | deadline.deleteQueue({ |
| 371 | farmId: output.farmId, |
| 372 | queueId: output.queueId, |
| 373 | }), |
| 374 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 375 | // Queue deletion is asynchronous; wait until it is gone so the |
| 376 | // parent farm's deletion does not hit a dependency conflict. |
| 377 | yield* retryWhileStillExists( |
| 378 | Effect.gen(function* () { |
| 379 | const described = yield* deadline |
| 380 | .getQueue({ farmId: output.farmId, queueId: output.queueId }) |
| 381 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 382 | if (described !== undefined) { |
| 383 | return yield* new QueueStillExists({ queueId: output.queueId }); |
| 384 | } |
| 385 | }), |
| 386 | ).pipe( |
| 387 | // Exhausted retries: deletion is already converging server-side. |
| 388 | Effect.catchTag("QueueStillExists", () => Effect.void), |
| 389 | ); |
| 390 | // Deadline auto-creates /aws/deadline/{farmId}/{queueId} for the |
| 391 | // queue's job/session logs and deleteQueue does NOT remove it — |
| 392 | // reap it so a deleted queue leaves no orphaned log group. |
| 393 | yield* reapDeadlineLogGroups(`/aws/deadline/${output.farmId}/${output.queueId}`); |
| 394 | }), |
| 395 | }; |
| 396 | }), |
| 397 | ); |