1use std::time::Duration;
6
7pub const ACTIVE_STATES: &[&str] = &["queued", "in_progress", "waiting_for_user"];
10pub const FINISHED_STATES: &[&str] = &["completed", "failed", "timed_out", "cancelled", "idle"];
11
12pub const POLL_INTERVAL: Duration = Duration::from_secs(30);
14pub const MAX_POLL_INTERVAL: Duration = Duration::from_secs(120);
16
17pub fn is_active(state: &str) -> bool {
20 ACTIVE_STATES.contains(&state)
21}
22
23pub fn is_finished(state: &str) -> bool {
27 FINISHED_STATES.contains(&state)
28}
29
30pub fn needs_input(state: &str) -> bool {
32 state == "waiting_for_user"
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum AlertKind {
38 NeedsInput,
39 Completed,
40 Failed,
41 TimedOut,
42 Cancelled,
43 Idle,
45}
46
47const ALERTS: &[(&str, AlertKind, &str)] = &[
50 ("waiting_for_user", AlertKind::NeedsInput, "needs input"),
51 ("completed", AlertKind::Completed, "completed"),
52 ("failed", AlertKind::Failed, "failed"),
53 ("timed_out", AlertKind::TimedOut, "timed out"),
54 ("cancelled", AlertKind::Cancelled, "was cancelled"),
55 ("idle", AlertKind::Idle, "went idle"),
56];
57
58impl AlertKind {
59 pub fn label(self) -> &'static str {
61 ALERTS
62 .iter()
63 .find(|(_, kind, _)| *kind == self)
64 .map_or("changed", |(_, _, label)| *label)
65 }
66}
67
68pub fn alert_for(previous: &str, current: &str) -> Option<AlertKind> {
73 if previous == current || !is_active(previous) {
74 return None;
75 }
76 ALERTS
77 .iter()
78 .find(|(state, _, _)| *state == current)
79 .map(|(_, kind, _)| *kind)
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
85pub struct PollBackoff {
86 interval: Duration,
87}
88
89impl Default for PollBackoff {
90 fn default() -> Self {
91 Self {
92 interval: POLL_INTERVAL,
93 }
94 }
95}
96
97impl PollBackoff {
98 pub fn new() -> Self {
99 Self::default()
100 }
101
102 pub fn interval(&self) -> Duration {
103 self.interval
104 }
105
106 pub fn failed(&mut self) -> Duration {
108 self.interval = (self.interval * 2).min(MAX_POLL_INTERVAL);
109 self.interval
110 }
111
112 pub fn succeeded(&mut self) -> Duration {
114 self.interval = POLL_INTERVAL;
115 self.interval
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::{
122 ACTIVE_STATES, AlertKind, FINISHED_STATES, MAX_POLL_INTERVAL, POLL_INTERVAL, PollBackoff,
123 alert_for, is_active, is_finished, needs_input,
124 };
125
126 #[test]
127 fn active_and_finished_states_are_disjoint_and_cover_the_documented_set() {
128 for state in ACTIVE_STATES {
129 assert!(is_active(state), "{state} is active");
130 assert!(!is_finished(state), "{state} is not finished");
131 }
132 for state in FINISHED_STATES {
133 assert!(is_finished(state), "{state} is finished");
134 assert!(!is_active(state), "{state} is not active");
135 }
136 assert!(!is_active("new_state"));
137 assert!(!is_finished("new_state"));
138 assert!(needs_input("waiting_for_user"));
139 assert!(!needs_input("in_progress"));
140 }
141
142 #[test]
143 fn alerts_fire_only_when_an_active_task_reaches_a_reportable_state() {
144 assert_eq!(
145 alert_for("in_progress", "waiting_for_user"),
146 Some(AlertKind::NeedsInput)
147 );
148 assert_eq!(
149 alert_for("waiting_for_user", "completed"),
150 Some(AlertKind::Completed)
151 );
152 assert_eq!(alert_for("queued", "failed"), Some(AlertKind::Failed));
153 assert_eq!(
154 alert_for("in_progress", "timed_out"),
155 Some(AlertKind::TimedOut)
156 );
157 assert_eq!(
158 alert_for("in_progress", "cancelled"),
159 Some(AlertKind::Cancelled)
160 );
161 assert_eq!(alert_for("in_progress", "idle"), Some(AlertKind::Idle));
162 assert_eq!(alert_for("in_progress", "in_progress"), None);
163 assert_eq!(alert_for("queued", "in_progress"), None);
164 assert_eq!(alert_for("completed", "failed"), None);
165 assert_eq!(alert_for("in_progress", "new_state"), None);
166 assert_eq!(AlertKind::NeedsInput.label(), "needs input");
167 assert_eq!(AlertKind::TimedOut.label(), "timed out");
168 }
169
170 #[test]
171 fn backoff_doubles_to_the_cap_and_a_success_resets_it() {
172 let mut backoff = PollBackoff::new();
173 assert_eq!(backoff.interval(), POLL_INTERVAL);
174 assert_eq!(backoff.failed(), POLL_INTERVAL * 2);
175 assert_eq!(backoff.failed(), MAX_POLL_INTERVAL);
176 assert_eq!(backoff.failed(), MAX_POLL_INTERVAL);
177 assert_eq!(backoff.succeeded(), POLL_INTERVAL);
178 assert_eq!(backoff.interval(), POLL_INTERVAL);
179 }
180}