Skip to content

Commit 5daab6e

Browse files
maheshwariphyperlint-ai[bot]OxyjunGregBrimble
authored
Added new compat flag for queue consumers and waitUntil (#20952)
* Added new compat flag for queue consumers and waitUntil * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: Jun Lee <[email protected]> * Apply suggestions from code review Co-authored-by: Jun Lee <[email protected]> * Update src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md Co-authored-by: Greg Brimble <[email protected]> --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> Co-authored-by: Jun Lee <[email protected]> Co-authored-by: Greg Brimble <[email protected]>
1 parent cdf3560 commit 5daab6e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
_build:
3+
publishResources: false
4+
render: never
5+
list: never
6+
7+
name: "Queue consumers don't wait for `ctx.waitUntil()` to resolve"
8+
sort_date: "2025-03-19"
9+
experimental: true
10+
enable_flag: "queue_consumer_no_wait_for_wait_until"
11+
---
12+
13+
By default, [Queues](/queues/) Consumer Workers acknowledge messages only after promises passed to [`ctx.waitUntil()`](/workers/runtime-apis/context) have resolved. This behavior can cause queue consumers which utilize `ctx.waitUntil()` to process messages slowly. The default behavior is documented in the [Queues Consumer Configuration Guide](/queues/configuration/javascript-apis#consumer).
14+
15+
This Consumer Worker is an example of a Worker which utilizes `ctx.waitUntil()`. Under the default behavior, this consumer Worker will only acknowledge a batch of messages after the sleep function has resolved.
16+
```js
17+
export default {
18+
async fetch(request, env, ctx) {
19+
// omitted
20+
},
21+
22+
async queue(batch, env, ctx) {
23+
console.log(`received batch of ${batch.messages.length} messages to queue ${batch.queue}`);
24+
for (let i = 0; i < batch.messages.length; ++i) {
25+
console.log(`message #${i}: ${JSON.stringify(batch.messages[i])}`);
26+
}
27+
ctx.waitUntil(sleep(30 * 1000));
28+
}
29+
};
30+
31+
function sleep(ms) {
32+
return new Promise(resolve => setTimeout(resolve, ms));
33+
}
34+
```
35+
36+
If the `queue_consumer_no_wait_for_wait_until` flag is enabled, Queues consumers will no longer wait for promises passed to `ctx.waitUntil()` to resolve before acknowledging messages. This can improve the performance of queue consumers which utilize `ctx.waitUntil()`. With the flag enabled, in the above example, the consumer Worker will acknowledge the batch without waiting for the sleep function to resolve.
37+
38+
Using this flag will not affect the behavior of `ctx.waitUntil()`. `ctx.waitUntil()` will continue to extend the lifetime of your consumer Worker to continue to work even after the batch of messages has been acknowledged.

0 commit comments

Comments
 (0)