Skip to main content

aster_forge_tasks/
retry.rs

1//! Shared task retry classification.
2
3/// Retry behavior selected after a task failure.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum TaskRetryClass {
6    /// The dispatcher may automatically retry while the retry budget remains.
7    Auto,
8    /// The task may be retried manually, but not automatically.
9    Manual,
10    /// The failure is permanent and must not be retried.
11    Never,
12}
13
14impl TaskRetryClass {
15    /// Returns whether the class permits automatic retry.
16    pub const fn should_auto_retry(self) -> bool {
17        matches!(self, Self::Auto)
18    }
19
20    /// Returns whether the class permits manual retry.
21    pub const fn can_manual_retry(self) -> bool {
22        matches!(self, Self::Auto | Self::Manual)
23    }
24}
25
26/// Default retry delay used by Aster task dispatchers.
27///
28/// The delay is intentionally short for the first two attempts and then backs off to a stable
29/// five-minute retry interval. Product crates can pass a custom delay function into the execution
30/// runner when a task subsystem needs a different retry cadence.
31pub const fn default_task_retry_delay_secs(attempt_count: i32) -> i64 {
32    match attempt_count {
33        1 => 5,
34        2 => 15,
35        3 => 60,
36        _ => 300,
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::{TaskRetryClass, default_task_retry_delay_secs};
43
44    #[test]
45    fn retry_class_helpers_match_retry_capabilities() {
46        assert!(TaskRetryClass::Auto.should_auto_retry());
47        assert!(TaskRetryClass::Auto.can_manual_retry());
48
49        assert!(!TaskRetryClass::Manual.should_auto_retry());
50        assert!(TaskRetryClass::Manual.can_manual_retry());
51
52        assert!(!TaskRetryClass::Never.should_auto_retry());
53        assert!(!TaskRetryClass::Never.can_manual_retry());
54    }
55
56    #[test]
57    fn default_task_retry_delay_matches_existing_dispatcher_cadence() {
58        assert_eq!(default_task_retry_delay_secs(1), 5);
59        assert_eq!(default_task_retry_delay_secs(2), 15);
60        assert_eq!(default_task_retry_delay_secs(3), 60);
61        assert_eq!(default_task_retry_delay_secs(4), 300);
62        assert_eq!(default_task_retry_delay_secs(99), 300);
63    }
64}