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, 06:00 UTC
| 1 | import * as cloudtasks from "@distilled.cloud/gcp/cloudtasks_v2"; |
| 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 * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | |
| 12 | const DEFAULT_STATE = "RUNNING" as const; |
| 13 | const MAX_QUEUE_ID_LENGTH = 100; |
| 14 | |
| 15 | export type QueueRetryConfig = { |
| 16 | /** Attempts per task, including the first. `-1` means unlimited. */ |
| 17 | maxAttempts?: number; |
| 18 | /** Time limit for retrying a failed task, e.g. `"3600s"`. `"0s"` is unlimited. */ |
| 19 | maxRetryDuration?: string; |
| 20 | /** Minimum backoff after a failed attempt, e.g. `"0.1s"`. */ |
| 21 | minBackoff?: string; |
| 22 | /** Maximum backoff after a failed attempt, e.g. `"3600s"`. */ |
| 23 | maxBackoff?: string; |
| 24 | /** Times the retry interval doubles before increasing linearly. */ |
| 25 | maxDoublings?: number; |
| 26 | }; |
| 27 | |
| 28 | export type QueueRateLimits = { |
| 29 | /** Maximum dispatch rate (tasks per second). Max `500`. */ |
| 30 | maxDispatchesPerSecond?: number; |
| 31 | /** Maximum concurrent dispatches. Max `5000`. */ |
| 32 | maxConcurrentDispatches?: number; |
| 33 | }; |
| 34 | |
| 35 | export type QueueStackdriverLoggingConfig = { |
| 36 | /** Fraction of operations to log, in `[0.0, 1.0]`. `0` logs nothing. */ |
| 37 | samplingRatio?: number; |
| 38 | }; |
| 39 | |
| 40 | export type QueueHttpTarget = cloudtasks.HttpTarget; |
| 41 | export type QueueAppEngineRouting = cloudtasks.AppEngineRouting; |
| 42 | |
| 43 | export type QueueState = "RUNNING" | "PAUSED"; |
| 44 | |
| 45 | export type QueueProps = { |
| 46 | /** |
| 47 | * Queue id (the `{queue}` segment of |
| 48 | * `projects/{project}/locations/{location}/queues/{queue}`). If omitted, |
| 49 | * a unique name is generated from the stack, stage, and logical id. |
| 50 | * Immutable — changing it replaces the queue. |
| 51 | */ |
| 52 | queueId?: string; |
| 53 | /** |
| 54 | * Location of the queue (e.g. `us-central1`). Immutable — changing it |
| 55 | * replaces the queue. `US-CENTRAL1` is accepted and normalized to |
| 56 | * `us-central1`. |
| 57 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 58 | */ |
| 59 | location?: string; |
| 60 | /** |
| 61 | * Queue-level retry settings applied to Cloud Tasks–created tasks. |
| 62 | * Omitted fields keep the values Cloud Tasks currently has (including |
| 63 | * API defaults). |
| 64 | */ |
| 65 | retryConfig?: QueueRetryConfig; |
| 66 | /** |
| 67 | * Dispatch rate limits. `maxBurstSize` is output-only and not settable. |
| 68 | * Omitted fields keep the current Cloud Tasks values. |
| 69 | */ |
| 70 | rateLimits?: QueueRateLimits; |
| 71 | /** |
| 72 | * Cloud Logging sampling. If omitted, existing logging config is left |
| 73 | * unchanged. |
| 74 | */ |
| 75 | stackdriverLoggingConfig?: QueueStackdriverLoggingConfig; |
| 76 | /** |
| 77 | * Queue-level HTTP target overrides for HTTP tasks. If omitted, the |
| 78 | * queue's current overrides are left unchanged. |
| 79 | */ |
| 80 | httpTarget?: QueueHttpTarget; |
| 81 | /** |
| 82 | * App Engine routing override for App Engine tasks in this queue. |
| 83 | */ |
| 84 | appEngineRoutingOverride?: QueueAppEngineRouting; |
| 85 | /** |
| 86 | * Desired queue state. `UpdateQueue` cannot change state — Alchemy |
| 87 | * calls PauseQueue / ResumeQueue. `DISABLED` is queue.yaml-only and |
| 88 | * is not settable here. |
| 89 | * @default "RUNNING" |
| 90 | */ |
| 91 | state?: QueueState; |
| 92 | }; |
| 93 | |
| 94 | export type Queue = Resource< |
| 95 | "GCP.CloudTasks.Queue", |
| 96 | QueueProps, |
| 97 | { |
| 98 | /** Full resource name `projects/{project}/locations/{location}/queues/{queue}`. */ |
| 99 | name: string; |
| 100 | /** Queue id (last path segment). */ |
| 101 | queueId: string; |
| 102 | /** Project id. */ |
| 103 | project: string; |
| 104 | /** Location id (e.g. `us-central1`). */ |
| 105 | location: string; |
| 106 | /** Server-reported state (`RUNNING`, `PAUSED`, `DISABLED`). */ |
| 107 | state: string | undefined; |
| 108 | /** Retry settings currently on the queue. */ |
| 109 | retryConfig: QueueRetryConfig | undefined; |
| 110 | /** Dispatch rate limits currently on the queue. */ |
| 111 | rateLimits: QueueRateLimits | undefined; |
| 112 | /** Logging config currently on the queue. */ |
| 113 | stackdriverLoggingConfig: QueueStackdriverLoggingConfig | undefined; |
| 114 | /** Queue-level HTTP target overrides. */ |
| 115 | httpTarget: QueueHttpTarget | undefined; |
| 116 | /** App Engine routing override, if any. */ |
| 117 | appEngineRoutingOverride: QueueAppEngineRouting | undefined; |
| 118 | /** Last purge time, if the queue has ever been purged. */ |
| 119 | purgeTime: string | undefined; |
| 120 | }, |
| 121 | never, |
| 122 | Providers |
| 123 | >; |
| 124 | |
| 125 | /** |
| 126 | * A Google Cloud Tasks queue. |
| 127 | * |
| 128 | * Cloud Tasks queues have no labels, so Alchemy identifies its queues by |
| 129 | * the generated `queueId`: `read` reports a queue with an explicit |
| 130 | * `queueId` as unowned (adopt it with `--adopt`), and nuke cannot |
| 131 | * discover queues. |
| 132 | * |
| 133 | * Changing `queueId` or `location` replaces the queue. Deleted queue |
| 134 | * names are tombstoned for up to 3 days — CreateQueue may appear to |
| 135 | * succeed; GetQueue is the source of truth. |
| 136 | * |
| 137 | * ### Creating a Queue |
| 138 | * **Example:** Generated name |
| 139 | * ```typescript |
| 140 | * const queue = yield* GCP.CloudTasks.Queue("jobs", {}); |
| 141 | * ``` |
| 142 | * |
| 143 | * **Example:** Rate limits, retries, and logging |
| 144 | * ```typescript |
| 145 | * const queue = yield* GCP.CloudTasks.Queue("jobs", { |
| 146 | * location: "us-central1", |
| 147 | * rateLimits: { |
| 148 | * maxDispatchesPerSecond: 10, |
| 149 | * maxConcurrentDispatches: 5, |
| 150 | * }, |
| 151 | * retryConfig: { |
| 152 | * maxAttempts: 5, |
| 153 | * minBackoff: "1s", |
| 154 | * maxBackoff: "60s", |
| 155 | * }, |
| 156 | * stackdriverLoggingConfig: { samplingRatio: 1 }, |
| 157 | * }); |
| 158 | * ``` |
| 159 | * |
| 160 | * **Example:** Paused queue |
| 161 | * ```typescript |
| 162 | * const queue = yield* GCP.CloudTasks.Queue("jobs", { |
| 163 | * state: "PAUSED", |
| 164 | * }); |
| 165 | * ``` |
| 166 | * |
| 167 | * @resource |
| 168 | * @category CloudTasks |
| 169 | */ |
| 170 | export const Queue = Resource<Queue>("GCP.CloudTasks.Queue"); |
| 171 | |
| 172 | export class QueueNotResolved extends Data.TaggedError("GCP.CloudTasks.QueueNotResolved")<{ |
| 173 | resourceName: string; |
| 174 | }> {} |
| 175 | |
| 176 | const lastSegment = (value: string) => { |
| 177 | const trimmed = value.replace(/\/+$/, ""); |
| 178 | const parts = trimmed.split("/"); |
| 179 | return parts[parts.length - 1] || trimmed; |
| 180 | }; |
| 181 | |
| 182 | const normalizeLocation = (location: string | undefined, fallback: string) => |
| 183 | lastSegment(location ?? fallback).toLowerCase(); |
| 184 | |
| 185 | const parseName = (name: string) => { |
| 186 | const parts = name.split("/"); |
| 187 | return { |
| 188 | project: parts[1] ?? "", |
| 189 | location: parts[3] ?? "", |
| 190 | queueId: parts[5] ?? lastSegment(name), |
| 191 | }; |
| 192 | }; |
| 193 | |
| 194 | const resourceName = (project: string, location: string, queueId: string) => |
| 195 | `projects/${project}/locations/${location}/queues/${queueId}`; |
| 196 | |
| 197 | const locationParent = (project: string, location: string) => |
| 198 | `projects/${project}/locations/${location}`; |
| 199 | |
| 200 | const toQueueId = (id: string, queueId: string | undefined, existing?: string) => |
| 201 | Effect.gen(function* () { |
| 202 | if (queueId !== undefined) return queueId; |
| 203 | if (existing !== undefined) return existing; |
| 204 | const generated = yield* createPhysicalName({ |
| 205 | id, |
| 206 | maxLength: MAX_QUEUE_ID_LENGTH, |
| 207 | lowercase: true, |
| 208 | }); |
| 209 | const named = /^[a-z]/.test(generated) ? generated : `q${generated}`; |
| 210 | return named.replace(/-+$/g, "").slice(0, MAX_QUEUE_ID_LENGTH); |
| 211 | }); |
| 212 | |
| 213 | const sortHeaderOverrides = ( |
| 214 | headers: cloudtasks.HeaderOverrideList | undefined, |
| 215 | ): cloudtasks.HeaderOverrideList => |
| 216 | [...(headers ?? [])] |
| 217 | .map((item) => ({ |
| 218 | header: { |
| 219 | key: item.header?.key?.toLowerCase(), |
| 220 | value: item.header?.value, |
| 221 | }, |
| 222 | })) |
| 223 | .sort((left, right) => (left.header?.key ?? "").localeCompare(right.header?.key ?? "")); |
| 224 | |
| 225 | const stable = (value: unknown): string => |
| 226 | JSON.stringify(value, (_key, current) => { |
| 227 | if (current && typeof current === "object" && !Array.isArray(current)) { |
| 228 | return Object.fromEntries( |
| 229 | Object.entries(current as Record<string, unknown>) |
| 230 | .filter(([, item]) => item !== undefined) |
| 231 | .sort(([a], [b]) => a.localeCompare(b)), |
| 232 | ); |
| 233 | } |
| 234 | return current; |
| 235 | }); |
| 236 | |
| 237 | const normalizeDuration = (value: string | undefined) => { |
| 238 | if (value === undefined) return undefined; |
| 239 | const match = /^([0-9]+(?:\.[0-9]+)?)s$/.exec(value.trim()); |
| 240 | if (!match) return value; |
| 241 | return `${Number(match[1])}s`; |
| 242 | }; |
| 243 | |
| 244 | const normalizeRetry = (config: QueueRetryConfig) => ({ |
| 245 | maxAttempts: config.maxAttempts, |
| 246 | maxRetryDuration: normalizeDuration(config.maxRetryDuration), |
| 247 | minBackoff: normalizeDuration(config.minBackoff), |
| 248 | maxBackoff: normalizeDuration(config.maxBackoff), |
| 249 | maxDoublings: config.maxDoublings, |
| 250 | }); |
| 251 | |
| 252 | const overlayChanged = ( |
| 253 | desired: object | undefined, |
| 254 | observed: object | undefined, |
| 255 | normalize: (value: object) => unknown = (value) => value, |
| 256 | ): boolean => { |
| 257 | if (desired === undefined) return false; |
| 258 | const picked: Record<string, unknown> = {}; |
| 259 | const source = (observed ?? {}) as Record<string, unknown>; |
| 260 | for (const key of Object.keys(desired)) { |
| 261 | picked[key] = source[key]; |
| 262 | } |
| 263 | return stable(normalize(desired)) !== stable(normalize(picked)); |
| 264 | }; |
| 265 | |
| 266 | const toRateLimits = ( |
| 267 | config: QueueRateLimits, |
| 268 | observed?: cloudtasks.RateLimits, |
| 269 | ): cloudtasks.RateLimits => { |
| 270 | const merged: cloudtasks.RateLimits = { ...observed, ...config }; |
| 271 | delete merged.maxBurstSize; |
| 272 | return merged; |
| 273 | }; |
| 274 | |
| 275 | const toUserRateLimits = ( |
| 276 | config: cloudtasks.RateLimits | undefined, |
| 277 | ): QueueRateLimits | undefined => { |
| 278 | if (config === undefined) return undefined; |
| 279 | const next: QueueRateLimits = {}; |
| 280 | if (config.maxDispatchesPerSecond !== undefined) { |
| 281 | next.maxDispatchesPerSecond = config.maxDispatchesPerSecond; |
| 282 | } |
| 283 | if (config.maxConcurrentDispatches !== undefined) { |
| 284 | next.maxConcurrentDispatches = config.maxConcurrentDispatches; |
| 285 | } |
| 286 | return Object.keys(next).length > 0 ? next : undefined; |
| 287 | }; |
| 288 | |
| 289 | const toAttrs = (queue: cloudtasks.Queue, project: string): Queue["Attributes"] => { |
| 290 | const name = queue.name ?? ""; |
| 291 | const parsed = parseName(name); |
| 292 | return { |
| 293 | name, |
| 294 | queueId: parsed.queueId, |
| 295 | project: parsed.project || project, |
| 296 | location: parsed.location, |
| 297 | state: queue.state, |
| 298 | retryConfig: queue.retryConfig, |
| 299 | rateLimits: toUserRateLimits(queue.rateLimits), |
| 300 | stackdriverLoggingConfig: queue.stackdriverLoggingConfig, |
| 301 | httpTarget: queue.httpTarget, |
| 302 | appEngineRoutingOverride: queue.appEngineRoutingOverride, |
| 303 | purgeTime: queue.purgeTime, |
| 304 | }; |
| 305 | }; |
| 306 | |
| 307 | const getByName = (name: string) => |
| 308 | cloudtasks |
| 309 | .getProjectsLocationsQueues({ name }) |
| 310 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 311 | |
| 312 | const sameHttpTarget = (left: cloudtasks.HttpTarget, right: cloudtasks.HttpTarget | undefined) => { |
| 313 | const normalize = (target: cloudtasks.HttpTarget | undefined) => { |
| 314 | if (target === undefined) return undefined; |
| 315 | return { |
| 316 | ...target, |
| 317 | headerOverrides: sortHeaderOverrides(target.headerOverrides), |
| 318 | }; |
| 319 | }; |
| 320 | return stable(normalize(left)) === stable(normalize(right)); |
| 321 | }; |
| 322 | |
| 323 | export const QueueProvider = () => |
| 324 | Provider.succeed(Queue, { |
| 325 | stables: ["name", "queueId", "project", "location"], |
| 326 | |
| 327 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 328 | const env = yield* GcpEnvironment.current; |
| 329 | if (!isResolved(news)) return undefined; |
| 330 | const previousId = olds?.queueId ?? output?.queueId; |
| 331 | const nextId = news.queueId ?? previousId; |
| 332 | const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId; |
| 333 | |
| 334 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 335 | const nextLocation = normalizeLocation(news.location ?? output?.location, env.region); |
| 336 | if (idChanged || previousLocation !== nextLocation) { |
| 337 | return { action: "replace" as const }; |
| 338 | } |
| 339 | return undefined; |
| 340 | }), |
| 341 | |
| 342 | read: Effect.fn(function* ({ id, olds, output }) { |
| 343 | const env = yield* GcpEnvironment.current; |
| 344 | const queueId = yield* toQueueId(id, olds?.queueId, output?.queueId); |
| 345 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 346 | const name = output?.name ?? resourceName(env.project, location, queueId); |
| 347 | const existing = yield* getByName(name); |
| 348 | if (existing === undefined) return undefined; |
| 349 | const attrs = toAttrs(existing, env.project); |
| 350 | const generated = yield* toQueueId(id, undefined); |
| 351 | return attrs.queueId === generated ? attrs : Unowned(attrs); |
| 352 | }), |
| 353 | |
| 354 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 355 | const env = yield* GcpEnvironment.current; |
| 356 | const queueId = yield* toQueueId(id, news.queueId, output?.queueId); |
| 357 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 358 | const name = resourceName(env.project, location, queueId); |
| 359 | const parent = locationParent(env.project, location); |
| 360 | const desiredHttpTarget = news.httpTarget; |
| 361 | const desiredState = news.state ?? DEFAULT_STATE; |
| 362 | |
| 363 | let current = yield* getByName(name); |
| 364 | |
| 365 | if (current === undefined) { |
| 366 | const body: cloudtasks.Queue = { |
| 367 | name, |
| 368 | httpTarget: desiredHttpTarget, |
| 369 | }; |
| 370 | if (news.retryConfig !== undefined) { |
| 371 | body.retryConfig = news.retryConfig; |
| 372 | } |
| 373 | if (news.rateLimits !== undefined) { |
| 374 | body.rateLimits = toRateLimits(news.rateLimits); |
| 375 | } |
| 376 | if (news.stackdriverLoggingConfig !== undefined) { |
| 377 | body.stackdriverLoggingConfig = news.stackdriverLoggingConfig; |
| 378 | } |
| 379 | if (news.appEngineRoutingOverride !== undefined) { |
| 380 | body.appEngineRoutingOverride = news.appEngineRoutingOverride; |
| 381 | } |
| 382 | // CreateQueue can appear to succeed for a tombstoned name. GetQueue |
| 383 | // is the source of truth (up to ~3 days after delete). |
| 384 | yield* cloudtasks |
| 385 | .createProjectsLocationsQueues({ |
| 386 | parent, |
| 387 | body, |
| 388 | }) |
| 389 | .pipe(Effect.catchTag("Conflict", () => Effect.void)); |
| 390 | current = yield* getByName(name); |
| 391 | } |
| 392 | |
| 393 | if (current === undefined) { |
| 394 | return yield* new QueueNotResolved({ resourceName: name }); |
| 395 | } |
| 396 | |
| 397 | const mask: string[] = []; |
| 398 | const patchBody: cloudtasks.Queue = { name }; |
| 399 | |
| 400 | if ( |
| 401 | overlayChanged(news.retryConfig, current.retryConfig, (value) => |
| 402 | normalizeRetry(value as QueueRetryConfig), |
| 403 | ) |
| 404 | ) { |
| 405 | patchBody.retryConfig = { |
| 406 | ...current.retryConfig, |
| 407 | ...news.retryConfig, |
| 408 | }; |
| 409 | mask.push("retryConfig"); |
| 410 | } |
| 411 | |
| 412 | const desiredRateLimits = news.rateLimits; |
| 413 | if ( |
| 414 | desiredRateLimits !== undefined && |
| 415 | overlayChanged(desiredRateLimits, toUserRateLimits(current.rateLimits)) |
| 416 | ) { |
| 417 | patchBody.rateLimits = toRateLimits(desiredRateLimits, current.rateLimits); |
| 418 | mask.push("rateLimits"); |
| 419 | } |
| 420 | |
| 421 | if (overlayChanged(news.stackdriverLoggingConfig, current.stackdriverLoggingConfig)) { |
| 422 | patchBody.stackdriverLoggingConfig = { |
| 423 | ...current.stackdriverLoggingConfig, |
| 424 | ...news.stackdriverLoggingConfig, |
| 425 | }; |
| 426 | mask.push("stackdriverLoggingConfig"); |
| 427 | } |
| 428 | |
| 429 | if ( |
| 430 | desiredHttpTarget !== undefined && |
| 431 | !sameHttpTarget(desiredHttpTarget, current.httpTarget) |
| 432 | ) { |
| 433 | patchBody.httpTarget = desiredHttpTarget; |
| 434 | mask.push("httpTarget"); |
| 435 | } |
| 436 | |
| 437 | if (overlayChanged(news.appEngineRoutingOverride, current.appEngineRoutingOverride)) { |
| 438 | patchBody.appEngineRoutingOverride = { |
| 439 | ...current.appEngineRoutingOverride, |
| 440 | ...news.appEngineRoutingOverride, |
| 441 | }; |
| 442 | mask.push("appEngineRoutingOverride"); |
| 443 | } |
| 444 | |
| 445 | if (mask.length > 0) { |
| 446 | current = yield* cloudtasks.patchProjectsLocationsQueues({ |
| 447 | name, |
| 448 | updateMask: mask.join(","), |
| 449 | body: patchBody, |
| 450 | }); |
| 451 | } |
| 452 | |
| 453 | if (desiredState === "PAUSED" && current.state === "RUNNING") { |
| 454 | current = yield* cloudtasks.pauseProjectsLocationsQueues({ |
| 455 | name, |
| 456 | body: {}, |
| 457 | }); |
| 458 | } else if ( |
| 459 | desiredState === "RUNNING" && |
| 460 | (current.state === "PAUSED" || current.state === "DISABLED") |
| 461 | ) { |
| 462 | current = yield* cloudtasks.resumeProjectsLocationsQueues({ |
| 463 | name, |
| 464 | body: {}, |
| 465 | }); |
| 466 | } |
| 467 | |
| 468 | return toAttrs(current, env.project); |
| 469 | }), |
| 470 | |
| 471 | delete: Effect.fn(function* ({ output }) { |
| 472 | yield* cloudtasks |
| 473 | .deleteProjectsLocationsQueues({ name: output.name }) |
| 474 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 475 | }), |
| 476 | }); |