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.23This example demonstrates:4- Invalid cron expression handling5- Invalid timezone handling6- Duplicate schedule name handling7- Complex cron patterns (ranges, steps, lists)8- Method auto-uppercasing9"""1011from kern.db.sqlite import SqliteDb12from kern.scheduler import ScheduleManager13from kern.scheduler.cli import SchedulerConsole1415# --- Setup ---1617db = SqliteDb(id="validation-demo", db_file="tmp/validation_demo.db")18mgr = ScheduleManager(db)1920# =============================================================================21# 1. Invalid cron expression22# =============================================================================2324print("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}")2930# =============================================================================31# 2. Invalid timezone32# =============================================================================3334print("\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}")3940# =============================================================================41# 3. Duplicate schedule name42# =============================================================================4344print("\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)5152# =============================================================================53# 4. Complex cron patterns54# =============================================================================5556print("\n4. Complex cron patterns:")5758# Every 5 minutes59s1 = mgr.create(name="every-5-min", cron="*/5 * * * *", endpoint="/test")60print(f" */5 * * * * -> Created: {s1.name}")6162# Weekdays 9-1763s2 = mgr.create(name="business-hours", cron="0 9-17 * * 1-5", endpoint="/test")64print(f" 0 9-17 * * 1-5 -> Created: {s2.name}")6566# First day of month at midnight67s3 = mgr.create(name="monthly-report", cron="0 0 1 * *", endpoint="/test")68print(f" 0 0 1 * * -> Created: {s3.name}")6970# =============================================================================71# 5. Method auto-uppercasing72# =============================================================================7374print("\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}'")7980# =============================================================================81# 6. Display all valid schedules82# =============================================================================8384print()85console = SchedulerConsole(mgr)86console.show_schedules()8788# =============================================================================89# Cleanup90# =============================================================================9192for s in [s1, s2, s3, s4]:93 mgr.delete(s.id)94print("All schedules cleaned up.")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 schedule_validation.py