-
Notifications
You must be signed in to change notification settings - Fork 53
Add experimental_waitUntil API for long-running tasks #296
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
Conversation
Introduces an internal keep-alive WebSocket endpoint and the experimental_waitUntil method to allow Durable Objects to remain alive while executing long-running async functions. This mechanism uses a self-connecting WebSocket with periodic pings and requires the 'enable_ctx_exports' compatibility flag. Additional handling is added to ignore keep-alive sockets in WebSocket event methods. Based on @eastlondoner's https://github.com/eastlondoner/better-wait-until
🦋 Changeset detectedLatest commit: e4d4f6d The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Replaces manual timeout tracking with a Promise.race approach for cleaner and more reliable timeout handling in the async function. This improves code clarity and ensures the function properly rejects if the timeout is reached.
| const exports = ( | ||
| this.ctx as DurableObjectState & { exports?: Record<string, unknown> } | ||
| ).exports; | ||
| if (!exports) { | ||
| throw new Error( | ||
| "waitUntil requires the 'enable_ctx_exports' compatibility flag. " + | ||
| 'Add it to your wrangler.jsonc: { "compatibility_flags": ["enable_ctx_exports"] }' | ||
| ); | ||
| } | ||
|
|
||
| const namespace = exports[this.#ParentClass.name] as | ||
| | DurableObjectNamespace | ||
| | undefined; | ||
| if (!namespace) { | ||
| throw new Error( | ||
| `Could not find namespace for ${this.#ParentClass.name} in ctx.exports. ` + | ||
| "Make sure the class name matches your Durable Object binding." | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ctx exports is fire
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the flag is actually on by default now too
Introduces an internal keep-alive WebSocket endpoint and the experimental_waitUntil method to allow Durable Objects to remain alive while executing long-running async functions. This mechanism uses a self-connecting WebSocket with periodic pings and requires the 'enable_ctx_exports' compatibility flag. Additional handling is added to ignore keep-alive sockets in WebSocket event methods.
Based on @eastlondoner's https://github.com/eastlondoner/better-wait-until