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, 03:37 UTC
| 1 | import * as queues from "@distilled.cloud/cloudflare/queues"; |
| 2 | import * as workers from "@distilled.cloud/cloudflare/workers"; |
| 3 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 4 | import { isResolved } from "alchemy/Diff"; |
| 5 | import * as ProviderLayer from "alchemy/Local/ProviderLayer"; |
| 6 | import * as RpcProvider from "alchemy/Local/RpcProvider"; |
| 7 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 8 | import * as Provider from "alchemy/Provider"; |
| 9 | import { isResourceOfType, Resource } from "alchemy/Resource"; |
| 10 | import { Stack } from "alchemy/Stack"; |
| 11 | import * as Data from "effect/Data"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as MutableHashMap from "effect/MutableHashMap"; |
| 15 | import * as Option from "effect/Option"; |
| 16 | import * as Schedule from "effect/Schedule"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 19 | import { localAccountId } from "../LocalAccount.ts"; |
| 20 | import { |
| 21 | generateLocalId, |
| 22 | isLiveId, |
| 23 | LOCAL_PROVIDERS_URL, |
| 24 | LocalRuntimeState, |
| 25 | localRuntimeServices, |
| 26 | } from "../LocalRuntime.ts"; |
| 27 | import type { Providers } from "../Providers.ts"; |
| 28 | import { detachQueueConsumersOfScript } from "./Consumer.ts"; |
| 29 | |
| 30 | export const isQueue = (value: unknown): value is Queue => |
| 31 | isResourceOfType(value, "Cloudflare.Queues.Queue"); |
| 32 | |
| 33 | /** |
| 34 | * Where Cloudflare stores and processes a queue's messages. |
| 35 | */ |
| 36 | export type QueueJurisdiction = "eu" | "us" | "fedramp"; |
| 37 | |
| 38 | export type QueueProps = { |
| 39 | /** |
| 40 | * Name of the queue. If omitted, a unique name will be generated. |
| 41 | * Queue names are unique per account across all jurisdictions. |
| 42 | * @default ${app}-${stage}-${id} |
| 43 | */ |
| 44 | name?: string; |
| 45 | /** |
| 46 | * Restrict where Cloudflare stores and processes the queue's messages. |
| 47 | * Workers can still produce to and consume from it from any location. |
| 48 | * Cloudflare only accepts it when the queue is created, so changing it |
| 49 | * replaces the queue and drops the messages still in it. A dead-letter |
| 50 | * queue must be in the same jurisdiction as its source queue. |
| 51 | * @default undefined (global) |
| 52 | */ |
| 53 | jurisdiction?: QueueJurisdiction; |
| 54 | /** |
| 55 | * Seconds an unconsumed message is kept before it is deleted. Cloudflare |
| 56 | * allows 60 seconds to 14 days (1209600). Workers Free plans can't change it. |
| 57 | * Removing it resets it to the default. |
| 58 | * @default 345600 (4 days) |
| 59 | */ |
| 60 | messageRetentionPeriod?: number; |
| 61 | /** |
| 62 | * Seconds to delay delivery of every message to consumers, from 0 to 86400 (24 hours). |
| 63 | * Removing it resets it to the default. |
| 64 | * @default 0 |
| 65 | */ |
| 66 | deliveryDelay?: number; |
| 67 | /** |
| 68 | * Pause delivery of messages to consumers. Producers can still send. |
| 69 | * Removing it resets it to the default. |
| 70 | * @default false |
| 71 | */ |
| 72 | deliveryPaused?: boolean; |
| 73 | }; |
| 74 | |
| 75 | type QueueSettings = Pick< |
| 76 | QueueProps, |
| 77 | "messageRetentionPeriod" | "deliveryDelay" | "deliveryPaused" |
| 78 | >; |
| 79 | |
| 80 | /** |
| 81 | * The queue found by name is in a different jurisdiction than the one |
| 82 | * declared. Cloudflare can't move a queue between jurisdictions, so it is |
| 83 | * not adopted. |
| 84 | */ |
| 85 | export class QueueJurisdictionMismatch extends Data.TaggedError("QueueJurisdictionMismatch")<{ |
| 86 | readonly message: string; |
| 87 | readonly queueName: string; |
| 88 | readonly expected: QueueJurisdiction | undefined; |
| 89 | readonly actual: QueueJurisdiction | undefined; |
| 90 | }> {} |
| 91 | |
| 92 | export type Queue = Resource< |
| 93 | "Cloudflare.Queues.Queue", |
| 94 | QueueProps, |
| 95 | { |
| 96 | queueId: string; |
| 97 | queueName: string; |
| 98 | accountId: string; |
| 99 | /** The queue's jurisdiction, or `undefined` for a global queue. */ |
| 100 | jurisdiction: QueueJurisdiction | undefined; |
| 101 | /** Seconds an unconsumed message is kept before it is deleted. */ |
| 102 | messageRetentionPeriod: number; |
| 103 | /** Seconds delivery of every message to consumers is delayed. */ |
| 104 | deliveryDelay: number; |
| 105 | /** Whether delivery of messages to consumers is paused. */ |
| 106 | deliveryPaused: boolean; |
| 107 | }, |
| 108 | never, |
| 109 | Providers |
| 110 | >; |
| 111 | |
| 112 | /** |
| 113 | * A Cloudflare Queue for reliable message passing between Workers. |
| 114 | * |
| 115 | * Queues enable you to send and receive messages with guaranteed delivery. |
| 116 | * Create a queue as a resource, then bind it to a Worker to send messages |
| 117 | * at runtime. Register a consumer to process messages. |
| 118 | * ### Creating a Queue |
| 119 | * **Example:** Basic queue |
| 120 | * ```typescript |
| 121 | * const queue = yield* Cloudflare.Queues.Queue("MyQueue"); |
| 122 | * ``` |
| 123 | * |
| 124 | * **Example:** Queue with explicit name |
| 125 | * ```typescript |
| 126 | * const queue = yield* Cloudflare.Queues.Queue("MyQueue", { |
| 127 | * name: "my-app-queue", |
| 128 | * }); |
| 129 | * ``` |
| 130 | * |
| 131 | * ### Configuring Settings |
| 132 | * Durations are in seconds, matching the Cloudflare API. A setting you |
| 133 | * never declare keeps whatever value the queue already has. |
| 134 | * |
| 135 | * **Example:** Dead-letter queue that keeps messages for 14 days |
| 136 | * ```typescript |
| 137 | * const deadLetters = yield* Cloudflare.Queues.Queue("DeadLetters", { |
| 138 | * messageRetentionPeriod: 14 * 24 * 60 * 60, |
| 139 | * }); |
| 140 | * ``` |
| 141 | * |
| 142 | * **Example:** Delay and pause delivery |
| 143 | * ```typescript |
| 144 | * const queue = yield* Cloudflare.Queues.Queue("MyQueue", { |
| 145 | * deliveryDelay: 30, |
| 146 | * deliveryPaused: true, |
| 147 | * }); |
| 148 | * ``` |
| 149 | * |
| 150 | * ### Data Residency |
| 151 | * **Example:** Queue restricted to the EU |
| 152 | * ```typescript |
| 153 | * const queue = yield* Cloudflare.Queues.Queue("EuQueue", { |
| 154 | * jurisdiction: "eu", |
| 155 | * }); |
| 156 | * const deadLetters = yield* Cloudflare.Queues.Queue("EuDeadLetters", { |
| 157 | * jurisdiction: "eu", |
| 158 | * }); |
| 159 | * ``` |
| 160 | * |
| 161 | * ### Binding to a Worker |
| 162 | * In an Effect-style Worker, use `Cloudflare.Queues.WriteQueue` in |
| 163 | * the init phase and provide `Cloudflare.Queues.WriteQueueBinding` in |
| 164 | * the runtime layer. The returned `WriteQueueClient` exposes `send` |
| 165 | * and `sendBatch`. |
| 166 | * |
| 167 | * **Example:** Sending messages from a Worker |
| 168 | * ```typescript |
| 169 | * import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 170 | * import * as Effect from "effect/Effect"; |
| 171 | * import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 172 | * import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 173 | * |
| 174 | * export const Queue = Cloudflare.Queues.Queue("Queue"); |
| 175 | * |
| 176 | * export default Cloudflare.Worker( |
| 177 | * "Worker", |
| 178 | * { main: import.meta.url }, |
| 179 | * Effect.gen(function* () { |
| 180 | * const queue = yield* Cloudflare.Queues.WriteQueue(Queue); |
| 181 | * |
| 182 | * return { |
| 183 | * fetch: Effect.gen(function* () { |
| 184 | * const request = yield* HttpServerRequest; |
| 185 | * if (request.url === "/queue/send" && request.method === "POST") { |
| 186 | * const text = yield* request.text; |
| 187 | * yield* queue.send({ text, sentAt: Date.now() }).pipe(Effect.orDie); |
| 188 | * return yield* HttpServerResponse.json( |
| 189 | * { sent: { text } }, |
| 190 | * { status: 202 }, |
| 191 | * ); |
| 192 | * } |
| 193 | * return HttpServerResponse.text("Not Found", { status: 404 }); |
| 194 | * }), |
| 195 | * }; |
| 196 | * }).pipe(Effect.provide(Cloudflare.Queues.WriteQueueBinding)), |
| 197 | * ); |
| 198 | * ``` |
| 199 | * |
| 200 | * @resource |
| 201 | * @product Queues |
| 202 | * @category Storage & Databases |
| 203 | */ |
| 204 | export const Queue = Resource<Queue>("Cloudflare.Queues.Queue", { |
| 205 | aliases: ["Cloudflare.Queue"], |
| 206 | }); |
| 207 | |
| 208 | export const ProviderLive = () => |
| 209 | Provider.succeed(Queue, { |
| 210 | stables: ["queueId", "accountId"], |
| 211 | diff: Effect.fn(function* ({ id, olds = {}, news = {}, output }) { |
| 212 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 213 | if (!isResolved(news)) return undefined; |
| 214 | if ((output?.accountId ?? accountId) !== accountId) { |
| 215 | return { action: "replace" } as const; |
| 216 | } |
| 217 | const oldName = output?.queueName ?? (yield* createQueueName(id, olds.name)); |
| 218 | // Auto-generated names are engine-owned: the deployed name stays |
| 219 | // authoritative even if the generator would name this id differently |
| 220 | // today. Only an explicit user-provided name can force a replace. |
| 221 | const name = news.name ?? oldName; |
| 222 | const oldJurisdiction = output ? output.jurisdiction : olds.jurisdiction; |
| 223 | if (name !== oldName || oldJurisdiction !== news.jurisdiction) { |
| 224 | // Queue names are unique per account across jurisdictions, so a |
| 225 | // replacement that keeps an explicit name can't coexist with its |
| 226 | // predecessor. Delete the old generation first. Generated names |
| 227 | // change with the new instance id, so they stay create-first. |
| 228 | return news.name !== undefined && news.name === oldName |
| 229 | ? ({ action: "replace", deleteFirst: true } as const) |
| 230 | : ({ action: "replace" } as const); |
| 231 | } |
| 232 | }), |
| 233 | reconcile: Effect.fn(function* ({ id, news = {}, olds, output }) { |
| 234 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 235 | const queueName = yield* createQueueName(id, news.name); |
| 236 | const acct = output?.accountId ?? accountId; |
| 237 | |
| 238 | // Observe — re-fetch the cached queue; fall back to a name scan |
| 239 | // when the cached id is gone (out-of-band delete or partial |
| 240 | // state-persistence failure). |
| 241 | let observed: ObservedQueue | undefined; |
| 242 | // A `dev:` id (a mis-stamped legacy local row) is not a real queue id — |
| 243 | // skip the lookup (Cloudflare rejects it as a malformed parameter) and |
| 244 | // fall through to the name scan. |
| 245 | if (output?.queueId && isLiveId(output.queueId)) { |
| 246 | observed = yield* queues |
| 247 | .getQueue({ |
| 248 | accountId: acct, |
| 249 | queueId: output.queueId, |
| 250 | }) |
| 251 | .pipe(Effect.catchTag(["QueueNotFound", "InvalidRoute"], () => Effect.undefined)); |
| 252 | } |
| 253 | if (!observed) { |
| 254 | observed = yield* findQueueByName(queueName); |
| 255 | } |
| 256 | |
| 257 | // Ensure — create if missing. Cloudflare returns a generic |
| 258 | // failure when the queue name is taken; tolerate by adopting |
| 259 | // the queue with the same name so reconciles converge after a |
| 260 | // crashed peer. |
| 261 | if (!observed) { |
| 262 | observed = yield* queues |
| 263 | .createQueue({ |
| 264 | accountId: acct, |
| 265 | queueName, |
| 266 | jurisdiction: news.jurisdiction, |
| 267 | }) |
| 268 | .pipe( |
| 269 | Effect.catchTag("QueueAlreadyExists", () => |
| 270 | Effect.gen(function* () { |
| 271 | const match = yield* findQueueByName(queueName); |
| 272 | if (match && match.queueId && match.queueName) { |
| 273 | return match; |
| 274 | } |
| 275 | return yield* Effect.die( |
| 276 | `Queue "${queueName}" already exists but could not be found`, |
| 277 | ); |
| 278 | }), |
| 279 | ), |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | // Jurisdiction is create-only. A queue found by name (adoption, a |
| 284 | // crashed peer) in another jurisdiction can't be converged. |
| 285 | const jurisdiction = toJurisdiction(observed.jurisdiction); |
| 286 | if (jurisdiction !== news.jurisdiction) { |
| 287 | return yield* new QueueJurisdictionMismatch({ |
| 288 | message: |
| 289 | `Queue "${queueName}" exists in jurisdiction ${jurisdiction ?? "global"}, ` + |
| 290 | `not ${news.jurisdiction ?? "global"}. A queue's jurisdiction can't be changed.`, |
| 291 | queueName, |
| 292 | expected: news.jurisdiction, |
| 293 | actual: jurisdiction, |
| 294 | }); |
| 295 | } |
| 296 | |
| 297 | // Sync settings — diff the observed settings against the declared |
| 298 | // ones and patch only the delta. |
| 299 | const current = toSettings(observed.settings); |
| 300 | const desired = desiredSettings(news, olds); |
| 301 | const delta: QueueSettings = {}; |
| 302 | for (const key of Object.keys(desired) as (keyof QueueSettings)[]) { |
| 303 | if (desired[key] !== undefined && desired[key] !== current[key]) { |
| 304 | (delta as Record<string, unknown>)[key] = desired[key]; |
| 305 | } |
| 306 | } |
| 307 | let settings = current; |
| 308 | if (Object.keys(delta).length > 0) { |
| 309 | const patched = yield* queues.patchQueue({ |
| 310 | accountId: acct, |
| 311 | queueId: observed.queueId!, |
| 312 | settings: delta, |
| 313 | }); |
| 314 | settings = toSettings(patched.settings); |
| 315 | } |
| 316 | |
| 317 | return { |
| 318 | queueId: observed.queueId!, |
| 319 | queueName: observed.queueName!, |
| 320 | accountId: acct, |
| 321 | jurisdiction, |
| 322 | ...settings, |
| 323 | }; |
| 324 | }), |
| 325 | delete: Effect.fn(function* ({ output }) { |
| 326 | // A `dev:` id means the physical resource only ever existed locally |
| 327 | // (a mis-stamped legacy row) — there is nothing to delete, and the |
| 328 | // API would reject the id as a malformed parameter. |
| 329 | if (!isLiveId(output.queueId)) return; |
| 330 | // Dependents (e.g. R2 event notification configs targeting this |
| 331 | // queue) may still be tearing down concurrently — ride out the |
| 332 | // dependency violation briefly, then fail loudly instead of |
| 333 | // silently leaking the queue. A worker producer binding |
| 334 | // (`QueueInUseByWorkerBinding`) gets a shorter window: a sibling |
| 335 | // Worker's just-processed delete/unbind takes a few seconds to |
| 336 | // propagate to the queues subsystem. |
| 337 | const attempt = queues |
| 338 | .deleteQueue({ |
| 339 | accountId: output.accountId, |
| 340 | queueId: output.queueId, |
| 341 | }) |
| 342 | .pipe( |
| 343 | Effect.retry({ |
| 344 | while: (e) => e._tag === "QueueInUseByEventNotification", |
| 345 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 346 | }), |
| 347 | Effect.retry({ |
| 348 | while: (e) => e._tag === "QueueInUseByWorkerBinding", |
| 349 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(6)]), |
| 350 | }), |
| 351 | Effect.catchTag("QueueNotFound", () => Effect.void), |
| 352 | ); |
| 353 | yield* attempt.pipe( |
| 354 | // Still pinned by a worker producer binding after the propagation |
| 355 | // window: every *tracked* worker is deleted or unbound before its |
| 356 | // queues by dependency order, so the referencing script is an |
| 357 | // orphaned generation leaked by a pre-stamping dev run (a local- |
| 358 | // stamped row whose real script was never deleted). Remove the |
| 359 | // scripts that carry this stack+stage's ownership tags and retry; |
| 360 | // a script from outside this stack+stage keeps the typed failure. |
| 361 | Effect.catchTag("QueueInUseByWorkerBinding", (cause) => |
| 362 | Effect.gen(function* () { |
| 363 | const removed = yield* deleteOwnedProducerScripts(output.accountId, output.queueId); |
| 364 | if (removed === 0) return yield* cause; |
| 365 | return yield* attempt; |
| 366 | }), |
| 367 | ), |
| 368 | ); |
| 369 | }), |
| 370 | list: Effect.fn(function* () { |
| 371 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 372 | return yield* queues.listQueues.pages({ accountId }).pipe( |
| 373 | Stream.runCollect, |
| 374 | Effect.map((chunk) => |
| 375 | Array.from(chunk).flatMap((page) => |
| 376 | (page.result ?? []) |
| 377 | .filter( |
| 378 | (q): q is typeof q & { queueId: string; queueName: string } => |
| 379 | q.queueId != null && q.queueName != null, |
| 380 | ) |
| 381 | .map((q) => toAttributes(q, accountId)), |
| 382 | ), |
| 383 | ), |
| 384 | ); |
| 385 | }), |
| 386 | read: Effect.fn(function* ({ id, output, olds, previous }) { |
| 387 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 388 | if (output?.queueId && isLiveId(output.queueId)) { |
| 389 | return yield* queues |
| 390 | .getQueue({ |
| 391 | accountId: output.accountId, |
| 392 | queueId: output.queueId, |
| 393 | }) |
| 394 | .pipe( |
| 395 | Effect.map((queue) => toAttributes(queue, output.accountId)), |
| 396 | Effect.catchTag(["QueueNotFound", "InvalidRoute"], () => Effect.undefined), |
| 397 | ); |
| 398 | } |
| 399 | const queueName = yield* createQueueName(id, olds?.name); |
| 400 | const match = yield* findQueueByName(queueName); |
| 401 | if (match && match.queueId && match.queueName) { |
| 402 | // A queue in another jurisdiction can't be converged, only |
| 403 | // replaced — and replacing it deletes whatever owns that name. |
| 404 | // Report it as foreign so the engine refuses it without `--adopt` |
| 405 | // (and still admits our own previous generation on replacement). |
| 406 | const attrs = toAttributes(match, accountId); |
| 407 | return attrs.jurisdiction !== olds?.jurisdiction && attrs.queueId !== previous?.queueId |
| 408 | ? Unowned(attrs) |
| 409 | : attrs; |
| 410 | } |
| 411 | return undefined; |
| 412 | }), |
| 413 | }); |
| 414 | |
| 415 | type ObservedQueue = { |
| 416 | queueId?: string | null; |
| 417 | queueName?: string | null; |
| 418 | jurisdiction?: string | null; |
| 419 | settings?: { |
| 420 | messageRetentionPeriod?: number | null; |
| 421 | deliveryDelay?: number | null; |
| 422 | deliveryPaused?: boolean | null; |
| 423 | } | null; |
| 424 | }; |
| 425 | |
| 426 | const DEFAULT_SETTINGS: Required<QueueSettings> = { |
| 427 | messageRetentionPeriod: 345600, |
| 428 | deliveryDelay: 0, |
| 429 | deliveryPaused: false, |
| 430 | }; |
| 431 | |
| 432 | // Cloudflare omits `jurisdiction` for a global queue and `deliveryPaused` |
| 433 | // while delivery runs. |
| 434 | const toJurisdiction = (jurisdiction: string | null | undefined) => |
| 435 | (jurisdiction || undefined) as QueueJurisdiction | undefined; |
| 436 | |
| 437 | const toSettings = (settings: ObservedQueue["settings"]): Required<QueueSettings> => ({ |
| 438 | messageRetentionPeriod: |
| 439 | settings?.messageRetentionPeriod ?? DEFAULT_SETTINGS.messageRetentionPeriod, |
| 440 | deliveryDelay: settings?.deliveryDelay ?? DEFAULT_SETTINGS.deliveryDelay, |
| 441 | deliveryPaused: settings?.deliveryPaused ?? DEFAULT_SETTINGS.deliveryPaused, |
| 442 | }); |
| 443 | |
| 444 | /** |
| 445 | * The settings to converge to: every declared setting, plus the Cloudflare |
| 446 | * default for each setting that was declared before and has been removed. |
| 447 | * Settings never declared are left as they are. |
| 448 | */ |
| 449 | const desiredSettings = ( |
| 450 | news: QueueSettings | undefined, |
| 451 | olds: QueueSettings | undefined, |
| 452 | ): QueueSettings => { |
| 453 | const desired: QueueSettings = {}; |
| 454 | for (const key of Object.keys(DEFAULT_SETTINGS) as (keyof QueueSettings)[]) { |
| 455 | const value = news?.[key] ?? (olds?.[key] !== undefined ? DEFAULT_SETTINGS[key] : undefined); |
| 456 | if (value !== undefined) (desired as Record<string, unknown>)[key] = value; |
| 457 | } |
| 458 | return desired; |
| 459 | }; |
| 460 | |
| 461 | const toAttributes = ( |
| 462 | queue: ObservedQueue & { queueId?: string | null; queueName?: string | null }, |
| 463 | accountId: string, |
| 464 | ): Queue["Attributes"] => ({ |
| 465 | queueId: queue.queueId!, |
| 466 | queueName: queue.queueName!, |
| 467 | accountId, |
| 468 | jurisdiction: toJurisdiction(queue.jurisdiction), |
| 469 | ...toSettings(queue.settings), |
| 470 | }); |
| 471 | |
| 472 | const createQueueName = (id: string, name: string | undefined) => |
| 473 | Effect.gen(function* () { |
| 474 | if (name) return name; |
| 475 | return (yield* createPhysicalName({ |
| 476 | id, |
| 477 | maxLength: 63, |
| 478 | })).toLowerCase(); |
| 479 | }); |
| 480 | |
| 481 | /** |
| 482 | * Delete the worker scripts that hold a producer binding on `queueId` AND |
| 483 | * carry this stack+stage's `alchemy:` ownership tags. Returns how many |
| 484 | * scripts were deleted. |
| 485 | * |
| 486 | * Used by the live queue delete when `QueueInUseByWorkerBinding` persists |
| 487 | * past the propagation-lag retry window: tracked workers are always |
| 488 | * deleted/unbound before their queues, so an own-stack script still |
| 489 | * binding the queue at that point is an orphaned generation (leaked by a |
| 490 | * pre-stamping dev run that rewrote the worker's row as local). Scripts |
| 491 | * without our ownership tags are left alone. |
| 492 | */ |
| 493 | const deleteOwnedProducerScripts = Effect.fn(function* (accountId: string, queueId: string) { |
| 494 | const stack = yield* Stack; |
| 495 | const queue = yield* queues |
| 496 | .getQueue({ accountId, queueId }) |
| 497 | .pipe(Effect.catchTag(["QueueNotFound", "InvalidRoute"], () => Effect.undefined)); |
| 498 | const producerScripts = Array.from( |
| 499 | new Set( |
| 500 | (queue?.producers ?? []).flatMap((producer) => |
| 501 | producer.type === "worker" && "scriptName" in producer && producer.scriptName |
| 502 | ? [producer.scriptName] |
| 503 | : [], |
| 504 | ), |
| 505 | ), |
| 506 | ); |
| 507 | let removed = 0; |
| 508 | for (const scriptName of producerScripts) { |
| 509 | const settings = yield* workers |
| 510 | .getScriptScriptAndVersionSetting({ accountId, scriptName }) |
| 511 | .pipe(Effect.catchTag(["WorkerNotFound", "WorkerHasNoVersions"], () => Effect.undefined)); |
| 512 | const tags = new Set(settings?.tags ?? []); |
| 513 | if (!tags.has(`alchemy:stack:${stack.name}`) || !tags.has(`alchemy:stage:${stack.stage}`)) { |
| 514 | continue; |
| 515 | } |
| 516 | yield* Effect.logWarning( |
| 517 | `Cloudflare Queue delete: removing orphaned worker script ` + |
| 518 | `"${scriptName}" that still binds queue ${queueId} (leaked by a ` + |
| 519 | `pre-providerMode dev run)`, |
| 520 | ); |
| 521 | yield* workers.deleteScript({ accountId, scriptName, force: true }).pipe( |
| 522 | // The orphan may also be registered as a queue consumer — detach |
| 523 | // its consumers and retry, mirroring the Worker provider's own |
| 524 | // delete recovery. |
| 525 | Effect.catchTag("QueueConsumerConflict", () => |
| 526 | detachQueueConsumersOfScript(accountId, scriptName).pipe( |
| 527 | Effect.andThen(workers.deleteScript({ accountId, scriptName, force: true })), |
| 528 | ), |
| 529 | ), |
| 530 | Effect.catchTag("WorkerNotFound", () => Effect.void), |
| 531 | ); |
| 532 | removed++; |
| 533 | } |
| 534 | return removed; |
| 535 | }); |
| 536 | |
| 537 | // Cloudflare's `listQueues` accepts no name/prefix filter, so |
| 538 | // adoption-by-name has to scan every page. Use the paginated |
| 539 | // `.items` stream off the un-yielded operation method. |
| 540 | const findQueueByName = Effect.fn(function* (queueName: string) { |
| 541 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 542 | return yield* queues.listQueues.items({ accountId }).pipe( |
| 543 | Stream.filter((q) => q.queueName === queueName), |
| 544 | Stream.runHead, |
| 545 | Effect.map(Option.getOrUndefined), |
| 546 | ); |
| 547 | }); |
| 548 | |
| 549 | export const ProviderLocal = () => |
| 550 | RpcProvider.effect( |
| 551 | Queue, |
| 552 | LOCAL_PROVIDERS_URL, |
| 553 | Effect.gen(function* () { |
| 554 | const localRuntimeState = yield* LocalRuntimeState; |
| 555 | return { |
| 556 | stables: ["accountId"], |
| 557 | diff: Effect.fn(function* ({ id, olds = {}, news = {}, output }) { |
| 558 | const accountId = yield* localAccountId; |
| 559 | if (!output?.queueId) return { action: "update" }; |
| 560 | // A real (non-`dev:`) queueId on a local-mode row is legacy damage: |
| 561 | // pre-stamping dev runs preserved the live id, which the worker |
| 562 | // binding then treats as an `Alchemy.remote()` queue and fails on |
| 563 | // the missing producer shim. Replace so the new generation mints a |
| 564 | // true local identity (delete best-effort removes the stray live |
| 565 | // queue). |
| 566 | if (isLiveId(output.queueId)) { |
| 567 | return { action: "replace" }; |
| 568 | } |
| 569 | if (!isResolved(news)) return undefined; |
| 570 | const name = yield* createQueueName(id, news.name); |
| 571 | const oldName = output?.queueName |
| 572 | ? yield* createQueueName(id, olds.name) |
| 573 | : yield* createQueueName(id, olds.name); |
| 574 | if ( |
| 575 | name !== oldName || |
| 576 | output.accountId !== accountId || |
| 577 | output.jurisdiction !== news.jurisdiction |
| 578 | ) { |
| 579 | return { action: "replace" }; |
| 580 | } |
| 581 | const settings = toSettings(news); |
| 582 | if ( |
| 583 | (Object.keys(settings) as (keyof QueueSettings)[]).some( |
| 584 | (key) => settings[key] !== output[key], |
| 585 | ) |
| 586 | ) { |
| 587 | return { action: "update" }; |
| 588 | } |
| 589 | // If the resource is a noop, add it to the local runtime state so it's available downstream. |
| 590 | // We do it here instead of in the reconcile function so it doesn't appear as an update. |
| 591 | MutableHashMap.set(localRuntimeState.queues, output.queueId, output); |
| 592 | return { action: "noop" }; |
| 593 | }), |
| 594 | read: Effect.fn(function* ({ output }) { |
| 595 | if (!output?.queueId) return undefined; |
| 596 | return MutableHashMap.get(localRuntimeState.queues, output.queueId).pipe( |
| 597 | Option.getOrUndefined, |
| 598 | ); |
| 599 | }), |
| 600 | reconcile: Effect.fn(function* ({ id, news = {}, output }) { |
| 601 | const accountId = yield* localAccountId; |
| 602 | const queue: Queue["Attributes"] = { |
| 603 | // Never carry a real (non-`dev:`) id forward onto a local row — |
| 604 | // the worker binding would treat it as an `Alchemy.remote()` |
| 605 | // queue and fail on the missing producer shim. |
| 606 | queueId: |
| 607 | output?.queueId && !isLiveId(output.queueId) ? output.queueId : generateLocalId(), |
| 608 | queueName: yield* createQueueName(id, news.name), |
| 609 | accountId: output?.accountId ?? accountId, |
| 610 | jurisdiction: news.jurisdiction, |
| 611 | ...toSettings(news), |
| 612 | }; |
| 613 | MutableHashMap.set(localRuntimeState.queues, queue.queueId, queue); |
| 614 | return queue; |
| 615 | }), |
| 616 | delete: Effect.fn(function* ({ output }) { |
| 617 | MutableHashMap.remove(localRuntimeState.queues, output.queueId); |
| 618 | // Legacy local-mode rows written before providerMode stamping can |
| 619 | // carry a real queue's id — remove the live queue too so migrating |
| 620 | // the row to a true local identity doesn't leak it. |
| 621 | if (isLiveId(output.queueId)) { |
| 622 | yield* queues |
| 623 | .deleteQueue({ |
| 624 | accountId: output.accountId, |
| 625 | queueId: output.queueId, |
| 626 | }) |
| 627 | .pipe( |
| 628 | Effect.retry({ |
| 629 | while: (e) => e._tag === "QueueInUseByEventNotification", |
| 630 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 631 | }), |
| 632 | Effect.catchTag(["QueueNotFound", "InvalidRoute"], () => Effect.void), |
| 633 | ); |
| 634 | } |
| 635 | }), |
| 636 | }; |
| 637 | }), |
| 638 | ); |
| 639 | |
| 640 | export const QueueProvider = () => |
| 641 | ProviderLayer.dual(Queue, { |
| 642 | local: () => ProviderLocal().pipe(Layer.provide(localRuntimeServices())), |
| 643 | live: () => ProviderLive(), |
| 644 | }); |