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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use argentor_core::{ArgentorError, ArgentorResult};
use chrono::{DateTime, Utc};
use cron::Schedule;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
/// A single scheduled job definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduledJob {
/// Job name for identification.
pub name: String,
/// Cron expression defining the schedule.
pub cron_expression: String,
/// Description of the task to execute.
pub task_description: String,
/// Whether the job is currently active.
#[serde(default = "default_enabled")]
pub enabled: bool,
}
fn default_enabled() -> bool {
true
}
/// Cron-based scheduler that manages a collection of [`ScheduledJob`]s.
///
/// The scheduler can compute next fire times, filter enabled jobs, and run a
/// background loop that logs when jobs should fire.
#[derive(Debug)]
pub struct Scheduler {
jobs: Vec<ScheduledJob>,
}
impl Scheduler {
/// Create a new scheduler with the given jobs.
pub fn new(jobs: Vec<ScheduledJob>) -> Self {
Self { jobs }
}
/// Parse a cron expression string into a [`cron::Schedule`].
///
/// Uses the 7-field cron format: sec min hour day-of-month month day-of-week year.
pub fn parse_cron(cron_expr: &str) -> ArgentorResult<Schedule> {
Schedule::from_str(cron_expr).map_err(|e| {
ArgentorError::Config(format!("Invalid cron expression '{cron_expr}': {e}"))
})
}
/// Compute the next fire time for a given cron expression.
///
/// Returns the first upcoming `DateTime<Utc>` after `Utc::now()`, or an error
/// if the expression is invalid or has no upcoming times.
pub fn next_fire_time(cron_expr: &str) -> ArgentorResult<DateTime<Utc>> {
let schedule = Self::parse_cron(cron_expr)?;
schedule.upcoming(Utc).next().ok_or_else(|| {
ArgentorError::Config(format!(
"Cron expression '{cron_expr}' has no upcoming fire times"
))
})
}
/// Return references to only the enabled jobs.
pub fn enabled_jobs(&self) -> Vec<&ScheduledJob> {
self.jobs.iter().filter(|j| j.enabled).collect()
}
/// Return the total number of jobs (enabled and disabled).
pub fn job_count(&self) -> usize {
self.jobs.len()
}
/// Start the scheduler background loop.
///
/// Spawns a tokio task that continuously:
/// 1. Computes the next fire time for each enabled job.
/// 2. Sleeps until the nearest fire time.
/// 3. Logs every job whose fire time falls within a 1-second tolerance window.
///
/// Returns the [`tokio::task::JoinHandle`] so the caller can abort or await it.
///
/// Note: actual agent/task execution is intentionally not wired here to avoid
/// circular dependencies with `argentor-agent`. The `tracing::info!` log acts as
/// a hook point where execution can be integrated downstream.
pub async fn start(self) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
loop {
let enabled: Vec<&ScheduledJob> = self.jobs.iter().filter(|j| j.enabled).collect();
if enabled.is_empty() {
tracing::info!("Scheduler: no enabled jobs, sleeping 60s");
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
continue;
}
// Compute next fire time for each enabled job.
let mut nearest: Option<DateTime<Utc>> = None;
let mut job_times: Vec<(&ScheduledJob, DateTime<Utc>)> = Vec::new();
for job in &enabled {
match Self::next_fire_time(&job.cron_expression) {
Ok(next) => {
job_times.push((job, next));
nearest = Some(match nearest {
Some(cur) if next < cur => next,
Some(cur) => cur,
None => next,
});
}
Err(e) => {
tracing::warn!(
"Scheduler: skipping job '{}' due to cron error: {}",
job.name,
e
);
}
}
}
let nearest = match nearest {
Some(t) => t,
None => {
tracing::warn!(
"Scheduler: all enabled jobs have invalid cron expressions, sleeping 60s"
);
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
continue;
}
};
// Sleep until the nearest fire time.
let now = Utc::now();
if nearest > now {
let wait = (nearest - now).to_std().unwrap_or_default();
tracing::info!("Scheduler: sleeping for {:?} until next job", wait);
tokio::time::sleep(wait).await;
}
// Fire all jobs within a 1-second tolerance window.
let fire_threshold = Utc::now() + chrono::Duration::seconds(1);
for (job, fire_time) in &job_times {
if *fire_time <= fire_threshold {
tracing::info!(
"Scheduler: firing job '{}' — {}",
job.name,
job.task_description
);
}
}
}
})
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_parse_valid_cron() {
let result = Scheduler::parse_cron("0 * * * * * *"); // every minute
assert!(result.is_ok());
}
#[test]
fn test_parse_invalid_cron() {
let result = Scheduler::parse_cron("not a cron expression");
assert!(result.is_err());
}
#[test]
fn test_next_fire_time_is_future() {
let next = Scheduler::next_fire_time("0 * * * * * *").unwrap();
assert!(next > Utc::now());
}
#[test]
fn test_enabled_jobs_filter() {
let jobs = vec![
ScheduledJob {
name: "active".into(),
cron_expression: "0 * * * * * *".into(),
task_description: "do stuff".into(),
enabled: true,
},
ScheduledJob {
name: "inactive".into(),
cron_expression: "0 * * * * * *".into(),
task_description: "do other stuff".into(),
enabled: false,
},
];
let scheduler = Scheduler::new(jobs);
assert_eq!(scheduler.enabled_jobs().len(), 1);
assert_eq!(scheduler.enabled_jobs()[0].name, "active");
}
#[test]
fn test_job_count() {
let scheduler = Scheduler::new(vec![ScheduledJob {
name: "j1".into(),
cron_expression: "0 * * * * * *".into(),
task_description: "task".into(),
enabled: true,
}]);
assert_eq!(scheduler.job_count(), 1);
}
}