hyperlane-plugin-scheduler 23.3.1

A background async interval and 5-field cron job scheduler plugin for the Hyperlane framework with lock-free overlap protection, Tokio panic isolation, atomic per-job metrics, and graceful shutdown.
Documentation
use super::*;

/// Compiled 5-field UTC cron schedule represented as O(1) bitmasks.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CronMask {
    /// Allowed minutes (`0..=59`).
    pub(crate) minutes: u64,
    /// Allowed hours (`0..=23`).
    pub(crate) hours: u32,
    /// Allowed days of month (`1..=31`).
    pub(crate) days: u32,
    /// Allowed months (`1..=12`).
    pub(crate) months: u16,
    /// Allowed weekdays (`0..=6`, `0` = Sunday).
    pub(crate) weekdays: u8,
}

/// Lock-free atomic execution metrics for one registered background job.
#[derive(Debug, Default)]
pub struct JobMetrics {
    /// Total started executions.
    pub(crate) runs: AtomicU64,
    /// Total executions returning `Ok(())`.
    pub(crate) successes: AtomicU64,
    /// Total executions returning `Err(_)`.
    pub(crate) failures: AtomicU64,
    /// Total executions terminated by a panic.
    pub(crate) panics: AtomicU64,
    /// Total ticks skipped because a prior run was still in flight.
    pub(crate) skipped: AtomicU64,
}

/// Immutable point-in-time snapshot of [`JobMetrics`].
#[derive(Clone, Copy, Debug, Eq, Getter, PartialEq)]
pub struct JobMetricsSnapshot {
    /// Total started executions.
    #[get(type(copy))]
    pub(crate) runs: u64,
    /// Total successful runs.
    #[get(type(copy))]
    pub(crate) successes: u64,
    /// Total failed runs.
    #[get(type(copy))]
    pub(crate) failures: u64,
    /// Total panicked runs.
    #[get(type(copy))]
    pub(crate) panics: u64,
    /// Total skipped overlapping ticks.
    #[get(type(copy))]
    pub(crate) skipped: u64,
}

/// Closure adapter implementing [`JobHandler`].
pub struct FnJobHandler<F, Fut> {
    /// Async closure invoked on each tick.
    pub(crate) func: F,
    /// Phantom marker pinning the returned future type.
    pub(crate) _marker: PhantomData<fn() -> Fut>,
}

/// Internal registered job state shared across timer loops and metrics queries.
pub(crate) struct RegisteredJob {
    /// Unique job name.
    pub(crate) name: String,
    /// Execution schedule.
    pub(crate) schedule: JobSchedule,
    /// Whether overlapping ticks are skipped while a previous execution runs.
    pub(crate) skip_if_running: bool,
    /// Whether an initial tick runs immediately upon `start()`.
    pub(crate) run_immediately: bool,
    /// Lock-free in-flight flag.
    pub(crate) in_flight: AtomicBool,
    /// Atomic execution counters.
    pub(crate) metrics: Arc<JobMetrics>,
    /// Async job handler.
    pub(crate) handler: Arc<dyn JobHandler>,
}

/// Builder and registry for background interval and cron jobs.
#[derive(Default)]
pub struct Scheduler {
    /// Registered jobs started when [`Scheduler::start`] is called.
    pub(crate) jobs: Vec<Arc<RegisteredJob>>,
}

/// Handle to a running [`Scheduler`] supporting metrics inspection and graceful shutdown.
pub struct SchedulerHandle {
    /// Broadcast channel signalling all job loops to stop.
    pub(crate) stop_tx: Sender<bool>,
    /// Active background loop task handles.
    pub(crate) tasks: Vec<JoinHandle<()>>,
    /// Per-job named metric handles.
    pub(crate) metrics: Vec<(String, Arc<JobMetrics>)>,
}