Skip to content

Support existing Cloudflare workers for queue and cron#6935

Open
c-w-xiaohei wants to merge 1 commit into
anomalyco:devfrom
c-w-xiaohei:fix/queue-cron-creation
Open

Support existing Cloudflare workers for queue and cron#6935
c-w-xiaohei wants to merge 1 commit into
anomalyco:devfrom
c-w-xiaohei:fix/queue-cron-creation

Conversation

@c-w-xiaohei

Copy link
Copy Markdown

Summary

Allow Cloudflare Queue consumers and Cron triggers to attach to an existing sst.cloudflare.Worker.

const queue = new sst.cloudflare.Queue("Queue");

const worker = new sst.cloudflare.Worker("ServerWorker", {
  handler: "worker.ts",
  link: [queue],
});

queue.subscribe(worker);

new sst.cloudflare.Cron("Cron", {
  worker,
  schedules: ["*/5 * * * *"],
});

Existing behavior is unchanged:

  • queue.subscribe("consumer.ts") still creates a consumer Worker.
  • queue.subscribe({ handler: "consumer.ts" }) still creates a consumer Worker.
  • new sst.cloudflare.Cron(..., { worker: "cron.ts" }) still creates a Worker.
  • Deprecated job remains supported as before.

Why

Cloudflare Workers can handle fetch, queue, and scheduled events from the same script. Cloudflare Queue producer bindings and consumer attachments are separate: producers bind a queue to send messages, while a consumer points at a Worker scriptName. Cron triggers also point at a Worker script.

SST already supports the default separated-worker flow by accepting a handler or WorkerArgs and creating a Worker for Queue.subscribe() and Cron. That remains the default.

However, SST previously had no way to attach a Queue consumer or Cron trigger to a Worker created earlier in the same app. Users wanting one Worker to serve HTTP traffic, publish messages, process queue batches, and handle schedules had to create raw cloudflare.QueueConsumer and cloudflare.WorkersCronTrigger resources themselves.

Design

This is intentionally scoped to direct sst.cloudflare.Worker instances.

  • QueueWorkerSubscriber uses the existing Worker's nodes.worker for the consumer scriptName.
  • Cron uses the existing Worker's nodes.worker for the trigger scriptName.
  • Handler strings and WorkerArgs still use the existing workerBuilder path.
  • workerBuilder remains responsible only for creating Workers from source definitions.
  • transform.worker is rejected for an existing Queue subscriber Worker because no new Worker exists to transform.

This PR does not add raw scriptName support, Worker.get(), public QueueConsumer/CronTrigger components, or new deprecations.

Testing

Passed:

  • bun run --cwd platform test test/components/cloudflare-existing-worker.test.ts
  • bun run typecheck:platform
  • bun run build:platform

bun run --cwd platform test still has unrelated existing/environment failures resolving @types/node in the ALB/Bucket/Service tests, plus ERR_TRACE_EVENTS_UNAVAILABLE unhandled errors from Router WAF tests. The new Cloudflare existing Worker test passes in that full run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant