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 cron job handlers.
9pub trait ForgeCron: Send + Sync + 'static {
10    /// Get cron metadata.
11    fn info() -> CronInfo;
12
13    /// Execute the cron job.
14    fn execute(ctx: &CronContext) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>>;
15}
16
17/// Cron job metadata.
18#[derive(Debug, Clone)]
19pub struct CronInfo {
20    /// Cron name (function name).
21    pub name: &'static str,
22    /// Cron schedule expression.
23    pub schedule: CronSchedule,
24    /// Timezone for the schedule.
25    pub timezone: &'static str,
26    /// Leadership group for sharded leader election.
27    pub group: &'static str,
28    /// Whether to catch up missed runs.
29    pub catch_up: bool,
30    /// Maximum number of missed runs to catch up.
31    pub catch_up_limit: u32,
32    /// Timeout for execution.
33    pub timeout: std::time::Duration,
34    /// Default timeout for outbound HTTP requests made by this cron.
35    pub http_timeout: Option<std::time::Duration>,
36}
37
38impl Default for CronInfo {
39    fn default() -> Self {
40        Self {
41            name: "",
42            schedule: CronSchedule::default(),
43            timezone: "UTC",
44            group: "default",
45            catch_up: false,
46            catch_up_limit: 10,
47            timeout: std::time::Duration::from_secs(3600),
48            http_timeout: None,
49        }
50    }
51}
52
53#[cfg(test)]
54#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_cron_info_default() {
60        let info = CronInfo::default();
61        assert_eq!(info.name, "");
62        assert_eq!(info.timezone, "UTC");
63        assert!(!info.catch_up);
64        assert_eq!(info.catch_up_limit, 10);
65    }
66}