use aion_store::{DeployedBinaryIdentity, DesiredState};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ManagedWorkerState {
Starting,
Running,
Backoff,
Stopped,
Failed,
Uncommissioned,
}
impl ManagedWorkerState {
#[must_use]
pub const fn token(self) -> &'static str {
match self {
Self::Starting => "starting",
Self::Running => "running",
Self::Backoff => "backoff",
Self::Stopped => "stopped",
Self::Failed => "failed",
Self::Uncommissioned => "uncommissioned",
}
}
#[must_use]
pub const fn is_terminal(self) -> bool {
matches!(self, Self::Stopped | Self::Failed)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct SpawnedBinary {
pub path: String,
pub content_hash: String,
pub version: Option<String>,
pub commit: Option<String>,
pub dirty: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ManagedWorkerExit {
pub at: chrono::DateTime<chrono::Utc>,
pub code: Option<i32>,
pub signal: Option<i32>,
pub requested: bool,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ManagedWorkerStatus {
pub name: String,
pub task_queue: String,
pub desired: DesiredState,
pub state: ManagedWorkerState,
pub pid: Option<u32>,
pub process_group: Option<i32>,
pub restarts: u32,
pub last_exit: Option<ManagedWorkerExit>,
pub last_error: Option<String>,
pub deployed_binary: DeployedBinaryIdentity,
pub spawn_binary: Option<SpawnedBinary>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ManagedWorkerReport {
pub commissioned: bool,
pub remedy: Option<String>,
pub workers: Vec<ManagedWorkerStatus>,
pub undecodable: Vec<String>,
}
#[cfg(test)]
mod tests {
use super::ManagedWorkerState;
#[test]
fn tokens_are_distinct_and_terminality_matches_them() {
let states = [
ManagedWorkerState::Starting,
ManagedWorkerState::Running,
ManagedWorkerState::Backoff,
ManagedWorkerState::Stopped,
ManagedWorkerState::Failed,
ManagedWorkerState::Uncommissioned,
];
let mut tokens = states.map(ManagedWorkerState::token).to_vec();
tokens.sort_unstable();
tokens.dedup();
assert_eq!(tokens.len(), states.len());
assert!(ManagedWorkerState::Stopped.is_terminal());
assert!(ManagedWorkerState::Failed.is_terminal());
assert!(!ManagedWorkerState::Running.is_terminal());
assert!(!ManagedWorkerState::Backoff.is_terminal());
assert!(!ManagedWorkerState::Starting.is_terminal());
assert!(!ManagedWorkerState::Uncommissioned.is_terminal());
}
}