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.
2
3This example demonstrates:
4- Creating schedules and simulating run records
5- Using SchedulerConsole.show_runs() for Rich-formatted run history
6- Querying run history with pagination
7- Understanding run statuses (success, failed, running, paused)
8"""
9
10import time
11from uuid import uuid4
12
13from kern.db.sqlite import SqliteDb
14from kern.scheduler import ScheduleManager
15from kern.scheduler.cli import SchedulerConsole
16
17# --- Setup ---
18
19db = SqliteDb(id="run-history-demo", db_file="tmp/run_history_demo.db")
20mgr = ScheduleManager(db)
21console = SchedulerConsole(mgr)
22
23# --- Create a schedule ---
24
25schedule = 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})")
35
36# --- 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.
39
40now = int(time.time())
41
42# Simulate 3 runs with different statuses
43run_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]
84
85for record in run_records:
86 db.create_schedule_run(record)
87
88# --- Display run history with Rich ---
89
90print("\n=== Run History (Rich Table) ===\n")
91console.show_runs(schedule.id)
92
93# --- Query runs programmatically ---
94
95print("\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.status
100 attempt = run.attempt
101 error = run.error or "-"
102 print(f" Attempt {attempt}: {status} (error={error})")
103
104# --- Pagination ---
105
106print("\n=== Paginated (limit=2, offset=0) ===\n")
107page1 = mgr.get_runs(schedule.id, limit=2, offset=0)
108print(f"Page 1: {len(page1)} runs")
109
110page2 = mgr.get_runs(schedule.id, limit=2, offset=2)
111print(f"Page 2: {len(page2)} runs")
112
113# --- Cleanup ---
114
115mgr.delete(schedule.id)
116print("\nSchedule and runs deleted.")

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 run_history.py