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    #[must_use]
17    pub const fn should_auto_retry(self) -> bool {
18        matches!(self, Self::Auto)
19    }
20
21    /// Returns whether the class permits manual retry.
22    #[must_use]
23    pub const fn can_manual_retry(self) -> bool {
24        matches!(self, Self::Auto | Self::Manual)
25    }
26}
27
28/// Default retry delay used by Aster task dispatchers.
29///
30/// The delay is intentionally short for the first two attempts and then backs off to a stable
31/// five-minute retry interval. Product crates can pass a custom delay function into the execution
32/// runner when a task subsystem needs a different retry cadence.
33#[must_use]
34pub const fn default_task_retry_delay_secs(attempt_count: i32) -> i64 {
35    match attempt_count {
36        1 => 5,
37        2 => 15,
38        3 => 60,
39        _ => 300,
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::{TaskRetryClass, default_task_retry_delay_secs};
46
47    #[test]
48    fn retry_class_helpers_match_retry_capabilities() {
49        assert!(TaskRetryClass::Auto.should_auto_retry());
50        assert!(TaskRetryClass::Auto.can_manual_retry());
51
52        assert!(!TaskRetryClass::Manual.should_auto_retry());
53        assert!(TaskRetryClass::Manual.can_manual_retry());
54
55        assert!(!TaskRetryClass::Never.should_auto_retry());
56        assert!(!TaskRetryClass::Never.can_manual_retry());
57    }
58
59    #[test]
60    fn default_task_retry_delay_matches_existing_dispatcher_cadence() {
61        assert_eq!(default_task_retry_delay_secs(1), 5);
62        assert_eq!(default_task_retry_delay_secs(2), 15);
63        assert_eq!(default_task_retry_delay_secs(3), 60);
64        assert_eq!(default_task_retry_delay_secs(4), 300);
65        assert_eq!(default_task_retry_delay_secs(99), 300);
66    }
67}