Schedule validation and error handling.

This example demonstrates.

This example demonstrates: - Invalid cron expression handling - Invalid timezone handling - Duplicate schedule name handling - Complex cron patterns (ranges, steps, lists) - Method auto-uppercasing

1"""Schedule validation and error handling.
2
3This example demonstrates:
4- Invalid cron expression handling
5- Invalid timezone handling
6- Duplicate schedule name handling
7- Complex cron patterns (ranges, steps, lists)
8- Method auto-uppercasing
9"""
10
11from kern.db.sqlite import SqliteDb
12from kern.scheduler import ScheduleManager
13from kern.scheduler.cli import SchedulerConsole
14
15# --- Setup ---
16
17db = SqliteDb(id="validation-demo", db_file="tmp/validation_demo.db")
18mgr = ScheduleManager(db)
19
20# =============================================================================
21# 1. Invalid cron expression
22# =============================================================================
23
24print("1. Invalid cron expression:")
25try:
26 mgr.create(name="bad-cron", cron="not valid", endpoint="/test")
27except ValueError as e:
28 print(f" Caught ValueError: {e}")
29
30# =============================================================================
31# 2. Invalid timezone
32# =============================================================================
33
34print("\n2. Invalid timezone:")
35try:
36 mgr.create(name="bad-tz", cron="0 9 * * *", endpoint="/test", timezone="Fake/Zone")
37except ValueError as e:
38 print(f" Caught ValueError: {e}")
39
40# =============================================================================
41# 3. Duplicate schedule name
42# =============================================================================
43
44print("\n3. Duplicate schedule name:")
45s = mgr.create(name="unique-schedule", cron="0 9 * * *", endpoint="/test")
46try:
47 mgr.create(name="unique-schedule", cron="0 10 * * *", endpoint="/test")
48except ValueError as e:
49 print(f" Caught ValueError: {e}")
50mgr.delete(s.id)
51
52# =============================================================================
53# 4. Complex cron patterns
54# =============================================================================
55
56print("\n4. Complex cron patterns:")
57
58# Every 5 minutes
59s1 = mgr.create(name="every-5-min", cron="*/5 * * * *", endpoint="/test")
60print(f" */5 * * * * -> Created: {s1.name}")
61
62# Weekdays 9-17
63s2 = mgr.create(name="business-hours", cron="0 9-17 * * 1-5", endpoint="/test")
64print(f" 0 9-17 * * 1-5 -> Created: {s2.name}")
65
66# First day of month at midnight
67s3 = mgr.create(name="monthly-report", cron="0 0 1 * *", endpoint="/test")
68print(f" 0 0 1 * * -> Created: {s3.name}")
69
70# =============================================================================
71# 5. Method auto-uppercasing
72# =============================================================================
73
74print("\n5. Method auto-uppercasing:")
75s4 = mgr.create(
76 name="lowercase-method", cron="0 9 * * *", endpoint="/test", method="get"
77)
78print(f" Input: 'get' -> Stored: '{s4.method}'")
79
80# =============================================================================
81# 6. Display all valid schedules
82# =============================================================================
83
84print()
85console = SchedulerConsole(mgr)
86console.show_schedules()
87
88# =============================================================================
89# Cleanup
90# =============================================================================
91
92for s in [s1, s2, s3, s4]:
93 mgr.delete(s.id)
94print("All schedules cleaned up.")

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