Skip to main content

forge_core/cron/
traits.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use super::context::CronContext;
5use super::schedule::CronSchedule;
6use crate::Result;
7
8/// Trait for scheduled cron handlers.
9pub trait ForgeCron: crate::__sealed::Sealed + Send + Sync + 'static {
10    type Args: serde::de::DeserializeOwned + Send + Sync + 'static;
11
12    fn info() -> CronInfo;
13
14    fn execute(ctx: &CronContext) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>>;
15}
16
17/// Metadata for a registered cron handler.
18#[derive(Debug, Clone)]
19pub struct CronInfo {
20    pub name: &'static str,
21    pub schedule: CronSchedule,
22    pub timezone: &'static str,
23    /// Leadership group for sharded leader election.
24    pub group: &'static str,
25    pub catch_up: bool,
26    pub catch_up_limit: u32,
27    pub timeout: std::time::Duration,
28    pub http_timeout: Option<std::time::Duration>,
29}
30
31impl Default for CronInfo {
32    fn default() -> Self {
33        Self {
34            name: "",
35            schedule: CronSchedule::default(),
36            timezone: "UTC",
37            group: "default",
38            catch_up: false,
39            catch_up_limit: 10,
40            timeout: std::time::Duration::from_secs(3600),
41            http_timeout: None,
42        }
43    }
44}
45
46#[cfg(test)]
47#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_cron_info_default() {
53        let info = CronInfo::default();
54        assert_eq!(info.name, "");
55        assert_eq!(info.timezone, "UTC");
56        assert!(!info.catch_up);
57        assert_eq!(info.catch_up_limit, 10);
58    }
59}