hyperlane_plugin_scheduler/struct.rs
1use super::*;
2
3/// Compiled 5-field UTC cron schedule represented as O(1) bitmasks.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub struct CronMask {
6 /// Allowed minutes (`0..=59`).
7 pub(crate) minutes: u64,
8 /// Allowed hours (`0..=23`).
9 pub(crate) hours: u32,
10 /// Allowed days of month (`1..=31`).
11 pub(crate) days: u32,
12 /// Allowed months (`1..=12`).
13 pub(crate) months: u16,
14 /// Allowed weekdays (`0..=6`, `0` = Sunday).
15 pub(crate) weekdays: u8,
16}
17
18/// Lock-free atomic execution metrics for one registered background job.
19#[derive(Debug, Default)]
20pub struct JobMetrics {
21 /// Total started executions.
22 pub(crate) runs: AtomicU64,
23 /// Total executions returning `Ok(())`.
24 pub(crate) successes: AtomicU64,
25 /// Total executions returning `Err(_)`.
26 pub(crate) failures: AtomicU64,
27 /// Total executions terminated by a panic.
28 pub(crate) panics: AtomicU64,
29 /// Total ticks skipped because a prior run was still in flight.
30 pub(crate) skipped: AtomicU64,
31}
32
33/// Immutable point-in-time snapshot of [`JobMetrics`].
34#[derive(Clone, Copy, Debug, Eq, Getter, PartialEq)]
35pub struct JobMetricsSnapshot {
36 /// Total started executions.
37 #[get(type(copy))]
38 pub(crate) runs: u64,
39 /// Total successful runs.
40 #[get(type(copy))]
41 pub(crate) successes: u64,
42 /// Total failed runs.
43 #[get(type(copy))]
44 pub(crate) failures: u64,
45 /// Total panicked runs.
46 #[get(type(copy))]
47 pub(crate) panics: u64,
48 /// Total skipped overlapping ticks.
49 #[get(type(copy))]
50 pub(crate) skipped: u64,
51}
52
53/// Closure adapter implementing [`JobHandler`].
54pub struct FnJobHandler<F, Fut> {
55 /// Async closure invoked on each tick.
56 pub(crate) func: F,
57 /// Phantom marker pinning the returned future type.
58 pub(crate) _marker: PhantomData<fn() -> Fut>,
59}
60
61/// Internal registered job state shared across timer loops and metrics queries.
62pub(crate) struct RegisteredJob {
63 /// Unique job name.
64 pub(crate) name: String,
65 /// Execution schedule.
66 pub(crate) schedule: JobSchedule,
67 /// Whether overlapping ticks are skipped while a previous execution runs.
68 pub(crate) skip_if_running: bool,
69 /// Whether an initial tick runs immediately upon `start()`.
70 pub(crate) run_immediately: bool,
71 /// Lock-free in-flight flag.
72 pub(crate) in_flight: AtomicBool,
73 /// Atomic execution counters.
74 pub(crate) metrics: Arc<JobMetrics>,
75 /// Async job handler.
76 pub(crate) handler: Arc<dyn JobHandler>,
77}
78
79/// Builder and registry for background interval and cron jobs.
80#[derive(Default)]
81pub struct Scheduler {
82 /// Registered jobs started when [`Scheduler::start`] is called.
83 pub(crate) jobs: Vec<Arc<RegisteredJob>>,
84}
85
86/// Handle to a running [`Scheduler`] supporting metrics inspection and graceful shutdown.
87pub struct SchedulerHandle {
88 /// Broadcast channel signalling all job loops to stop.
89 pub(crate) stop_tx: Sender<bool>,
90 /// Active background loop task handles.
91 pub(crate) tasks: Vec<JoinHandle<()>>,
92 /// Per-job named metric handles.
93 pub(crate) metrics: Vec<(String, Arc<JobMetrics>)>,
94}