Async schedule management using the async ScheduleManager API.
This example demonstrates.
This example demonstrates: - Using acreate(), alist(), aget(), aupdate(), adelete() for async CRUD - Using aenable() and adisable() to toggle schedules - Using aget_runs() to list run history - Rich-formatted display with SchedulerConsole
1"""Async schedule management using the async ScheduleManager API.23This example demonstrates:4- Using acreate(), alist(), aget(), aupdate(), adelete() for async CRUD5- Using aenable() and adisable() to toggle schedules6- Using aget_runs() to list run history7- Rich-formatted display with SchedulerConsole8"""910import asyncio1112from kern.db.sqlite import SqliteDb13from kern.scheduler import ScheduleManager14from kern.scheduler.cli import SchedulerConsole151617async def main():18 # --- Setup ---19 db = SqliteDb(id="async-scheduler-demo", db_file="tmp/async_scheduler_demo.db")20 mgr = ScheduleManager(db)21 console = SchedulerConsole(mgr)2223 # --- Create schedules asynchronously ---24 s1 = await mgr.acreate(25 name="async-morning-report",26 cron="0 8 * * *",27 endpoint="/agents/async-agent/runs",28 description="Morning report via async API",29 payload={"message": "Generate the morning report"},30 )31 print(f"Created: {s1.name} (id={s1.id})")3233 s2 = await mgr.acreate(34 name="async-evening-summary",35 cron="0 18 * * *",36 endpoint="/agents/async-agent/runs",37 description="Evening summary via async API",38 payload={"message": "Summarize the day"},39 )40 print(f"Created: {s2.name} (id={s2.id})")4142 # --- List all schedules ---43 all_schedules = await mgr.alist()44 print(f"\nTotal schedules: {len(all_schedules)}")4546 # --- Get by ID ---47 fetched = await mgr.aget(s1.id)48 print(f"Fetched: {fetched.name}")4950 # --- Update ---51 updated = await mgr.aupdate(s1.id, description="Updated morning report description")52 print(f"Updated description: {updated.description}")5354 # --- Disable and re-enable ---55 await mgr.adisable(s2.id)56 disabled = await mgr.aget(s2.id)57 print(f"\n{disabled.name} enabled={disabled.enabled}")5859 await mgr.aenable(s2.id)60 enabled = await mgr.aget(s2.id)61 print(f"{enabled.name} enabled={enabled.enabled}")6263 # --- Check runs (none yet, since we haven't executed) ---64 runs = await mgr.aget_runs(s1.id)65 print(f"\nRuns for {s1.name}: {len(runs)}")6667 # --- Display with Rich ---68 print()69 console.show_schedules()7071 # --- Cleanup ---72 await mgr.adelete(s1.id)73 await mgr.adelete(s2.id)74 print("\nAll schedules deleted.")757677if __name__ == "__main__":78 asyncio.run(main())Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/05_agent_os/scheduler45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python async_schedule.py