Running the scheduler inside AgentOS with automatic polling
Run the scheduler inside AgentOS with automatic schedule discovery.
This example demonstrates the primary DX for the scheduler: - Setting scheduler=True on AgentOS to enable cron polling - The poller starts automatically on app startup and stops on shutdown - Schedules are created via the REST API (POST /schedules) - The internal service token handles auth between scheduler and agent endpoints
1"""Running the scheduler inside AgentOS with automatic polling.23This example demonstrates the primary DX for the scheduler:4- Setting scheduler=True on AgentOS to enable cron polling5- The poller starts automatically on app startup and stops on shutdown6- Schedules are created via the REST API (POST /schedules)7- The internal service token handles auth between scheduler and agent endpoints89Run with:10 .venvs/demo/bin/python cookbook/05_agent_os/scheduler/scheduler_with_agentos.py11"""1213from kern.agent import Agent14from kern.db.sqlite import SqliteDb15from kern.models.openai import OpenAIChat16from kern.os import AgentOS1718# --- 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 AgentOS with scheduler enabled.37# This does three things:38# 1. Registers the /schedules REST endpoints39# 2. Starts a SchedulePoller on app startup (polls every 15s by default)40# 3. Auto-generates an internal service token for scheduler -> agent auth41agent_os = AgentOS(42 name="Scheduled OS",43 agents=[greeter, reporter],44 db=db,45 scheduler=True,46 scheduler_poll_interval=15, # seconds between poll cycles (default: 15)47 # scheduler_base_url="http://127.0.0.1:7777", # default48 # internal_service_token="my-secret", # auto-generated if omitted49)50app = agent_os.get_app()5152# --- Run the server ---53# Once running, create schedules via:54#55# curl -X POST http://127.0.0.1:7777/schedules \56# -H "Content-Type: application/json" \57# -d '{58# "name": "greet-every-5-min",59# "cron_expr": "*/5 * * * *",60# "endpoint": "/agents/greeter/runs",61# "payload": {"message": "Say hello!"}62# }'63#64# The poller will pick it up on the next poll cycle and run the agent.6566if __name__ == "__main__":67 agent_os.serve(app="scheduler_with_agentos:app", reload=True)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 scheduler_with_agentos.py