Viewing and analyzing schedule run history.
This example demonstrates.
This example demonstrates: - Creating schedules and simulating run records - Using SchedulerConsole.show_runs() for Rich-formatted run history - Querying run history with pagination - Understanding run statuses (success, failed, running, paused)
1"""Viewing and analyzing schedule run history.23This example demonstrates:4- Creating schedules and simulating run records5- Using SchedulerConsole.show_runs() for Rich-formatted run history6- Querying run history with pagination7- Understanding run statuses (success, failed, running, paused)8"""910import time11from uuid import uuid41213from kern.db.sqlite import SqliteDb14from kern.scheduler import ScheduleManager15from kern.scheduler.cli import SchedulerConsole1617# --- Setup ---1819db = SqliteDb(id="run-history-demo", db_file="tmp/run_history_demo.db")20mgr = ScheduleManager(db)21console = SchedulerConsole(mgr)2223# --- Create a schedule ---2425schedule = mgr.create(26 name="monitored-task",27 cron="*/5 * * * *",28 endpoint="/agents/monitor/runs",29 description="A schedule with run history to inspect",30 payload={"message": "Run health check"},31 max_retries=2,32 retry_delay_seconds=30,33)34print(f"Created schedule: {schedule.name} (id={schedule.id})")3536# --- Simulate some run records by inserting directly ---37# In production, the ScheduleExecutor creates these automatically.38# Here we insert them manually to demonstrate the history display.3940now = int(time.time())4142# Simulate 3 runs with different statuses43run_records = [44 {45 "id": str(uuid4()),46 "schedule_id": schedule.id,47 "attempt": 1,48 "triggered_at": now - 600,49 "completed_at": now - 590,50 "status": "success",51 "status_code": 200,52 "run_id": str(uuid4()),53 "session_id": str(uuid4()),54 "error": None,55 "created_at": now - 600,56 },57 {58 "id": str(uuid4()),59 "schedule_id": schedule.id,60 "attempt": 1,61 "triggered_at": now - 300,62 "completed_at": now - 280,63 "status": "failed",64 "status_code": 500,65 "run_id": str(uuid4()),66 "session_id": None,67 "error": "Internal server error",68 "created_at": now - 300,69 },70 {71 "id": str(uuid4()),72 "schedule_id": schedule.id,73 "attempt": 2,74 "triggered_at": now - 240,75 "completed_at": now - 230,76 "status": "success",77 "status_code": 200,78 "run_id": str(uuid4()),79 "session_id": str(uuid4()),80 "error": None,81 "created_at": now - 240,82 },83]8485for record in run_records:86 db.create_schedule_run(record)8788# --- Display run history with Rich ---8990print("\n=== Run History (Rich Table) ===\n")91console.show_runs(schedule.id)9293# --- Query runs programmatically ---9495print("\n=== Run History (Programmatic) ===\n")96runs = mgr.get_runs(schedule.id, limit=10)97print(f"Total runs: {len(runs)}")98for run in runs:99 status = run.status100 attempt = run.attempt101 error = run.error or "-"102 print(f" Attempt {attempt}: {status} (error={error})")103104# --- Pagination ---105106print("\n=== Paginated (limit=2, offset=0) ===\n")107page1 = mgr.get_runs(schedule.id, limit=2, offset=0)108print(f"Page 1: {len(page1)} runs")109110page2 = mgr.get_runs(schedule.id, limit=2, offset=2)111print(f"Page 2: {len(page2)} runs")112113# --- Cleanup ---114115mgr.delete(schedule.id)116print("\nSchedule and runs deleted.")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 run_history.py