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.
2
3This example demonstrates:
4- Using acreate(), alist(), aget(), aupdate(), adelete() for async CRUD
5- Using aenable() and adisable() to toggle schedules
6- Using aget_runs() to list run history
7- Rich-formatted display with SchedulerConsole
8"""
9
10import asyncio
11
12from kern.db.sqlite import SqliteDb
13from kern.scheduler import ScheduleManager
14from kern.scheduler.cli import SchedulerConsole
15
16
17async 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)
22
23 # --- 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})")
32
33 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})")
41
42 # --- List all schedules ---
43 all_schedules = await mgr.alist()
44 print(f"\nTotal schedules: {len(all_schedules)}")
45
46 # --- Get by ID ---
47 fetched = await mgr.aget(s1.id)
48 print(f"Fetched: {fetched.name}")
49
50 # --- Update ---
51 updated = await mgr.aupdate(s1.id, description="Updated morning report description")
52 print(f"Updated description: {updated.description}")
53
54 # --- 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}")
58
59 await mgr.aenable(s2.id)
60 enabled = await mgr.aget(s2.id)
61 print(f"{enabled.name} enabled={enabled.enabled}")
62
63 # --- 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)}")
66
67 # --- Display with Rich ---
68 print()
69 console.show_schedules()
70
71 # --- Cleanup ---
72 await mgr.adelete(s1.id)
73 await mgr.adelete(s2.id)
74 print("\nAll schedules deleted.")
75
76
77if __name__ == "__main__":
78 asyncio.run(main())

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/05_agent_os/scheduler
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python async_schedule.py