-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Added new compat flag for queue consumers and waitUntil #20952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6690332
Added new compat flag for queue consumers and waitUntil
maheshwarip fc69066
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip 22a2a05
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip f1f9a91
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip 337f0b1
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip d434926
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip 88b74a2
Apply suggestions from code review
maheshwarip f7ba832
Update src/content/compatibility-flags/queue-consumer-no-wait-waitunt…
maheshwarip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/content/compatibility-flags/queue-consumer-no-wait-waituntil.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| --- | ||
| _build: | ||
| publishResources: false | ||
| render: never | ||
| list: never | ||
|
|
||
| name: "Queue consumers don't wait for `ctx.waitUntil()` to resolve" | ||
| sort_date: "2025-03-19" | ||
| experimental: true | ||
| enable_flag: "queue_consumer_no_wait_for_wait_until" | ||
| --- | ||
|
|
||
| By default, [Queue](/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 [Queue Consumer Configuration Guide](/queues/configuration/javascript-apis#consumer). | ||
|
|
||
| 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 after the sleep function has resolved. | ||
maheshwarip marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ```js | ||
| export default { | ||
| async fetch(request, env, ctx) { | ||
| // omitted | ||
| }, | ||
|
|
||
| async queue(batch, env, ctx) { | ||
| console.log(`received batch of ${batch.messages.length} messages to queue ${batch.queue}`); | ||
| for (let i = 0; i < batch.messages.length; ++i) { | ||
| console.log(`message #${i}: ${JSON.stringify(batch.messages[i])}`); | ||
| } | ||
| ctx.waitUntil(sleep(60 * 1000)); | ||
maheshwarip marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| function sleep(ms) { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
| ``` | ||
|
|
||
| If the `queue_consumer_no_wait_for_wait_until` flag is enabled, Queue 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. | ||
maheshwarip marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
maheshwarip marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.