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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Execution history and health tracking
//!
//! Contains types for tracking task execution history, health status,
//! retry policies, catch-up policies, jitter configuration, schedule
//! versioning, and dependency tracking.
use crate::schedule::Schedule;
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
/// Schedule health status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ScheduleHealth {
/// Schedule is healthy and functioning normally
Healthy,
/// Schedule has warnings but is still functional
Warning { issues: Vec<String> },
/// Schedule is unhealthy and may not execute properly
Unhealthy { issues: Vec<String> },
}
impl ScheduleHealth {
/// Check if the schedule is healthy
pub fn is_healthy(&self) -> bool {
matches!(self, ScheduleHealth::Healthy)
}
/// Check if the schedule has warnings
pub fn has_warnings(&self) -> bool {
matches!(self, ScheduleHealth::Warning { .. })
}
/// Check if the schedule is unhealthy
pub fn is_unhealthy(&self) -> bool {
matches!(self, ScheduleHealth::Unhealthy { .. })
}
/// Get all issues (warnings or errors)
pub fn get_issues(&self) -> Vec<String> {
match self {
ScheduleHealth::Healthy => Vec::new(),
ScheduleHealth::Warning { issues } | ScheduleHealth::Unhealthy { issues } => {
issues.clone()
}
}
}
}
/// Health check result for a scheduled task
#[derive(Debug, Clone)]
pub struct HealthCheckResult {
/// Task name
pub task_name: String,
/// Overall health status
pub health: ScheduleHealth,
/// Next scheduled run time (if calculable)
pub next_run: Option<DateTime<Utc>>,
/// Time since last execution (if applicable)
pub time_since_last_run: Option<chrono::Duration>,
}
impl HealthCheckResult {
/// Create a new health check result
pub fn new(task_name: String, health: ScheduleHealth) -> Self {
Self {
task_name,
health,
next_run: None,
time_since_last_run: None,
}
}
/// Create a healthy result
pub fn healthy(task_name: String) -> Self {
Self::new(task_name, ScheduleHealth::Healthy)
}
/// Create a warning result
pub fn warning(task_name: String, issues: Vec<String>) -> Self {
Self::new(task_name, ScheduleHealth::Warning { issues })
}
/// Create an unhealthy result
pub fn unhealthy(task_name: String, issues: Vec<String>) -> Self {
Self::new(task_name, ScheduleHealth::Unhealthy { issues })
}
/// Set next run time
pub fn with_next_run(mut self, next_run: DateTime<Utc>) -> Self {
self.next_run = Some(next_run);
self
}
/// Set time since last run
pub fn with_time_since_last_run(mut self, duration: chrono::Duration) -> Self {
self.time_since_last_run = Some(duration);
self
}
}
/// Execution result status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ExecutionResult {
/// Execution completed successfully
Success,
/// Execution failed with error message
Failure { error: String },
/// Execution timed out
Timeout,
/// Execution was interrupted (e.g., scheduler crash)
Interrupted,
}
/// Execution state for crash recovery
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub enum ExecutionState {
/// No execution in progress
#[default]
Idle,
/// Execution is currently running
Running {
/// When execution started
started_at: DateTime<Utc>,
/// Expected timeout (for detection)
timeout_after: Option<DateTime<Utc>>,
},
}
impl ExecutionState {
/// Check if execution is running
pub fn is_running(&self) -> bool {
matches!(self, ExecutionState::Running { .. })
}
/// Check if execution is idle
pub fn is_idle(&self) -> bool {
matches!(self, ExecutionState::Idle)
}
/// Check if a running execution has timed out
pub fn has_timed_out(&self) -> bool {
match self {
ExecutionState::Running {
timeout_after: Some(timeout),
..
} => Utc::now() > *timeout,
_ => false,
}
}
/// Get duration since execution started (if running)
pub fn running_duration(&self) -> Option<Duration> {
match self {
ExecutionState::Running { started_at, .. } => Some(Utc::now() - *started_at),
ExecutionState::Idle => None,
}
}
}
/// Record of a single task execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionRecord {
/// Execution start timestamp
pub started_at: DateTime<Utc>,
/// Execution end timestamp (if completed)
pub completed_at: Option<DateTime<Utc>>,
/// Execution result
pub result: ExecutionResult,
/// Execution duration in milliseconds
pub duration_ms: Option<u64>,
}
impl ExecutionRecord {
/// Create a new execution record
pub fn new(started_at: DateTime<Utc>) -> Self {
Self {
started_at,
completed_at: None,
result: ExecutionResult::Success,
duration_ms: None,
}
}
/// Create a completed execution record
pub fn completed(started_at: DateTime<Utc>, result: ExecutionResult) -> Self {
let now = Utc::now();
let duration_ms = now
.signed_duration_since(started_at)
.num_milliseconds()
.max(0) as u64;
Self {
started_at,
completed_at: Some(now),
result,
duration_ms: Some(duration_ms),
}
}
/// Check if execution was successful
pub fn is_success(&self) -> bool {
matches!(self.result, ExecutionResult::Success)
}
/// Check if execution failed
pub fn is_failure(&self) -> bool {
matches!(self.result, ExecutionResult::Failure { .. })
}
/// Check if execution timed out
pub fn is_timeout(&self) -> bool {
matches!(self.result, ExecutionResult::Timeout)
}
/// Check if execution is completed
pub fn is_completed(&self) -> bool {
self.completed_at.is_some()
}
/// Check if execution was interrupted
pub fn is_interrupted(&self) -> bool {
matches!(self.result, ExecutionResult::Interrupted)
}
}
/// Retry policy for failed task executions
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum RetryPolicy {
/// Don't retry failed tasks
#[default]
NoRetry,
/// Retry with fixed delay between attempts
FixedDelay {
/// Delay in seconds between retries
delay_seconds: u64,
/// Maximum number of retry attempts
max_retries: u32,
},
/// Retry with exponential backoff
ExponentialBackoff {
/// Initial delay in seconds
initial_delay_seconds: u64,
/// Multiplier for each retry (e.g., 2.0 doubles the delay)
multiplier: f64,
/// Maximum delay in seconds
max_delay_seconds: u64,
/// Maximum number of retry attempts
max_retries: u32,
},
}
impl RetryPolicy {
/// Calculate the next retry delay based on the number of attempts
pub fn next_retry_delay(&self, attempt: u32) -> Option<u64> {
match self {
RetryPolicy::NoRetry => None,
RetryPolicy::FixedDelay {
delay_seconds,
max_retries,
} => {
if attempt < *max_retries {
Some(*delay_seconds)
} else {
None
}
}
RetryPolicy::ExponentialBackoff {
initial_delay_seconds,
multiplier,
max_delay_seconds,
max_retries,
} => {
if attempt < *max_retries {
let delay = (*initial_delay_seconds as f64) * multiplier.powi(attempt as i32);
let delay = delay.min(*max_delay_seconds as f64) as u64;
Some(delay)
} else {
None
}
}
}
}
/// Check if retry is allowed for the given attempt
pub fn should_retry(&self, attempt: u32) -> bool {
self.next_retry_delay(attempt).is_some()
}
}
/// Catch-up policy for handling missed schedules
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum CatchupPolicy {
/// Skip all missed runs
#[default]
Skip,
/// Run once immediately if any runs were missed
RunOnce,
/// Run multiple times to catch up (up to max_catchup runs)
RunMultiple { max_catchup: u32 },
/// Only run if missed within a time window (in seconds)
TimeWindow { window_seconds: u64 },
}
impl CatchupPolicy {
/// Check if we should execute a catch-up run
pub fn should_catchup(
&self,
last_run_at: Option<DateTime<Utc>>,
next_scheduled_run: DateTime<Utc>,
now: DateTime<Utc>,
) -> bool {
match self {
CatchupPolicy::Skip => false,
CatchupPolicy::RunOnce => last_run_at.is_some() && now > next_scheduled_run,
CatchupPolicy::RunMultiple { .. } => last_run_at.is_some() && now > next_scheduled_run,
CatchupPolicy::TimeWindow { window_seconds } => {
if let Some(_last_run) = last_run_at {
let missed_duration = now.signed_duration_since(next_scheduled_run);
missed_duration.num_seconds() > 0
&& missed_duration.num_seconds() <= *window_seconds as i64
} else {
false
}
}
}
}
/// Calculate number of catch-up runs to execute
pub fn catchup_count(
&self,
last_run_at: Option<DateTime<Utc>>,
interval_seconds: u64,
now: DateTime<Utc>,
) -> u32 {
match self {
CatchupPolicy::Skip => 0,
CatchupPolicy::RunOnce => {
if last_run_at.is_some() {
1
} else {
0
}
}
CatchupPolicy::RunMultiple { max_catchup } => {
if let Some(last_run) = last_run_at {
let elapsed = now.signed_duration_since(last_run).num_seconds() as u64;
let missed_runs = elapsed / interval_seconds;
std::cmp::min(missed_runs.saturating_sub(1) as u32, *max_catchup)
} else {
0
}
}
CatchupPolicy::TimeWindow { .. } => {
if let Some(last_run) = last_run_at {
let next_run = last_run + Duration::seconds(interval_seconds as i64);
if self.should_catchup(last_run_at, next_run, now) {
1
} else {
0
}
} else {
0
}
}
}
}
}
/// Jitter configuration for schedule randomization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Jitter {
/// Minimum jitter offset in seconds (can be negative)
pub min_seconds: i64,
/// Maximum jitter offset in seconds
pub max_seconds: i64,
}
impl Jitter {
/// Create a new jitter configuration
pub fn new(min_seconds: i64, max_seconds: i64) -> Self {
Self {
min_seconds,
max_seconds,
}
}
/// Create jitter with only positive offset (0 to max_seconds)
pub fn positive(max_seconds: i64) -> Self {
Self {
min_seconds: 0,
max_seconds,
}
}
/// Create symmetric jitter (-seconds to +seconds)
pub fn symmetric(seconds: i64) -> Self {
Self {
min_seconds: -seconds,
max_seconds: seconds,
}
}
/// Apply jitter to a datetime using hash-based deterministic randomization
pub fn apply(&self, dt: DateTime<Utc>, task_name: &str) -> DateTime<Utc> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
// Use task name and datetime to generate deterministic random offset
let mut hasher = DefaultHasher::new();
task_name.hash(&mut hasher);
dt.timestamp().hash(&mut hasher);
let hash = hasher.finish();
// Map hash to jitter range
let range = (self.max_seconds - self.min_seconds) as u64;
let offset = if range > 0 {
(hash % range) as i64 + self.min_seconds
} else {
self.min_seconds
};
dt + Duration::seconds(offset)
}
}
/// Schedule version record for tracking changes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScheduleVersion {
/// Version number (starts at 1)
pub version: u32,
/// The schedule at this version
pub schedule: Schedule,
/// Timestamp when this version was created
pub created_at: DateTime<Utc>,
/// Optional reason for the change
#[serde(skip_serializing_if = "Option::is_none")]
pub change_reason: Option<String>,
/// Task configuration at this version (enabled, jitter, etc.)
#[serde(default)]
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub jitter: Option<Jitter>,
#[serde(default)]
pub catchup_policy: CatchupPolicy,
}
/// Task dependency status
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum DependencyStatus {
/// All dependencies satisfied
Satisfied,
/// Waiting for dependencies to complete
Waiting { pending: Vec<String> },
/// One or more dependencies failed
Failed { failed: Vec<String> },
}
impl DependencyStatus {
/// Check if dependencies are satisfied
pub fn is_satisfied(&self) -> bool {
matches!(self, DependencyStatus::Satisfied)
}
/// Check if any dependency failed
pub fn has_failures(&self) -> bool {
matches!(self, DependencyStatus::Failed { .. })
}
/// Get list of pending dependencies
pub fn pending_tasks(&self) -> Vec<String> {
match self {
DependencyStatus::Waiting { pending } => pending.clone(),
_ => Vec::new(),
}
}
/// Get list of failed dependencies
pub fn failed_tasks(&self) -> Vec<String> {
match self {
DependencyStatus::Failed { failed } => failed.clone(),
_ => Vec::new(),
}
}
}