use std::time::Duration;
pub const ACTIVE_STATES: &[&str] = &["queued", "in_progress", "waiting_for_user"];
pub const FINISHED_STATES: &[&str] = &["completed", "failed", "timed_out", "cancelled", "idle"];
pub const POLL_INTERVAL: Duration = Duration::from_secs(30);
pub const MAX_POLL_INTERVAL: Duration = Duration::from_secs(120);
pub fn is_active(state: &str) -> bool {
ACTIVE_STATES.contains(&state)
}
pub fn is_finished(state: &str) -> bool {
FINISHED_STATES.contains(&state)
}
pub fn needs_input(state: &str) -> bool {
state == "waiting_for_user"
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlertKind {
NeedsInput,
Completed,
Failed,
TimedOut,
Cancelled,
Idle,
}
const ALERTS: &[(&str, AlertKind, &str)] = &[
("waiting_for_user", AlertKind::NeedsInput, "needs input"),
("completed", AlertKind::Completed, "completed"),
("failed", AlertKind::Failed, "failed"),
("timed_out", AlertKind::TimedOut, "timed out"),
("cancelled", AlertKind::Cancelled, "was cancelled"),
("idle", AlertKind::Idle, "went idle"),
];
impl AlertKind {
pub fn label(self) -> &'static str {
ALERTS
.iter()
.find(|(_, kind, _)| *kind == self)
.map_or("changed", |(_, _, label)| *label)
}
}
pub fn alert_for(previous: &str, current: &str) -> Option<AlertKind> {
if previous == current || !is_active(previous) {
return None;
}
ALERTS
.iter()
.find(|(state, _, _)| *state == current)
.map(|(_, kind, _)| *kind)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PollBackoff {
interval: Duration,
}
impl Default for PollBackoff {
fn default() -> Self {
Self {
interval: POLL_INTERVAL,
}
}
}
impl PollBackoff {
pub fn new() -> Self {
Self::default()
}
pub fn interval(&self) -> Duration {
self.interval
}
pub fn failed(&mut self) -> Duration {
self.interval = (self.interval * 2).min(MAX_POLL_INTERVAL);
self.interval
}
pub fn succeeded(&mut self) -> Duration {
self.interval = POLL_INTERVAL;
self.interval
}
}
#[cfg(test)]
mod tests {
use super::{
ACTIVE_STATES, AlertKind, FINISHED_STATES, MAX_POLL_INTERVAL, POLL_INTERVAL, PollBackoff,
alert_for, is_active, is_finished, needs_input,
};
#[test]
fn active_and_finished_states_are_disjoint_and_cover_the_documented_set() {
for state in ACTIVE_STATES {
assert!(is_active(state), "{state} is active");
assert!(!is_finished(state), "{state} is not finished");
}
for state in FINISHED_STATES {
assert!(is_finished(state), "{state} is finished");
assert!(!is_active(state), "{state} is not active");
}
assert!(!is_active("new_state"));
assert!(!is_finished("new_state"));
assert!(needs_input("waiting_for_user"));
assert!(!needs_input("in_progress"));
}
#[test]
fn alerts_fire_only_when_an_active_task_reaches_a_reportable_state() {
assert_eq!(
alert_for("in_progress", "waiting_for_user"),
Some(AlertKind::NeedsInput)
);
assert_eq!(
alert_for("waiting_for_user", "completed"),
Some(AlertKind::Completed)
);
assert_eq!(alert_for("queued", "failed"), Some(AlertKind::Failed));
assert_eq!(
alert_for("in_progress", "timed_out"),
Some(AlertKind::TimedOut)
);
assert_eq!(
alert_for("in_progress", "cancelled"),
Some(AlertKind::Cancelled)
);
assert_eq!(alert_for("in_progress", "idle"), Some(AlertKind::Idle));
assert_eq!(alert_for("in_progress", "in_progress"), None);
assert_eq!(alert_for("queued", "in_progress"), None);
assert_eq!(alert_for("completed", "failed"), None);
assert_eq!(alert_for("in_progress", "new_state"), None);
assert_eq!(AlertKind::NeedsInput.label(), "needs input");
assert_eq!(AlertKind::TimedOut.label(), "timed out");
}
#[test]
fn backoff_doubles_to_the_cap_and_a_success_resets_it() {
let mut backoff = PollBackoff::new();
assert_eq!(backoff.interval(), POLL_INTERVAL);
assert_eq!(backoff.failed(), POLL_INTERVAL * 2);
assert_eq!(backoff.failed(), MAX_POLL_INTERVAL);
assert_eq!(backoff.failed(), MAX_POLL_INTERVAL);
assert_eq!(backoff.succeeded(), POLL_INTERVAL);
assert_eq!(backoff.interval(), POLL_INTERVAL);
}
}