Running the scheduler inside AgentOS with programmatic schedule creation
Run the scheduler inside AgentOS with programmatic schedule creation.
This example demonstrates: - Setting scheduler=True on AgentOS to enable cron polling - Using ScheduleManager to create schedules directly (no curl needed) - The poller starts automatically on app startup and executes due schedules
1"""Running the scheduler inside AgentOS with programmatic schedule creation.23This example demonstrates:4- Setting scheduler=True on AgentOS to enable cron polling5- Using ScheduleManager to create schedules directly (no curl needed)6- The poller starts automatically on app startup and executes due schedules78Run with:9 .venvs/demo/bin/python cookbook/05_agent_os/scheduler/demo.py10"""1112from kern.agent import Agent13from kern.db.sqlite import SqliteDb14from kern.models.openai import OpenAIChat15from kern.os import AgentOS16from kern.scheduler import ScheduleManager1718# --- Setup ---1920db = SqliteDb(id="scheduler-os-demo", db_file="tmp/scheduler_os_demo.db")2122greeter = Agent(23 name="Greeter",24 model=OpenAIChat(id="gpt-4o-mini"),25 instructions=["You are a friendly greeter."],26 db=db,27)2829reporter = Agent(30 name="Reporter",31 model=OpenAIChat(id="gpt-4o-mini"),32 instructions=["You summarize news headlines in 2-3 sentences."],33 db=db,34)3536# --- Create schedules programmatically ---3738mgr = ScheduleManager(db)3940# Create a schedule for the greeter agent (every 5 minutes)41greet_schedule = mgr.create(42 name="greet-every-5-min",43 cron="* * * * *",44 endpoint="/agents/greeter/runs",45 payload={"message": "Say hello!"},46 description="Greet every 5 minutes",47 if_exists="update",48)49print(f"Schedule ready: {greet_schedule.name} (next run: {greet_schedule.next_run_at})")5051# Create a schedule for the reporter agent (daily at 9 AM)52report_schedule = mgr.create(53 name="daily-news-report",54 cron="* * * * *",55 endpoint="/agents/reporter/runs",56 payload={"message": "Summarize today's top headlines."},57 description="Daily news summary at 9 AM UTC",58 if_exists="update",59)60print(61 f"Schedule ready: {report_schedule.name} (next run: {report_schedule.next_run_at})"62)6364# --- Create AgentOS with scheduler enabled ---6566agent_os = AgentOS(67 name="Scheduled OS",68 agents=[greeter, reporter],69 db=db,70 scheduler=True,71 scheduler_poll_interval=15,72)7374# --- Run the server ---75# The poller will automatically pick up the schedules created above.7677if __name__ == "__main__":78 import uvicorn7980 uvicorn.run(agent_os.get_app(), host="0.0.0.0", port=7777)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 demo.py