Expand description
Cron-based periodic task scheduler with SQLite persistence.
zeph-scheduler drives time-based work inside the Zeph agent. It supports two
task execution modes:
- Periodic — tasks defined by a cron expression and re-scheduled after each run.
- One-shot — tasks that run once at a specific
DateTime<Utc>and are removed on completion.
§Architecture
┌──────────────────────────────────────┐
│ Scheduler │
│ tasks: Vec<ScheduledTask> │
│ handlers: HashMap<kind, TaskHandler>│
│ store: JobStore (SQLite) │
│ shutdown_rx: watch::Receiver<bool> │
│ task_rx: mpsc::Receiver<Msg> │
└───────────┬──────────────────────────┘
│ tick() every N seconds
▼
for each due task → handler.execute()
│
▼
store.record_run() / store.mark_done()§Quick Start
use tokio::sync::watch;
use zeph_scheduler::{JobStore, Scheduler, ScheduledTask, TaskKind};
// 1. Open the job store.
let store = JobStore::open("sqlite:scheduler.db").await?;
// 2. Create the scheduler.
let (_shutdown_tx, shutdown_rx) = watch::channel(false);
let (mut scheduler, _msg_tx) = Scheduler::new(store, shutdown_rx);
// 3. Register a periodic task (every minute).
let task = ScheduledTask::new(
"health-check",
"*/1 * * * *",
TaskKind::HealthCheck,
serde_json::Value::Null,
)?;
scheduler.add_task(task);
// 4. Initialise persistence and run.
scheduler.init().await?;
scheduler.run().await;§Cron Expression Format
The scheduler accepts both 5-field (min hour day month weekday) and
6-field (sec min hour day month weekday) cron expressions. Five-field
expressions are automatically normalised by prepending "0 " (seconds = 0).
Use normalize_cron_expr directly if you need the canonical form.
§Shutdown
Send true on the watch::Sender<bool> that was passed to Scheduler::new
to trigger a graceful shutdown. The scheduler loop exits on the next tick.
§SQLite Persistence
All job definitions and run history are stored in a SQLite database managed by
zeph-db migrations. Use JobStore::open to connect or JobStore::new when
you already hold a zeph_db::DbPool.
Re-exports§
pub use update_check::UpdateCheckHandler;
Modules§
Structs§
- Custom
Task Handler TaskHandlerthat injects a custom prompt into the agent loop.- JobStore
- Persistent storage layer for scheduled jobs.
- Scheduled
Task - A task held in memory by the
crate::Scheduler. - Scheduled
Task Info - Full details for a scheduled task, returned by
JobStore::list_jobs_full. - Scheduled
Task Record - A scheduled task row returned by
JobStore::list_jobs. - Scheduler
- Cron-based periodic task scheduler.
- Task
Descriptor - Descriptor sent over the control channel to register tasks at runtime.
Enums§
- Scheduler
Error - Errors that can occur inside the scheduler subsystem.
- Scheduler
Message - Messages sent to the
Schedulerover its control channel. - Task
Kind - Identifies what type of work a scheduled task performs.
- Task
Mode - Execution mode for a scheduled task.
Traits§
- Task
Handler - Trait for types that can execute a scheduled task.
Functions§
- normalize_
cron_ expr - Normalise a cron expression to the 6-field format required by the
croncrate. - sanitize_
task_ prompt - Sanitise a user-supplied task prompt before injecting it into the agent loop.