1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! 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
//!
//! ```text
//! ┌──────────────────────────────────────┐
//! │ 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
//!
//! ```rust,no_run
//! use tokio::sync::watch;
//! use zeph_scheduler::{JobStore, Scheduler, ScheduledTask, TaskKind};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // 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;
//! # Ok(())
//! # }
//! ```
//!
//! # 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`].
pub use sql;
pub use SchedulerError;
pub use CustomTaskHandler;
pub use sanitize_task_prompt;
pub use ;
pub use ;
pub use ;
pub use ;
pub use PidFile;
pub use UpdateCheckHandler;