Skip to main content

scheduler/model/
schedule.rs

1use super::CronSchedule;
2use chrono::{DateTime, Utc};
3use chrono_tz::{Asia::Shanghai, Tz};
4use std::time::Duration;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Schedule {
8    /// Trigger repeatedly after the given interval.
9    Interval(Duration),
10    /// Trigger at the listed wall-clock times.
11    ///
12    /// The list is sorted before execution starts. An empty list is treated as
13    /// a no-op schedule and exits without running.
14    AtTimes(Vec<DateTime<Tz>>),
15    /// Trigger using a standard 5-field cron expression.
16    ///
17    /// The scheduler timezone is used to calculate the next matching wall-clock
18    /// time. The expression itself does not carry timezone information.
19    Cron(CronSchedule),
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
23pub enum MissedRunPolicy {
24    Skip,
25    #[default]
26    CatchUpOnce,
27    ReplayAll,
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
31pub enum OverlapPolicy {
32    #[default]
33    Forbid,
34    QueueOne,
35    AllowParallel,
36}
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
39pub enum TerminalStatePolicy {
40    #[default]
41    Retain,
42    Delete,
43}
44
45#[derive(Debug, Clone)]
46pub struct SchedulerConfig {
47    /// The timezone forwarded to each [`crate::RunContext`] and used to
48    /// evaluate [`Schedule::Cron`] expressions.
49    ///
50    /// This does not rewrite [`Schedule::AtTimes`] values, which already carry
51    /// their own timezone-aware timestamps.
52    pub timezone: Tz,
53    /// The maximum number of [`crate::RunRecord`] items kept in memory.
54    pub history_limit: usize,
55    /// Controls how persisted state is handled once a job reaches a terminal
56    /// state (`next_run_at == None` and no further work is pending).
57    pub terminal_state_policy: TerminalStatePolicy,
58}
59
60impl Default for SchedulerConfig {
61    fn default() -> Self {
62        Self {
63            timezone: Shanghai,
64            history_limit: 32,
65            terminal_state_policy: TerminalStatePolicy::Retain,
66        }
67    }
68}
69
70pub(crate) fn utc_time(value: DateTime<Tz>) -> DateTime<Utc> {
71    value.with_timezone(&Utc)
72}