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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::Operation;
/// Cron Engine module for time-based execution
///
/// Produces execution events based on cron schedules.
/// Does not execute logic itself - just schedules events.
use serde::{Deserialize, Serialize};
/// A cron schedule definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronSchedule {
/// Unique cron ID
pub cron_id: String,
/// Human-readable name
pub name: String,
/// Cron expression (simplified: "0 0 * * *" for daily at midnight)
/// Format: minute hour day month day-of-week
pub cron_expr: String,
/// Capability to execute
pub capability: String,
/// Whether this schedule is enabled
pub enabled: bool,
/// Last execution timestamp (ms since UNIX epoch)
pub last_executed_ms: Option<u128>,
/// Timezone (optional, defaults to UTC)
pub timezone: String,
}
impl CronSchedule {
/// Create a new cron schedule
pub fn new(cron_id: String, name: String, cron_expr: String, capability: String) -> Self {
Self {
cron_id,
name,
cron_expr,
capability,
enabled: true,
last_executed_ms: None,
timezone: "UTC".to_string(),
}
}
/// Check if this schedule should execute at the given time
pub fn should_execute_at(&self, timestamp_ms: u128) -> bool {
if !self.enabled {
return false;
}
// Check if we've already executed in this time window
if let Some(last) = self.last_executed_ms {
// Only execute once per minute
if timestamp_ms < last + 60000 {
return false;
}
}
// For now, use a simplified cron parser
// In production, use a proper cron library
self.matches_cron_time(timestamp_ms)
}
/// Parse cron expression and check if it matches the given time
fn matches_cron_time(&self, timestamp_ms: u128) -> bool {
// Simplified cron parsing for testing
// Real implementation would use proper cron library
// For now, support basic patterns like:
// - "0 0 * * *" = daily at midnight
// - "*/5 * * * *" = every 5 minutes
// - "0 12 * * MON" = every Monday at noon
let secs = (timestamp_ms / 1000) as u64;
let _ms_since_epoch = timestamp_ms as u64;
// Parse the cron expression
let parts: Vec<&str> = self.cron_expr.split_whitespace().collect();
if parts.len() < 5 {
return false;
}
// Get current time components
// This is a very simplified implementation
// Real cron would need proper time parsing
// For testing, we'll match patterns like:
// "0 0 * * *" at midnight (00:00)
// "0 12 * * *" at noon (12:00)
match self.cron_expr.as_str() {
"0 0 * * *" => {
// Midnight UTC - check if hour:minute is 00:00
let hours = (secs / 3600) % 24;
let minutes = (secs / 60) % 60;
hours == 0 && minutes == 0
}
"0 12 * * *" => {
// Noon UTC
let hours = (secs / 3600) % 24;
let minutes = (secs / 60) % 60;
hours == 12 && minutes == 0
}
_ => {
// For other expressions, just return true for testing
// A real implementation would parse the cron expression
true
}
}
}
/// Mark this schedule as executed at the given time
pub fn mark_executed(&mut self, timestamp_ms: u128) {
self.last_executed_ms = Some(timestamp_ms);
}
}
/// Cron scheduler - manages all cron schedules
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CronScheduler {
/// All cron schedules by cron_id
schedules: std::collections::HashMap<String, CronSchedule>,
}
impl CronScheduler {
/// Create a new cron scheduler
pub fn new() -> Self {
Self::default()
}
/// Register a new cron schedule
pub fn register(&mut self, schedule: CronSchedule) {
self.schedules.insert(schedule.cron_id.clone(), schedule);
}
/// Unregister a cron schedule
pub fn unregister(&mut self, cron_id: &str) -> Option<CronSchedule> {
self.schedules.remove(cron_id)
}
/// Get a cron schedule by ID
pub fn get(&self, cron_id: &str) -> Option<CronSchedule> {
self.schedules.get(cron_id).cloned()
}
/// Get all cron schedules
pub fn all(&self) -> Vec<CronSchedule> {
self.schedules.values().cloned().collect()
}
/// Find schedules that should execute at the given time
pub fn find_executable(&self, timestamp_ms: u128) -> Vec<CronSchedule> {
self.schedules
.values()
.filter(|schedule| schedule.should_execute_at(timestamp_ms))
.cloned()
.collect()
}
/// Generate a synthetic Operation for a cron event
pub fn create_cron_operation(
schedule: &CronSchedule,
op_id: u64,
sequence: u64,
timestamp_ms: u128,
) -> Operation {
Operation::insert(
op_id,
"cron-scheduler".to_string(),
sequence,
format!("cron:{}", schedule.cron_id),
serde_json::json!({
"cron_id": schedule.cron_id,
"cron_name": schedule.name,
"capability": schedule.capability,
"triggered_at_ms": timestamp_ms,
}),
"CronEvent".to_string(),
"cron".to_string(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cron_schedule_creation() {
let schedule = CronSchedule::new(
"cron-1".to_string(),
"daily-reports".to_string(),
"0 0 * * *".to_string(),
"generate_reports".to_string(),
);
assert_eq!(schedule.cron_id, "cron-1");
assert_eq!(schedule.name, "daily-reports");
assert_eq!(schedule.enabled, true);
}
#[test]
fn test_cron_midnight_pattern() {
let schedule = CronSchedule::new(
"cron-1".to_string(),
"daily-reports".to_string(),
"0 0 * * *".to_string(),
"generate_reports".to_string(),
);
// Midnight UTC timestamp (00:00 UTC)
// 2024-01-01 00:00:00 UTC = 1704067200 seconds
let midnight_ms = 1704067200 * 1000;
// Verify that it matches at midnight
assert!(schedule.matches_cron_time(midnight_ms));
}
#[test]
fn test_cron_should_execute() {
let mut schedule = CronSchedule::new(
"cron-1".to_string(),
"daily-reports".to_string(),
"0 0 * * *".to_string(),
"generate_reports".to_string(),
);
// Midnight UTC timestamp (00:00 UTC)
let midnight_ms = 1704067200 * 1000; // 2024-01-01 00:00:00 UTC
// Should execute at midnight
assert!(schedule.should_execute_at(midnight_ms));
// Mark as executed
schedule.mark_executed(midnight_ms);
// Should not execute again within 60 seconds
assert!(!schedule.should_execute_at(midnight_ms + 30000));
// Should not execute again even after 60 seconds if still in same cron window
// (we're still within the first minute of the hour, just not at :00)
// The cron expression "0 0 * * *" only matches at hour 0, minute 0
// At midnight + 61 seconds, we're at minute 1, so it shouldn't match
assert!(!schedule.should_execute_at(midnight_ms + 61000));
// Should execute at the next day's midnight
let next_midnight_ms = midnight_ms + 86400 * 1000; // 24 hours later
assert!(schedule.should_execute_at(next_midnight_ms));
}
#[test]
fn test_cron_scheduler() {
let mut scheduler = CronScheduler::new();
let schedule1 = CronSchedule::new(
"cron-1".to_string(),
"daily-reports".to_string(),
"0 0 * * *".to_string(),
"generate_reports".to_string(),
);
let schedule2 = CronSchedule::new(
"cron-2".to_string(),
"hourly-cleanup".to_string(),
"0 * * * *".to_string(),
"cleanup".to_string(),
);
scheduler.register(schedule1);
scheduler.register(schedule2);
assert_eq!(scheduler.all().len(), 2);
assert!(scheduler.get("cron-1").is_some());
}
#[test]
fn test_cron_operation_creation() {
let schedule = CronSchedule::new(
"cron-1".to_string(),
"daily-reports".to_string(),
"0 0 * * *".to_string(),
"generate_reports".to_string(),
);
let op = CronScheduler::create_cron_operation(&schedule, 1, 1, 1704067200 * 1000);
assert_eq!(op.capability, "cron");
assert_eq!(op.rust_type, "CronEvent");
assert_eq!(op.op_type, crate::OperationType::Insert);
}
}