Skip to content

Commit 85fc4b8

Browse files
dahliacodex
andcommitted
Document Workers ordering key handling
Explain the extra queue consumer step needed for ordering keys on Cloudflare Workers. The manual, deployment guide, and package README now point users to orderingKv and WorkersMessageQueue.processMessage(). AI-assisted with Codex for drafting and editing the documentation changes; reviewed in the workspace. Co-Authored-By: OpenAI Codex <noreply@openai.com>
1 parent d827be4 commit 85fc4b8

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

docs/manual/deploy.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ export default {
303303
};
304304
~~~~
305305

306+
If you use queue ordering keys on Cloudflare Workers, instantiate
307+
`WorkersMessageQueue` with an `orderingKv` namespace and call
308+
`WorkersMessageQueue.processMessage()` before
309+
`Federation.processQueuedTask()`. See the
310+
[*`WorkersMessageQueue`* section](./mq.md#workersmessagequeue-cloudflare-workers-only)
311+
for a complete example and caveats about best-effort ordering.
312+
306313
### Example deployment
307314

308315
For a complete working example, see the [Cloudflare Workers example]

docs/manual/mq.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,56 @@ export default {
604604
> process the messages. The `queue()` method is the only way to consume
605605
> messages from the queue in Cloudflare Workers.
606606
607+
> [!NOTE]
608+
> If you use `~MessageQueueEnqueueOptions.orderingKey` with
609+
> `WorkersMessageQueue`, you also need to provide a KV namespace for ordering
610+
> locks and pass each raw queue message through
611+
> `~WorkersMessageQueue.processMessage()` before calling
612+
> `Federation.processQueuedTask()`. Otherwise, the ordering key is embedded in
613+
> the message, but not enforced when the worker consumes it.
614+
>
615+
> ~~~~ typescript
616+
> import { createFederationBuilder, type Message } from "@fedify/fedify";
617+
> import { WorkersKvStore, WorkersMessageQueue } from "@fedify/cfworkers";
618+
>
619+
> type Env = {
620+
> KV_NAMESPACE: KVNamespace<string>;
621+
> QUEUE_BINDING: Queue;
622+
> ORDERING_KV: KVNamespace<string>;
623+
> };
624+
>
625+
> const builder = createFederationBuilder<Env>();
626+
>
627+
> export default {
628+
> async queue(batch: MessageBatch<unknown>, env: Env): Promise<void> {
629+
> const queue = new WorkersMessageQueue(env.QUEUE_BINDING, {
630+
> orderingKv: env.ORDERING_KV,
631+
> });
632+
> const federation = await builder.build({
633+
> kv: new WorkersKvStore(env.KV_NAMESPACE),
634+
> queue,
635+
> });
636+
>
637+
> for (const message of batch.messages) {
638+
> const result = await queue.processMessage(message.body);
639+
> if (!result.shouldProcess) {
640+
> message.retry();
641+
> continue;
642+
> }
643+
> try {
644+
> await federation.processQueuedTask(
645+
> env,
646+
> result.message as Message,
647+
> );
648+
> message.ack();
649+
> } finally {
650+
> await result.release?.();
651+
> }
652+
> }
653+
> },
654+
> };
655+
> ~~~~
656+
607657
[Cloudflare Workers]: https://workers.cloudflare.com/
608658
[Cloudflare Queues]: https://developers.cloudflare.com/queues/
609659

packages/cfworkers/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ management.
104104
> process the messages. The `queue()` method is the only way to consume
105105
> messages from the queue in Cloudflare Workers.
106106
107+
> [!NOTE]
108+
> If you use `orderingKey` when enqueueing messages, construct
109+
> `WorkersMessageQueue` with an `orderingKv` namespace and pass each raw queue
110+
> message through `WorkersMessageQueue.processMessage()` before calling
111+
> `Federation.processQueuedTask()`. This acquires and releases the best-effort
112+
> ordering lock for that key. You can also customize the lock behavior with
113+
> the `orderingKeyPrefix` and `orderingLockTtl` options.
114+
107115
[Cloudflare Queues]: https://developers.cloudflare.com/queues/
108116

109117

0 commit comments

Comments
 (0)