Add high-level schedule() API for scheduling functions#397
Add high-level schedule() API for scheduling functions#397flossypurse wants to merge 7 commits intomainfrom
Conversation
This commit enhances the existing schedule() method and fixes critical bugs:
Bug fixes:
- Fixed promise ID pattern to include function name (was missing)
- Fixed resonate:invoke tag to use function name instead of opts.target
- Pattern is now: `{scheduleId}.{{.timestamp}}.{functionName}`
Enhancements:
- Added schedule() method to ResonateFunc interface
- Registered functions can now call .schedule() directly
- Comprehensive documentation with examples
- Example script demonstrating all usage patterns
Examples:
// On Resonate class
await resonate.schedule("daily", "0 9 * * *", myFunc, arg1);
// On registered function
const func = resonate.register("myFunc", async (ctx, x) => x);
await func.schedule("daily", "0 9 * * *", arg1);
// With options
await resonate
.options({ timeout: 3600000, tags: { env: "prod" } })
.schedule("sync", "*/30 * * * *", syncFunc);
The implementation now matches the Python SDK's pattern and provides
a consistent developer experience across both SDKs.
Related:
- TypeScript issue: #435
- Python SDK PR: resonatehq/resonate-sdk-py#397
- Python SDK issue: resonatehq/resonate-sdk-py#328
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit implements a clean, high-level API for scheduling functions to run periodically using cron expressions. This addresses issue #328. Key features: - Simple API: resonate.schedule(id, func, cron, *args, **kwargs) - Function method: function.schedule(id, cron, *args, **kwargs) - Supports all existing options (timeout, tags, etc.) via .options() - Auto-generates promise IDs and handles encoding - Sensible defaults for timeout (24 hours) Examples: # Basic scheduling resonate.schedule("daily_report", generate_report, "0 9 * * *", user_id=123) # With options resonate.options(timeout=3600, tags={"env": "prod"}).schedule(...) # Function-level scheduling @resonate.register def report(ctx: Context, user_id: int): ... report.schedule("daily", "0 9 * * *", user_id=123) The implementation wraps the low-level schedules API and provides a developer-friendly interface similar to run() and rpc(). Includes: - Implementation in resonate.py (Resonate and Function classes) - Comprehensive test suite in tests/test_schedule_api.py - Example script demonstrating usage - README documentation with examples Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
fe83956 to
ce7549c
Compare
- Add future annotations import to example_schedule.py - Add noqa: T201 to all print statements in example - Move Schedule import to TYPE_CHECKING block in resonate.py - Fix type: ignore comments to specify arg-type rule - Use constant variable for pytest.mark.skipif condition - Fix docstrings to imperative mood Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Examples go in resonatehq-examples org, not in the SDK repo. A dedicated example app will be created there. Related to #397 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Two bugs in the Resonate.schedule() implementation: 1. resonate:invoke tag was set to the function name instead of the routing target (e.g. poll://any@default). Fixed to match how rpc() routes tasks using opts.target. 2. Encoded promise data was missing "func" and "version" fields required by the bridge to dispatch executions. Fixed to include all fields the bridge expects: func, args, kwargs, version. Both issues prevented scheduled promises from being picked up and executed by workers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Testing complete — ran the example app end-to-end against the local feature branch. Found and fixed two bugs: Bug 1: Wrong
|
The schedule API should only be accessible via the Resonate client (resonate.schedule()), not on registered Function objects. Remove Function.schedule() method and corresponding tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary
This PR implements a clean, high-level API for scheduling functions to run periodically using cron expressions. Closes #328.
✨ Now rebased on main with the new simplified client init from #386.
Problem
Previously, users had to use the low-level
schedulesAPI which required many parameters and was complex to use.Solution
This PR adds a simple, high-level
schedule()method that handles all the complexity:Key Features
resonate.schedule(id, func, cron, *args, **kwargs)function.schedule(id, cron, *args, **kwargs)for registered functions.options()for custom timeout, tags, etc.Resonate()constructor from Simplify client init, add token auth, clarify local routing #386Examples
Basic scheduling
With options
Function-level scheduling
Changes
schedule()method toResonateclassschedule()method toFunctionclassResonate()constructor (no more.local()or.remote())tests/test_schedule_api.py)example_schedule.py)Testing
The implementation has been tested with:
Run tests:
python example_schedule.py # Demonstrates all featuresBreaking Changes
None. This is a purely additive change that wraps the existing low-level API.
Related Issues
Closes #328
🤖 Generated with Claude Code