use crate::error::types::TaskFailure;
use crate::event::payload::PolicyDecision;
use crate::event::time::EventSequence;
use crate::id::types::{Attempt, ChildId, Generation, SupervisorPath};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChildLifecycleState {
Declared,
Starting,
Running,
Ready,
Restarting,
Paused,
Quarantined,
ShuttingDown,
Stopped,
Failed,
}
impl ChildLifecycleState {
pub fn as_label(&self) -> &'static str {
match self {
Self::Declared => "declared",
Self::Starting => "starting",
Self::Running => "running",
Self::Ready => "ready",
Self::Restarting => "restarting",
Self::Paused => "paused",
Self::Quarantined => "quarantined",
Self::ShuttingDown => "shutting_down",
Self::Stopped => "stopped",
Self::Failed => "failed",
}
}
pub fn is_terminal(&self) -> bool {
matches!(self, Self::Quarantined | Self::Stopped | Self::Failed)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChildHealth {
Unknown,
Healthy,
Stale,
Unhealthy,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChildReadiness {
NotRequired,
Pending,
Ready,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChildState {
pub path: SupervisorPath,
pub id: ChildId,
pub name: String,
pub state: ChildLifecycleState,
pub health: ChildHealth,
pub generation: Generation,
pub attempt: Attempt,
pub restart_count: u64,
pub last_failure: Option<TaskFailure>,
pub last_event_sequence: Option<EventSequence>,
pub last_policy_decision: Option<PolicyDecision>,
pub readiness: ChildReadiness,
}
impl ChildState {
pub fn declared(path: SupervisorPath, id: ChildId, name: impl Into<String>) -> Self {
Self {
path,
id,
name: name.into(),
state: ChildLifecycleState::Declared,
health: ChildHealth::Unknown,
generation: Generation::initial(),
attempt: Attempt::first(),
restart_count: 0,
last_failure: None,
last_event_sequence: None,
last_policy_decision: None,
readiness: ChildReadiness::Pending,
}
}
pub fn with_lifecycle_state(
mut self,
state: ChildLifecycleState,
sequence: EventSequence,
) -> Self {
self.state = state;
self.last_event_sequence = Some(sequence);
self
}
pub fn mark_ready(mut self, sequence: EventSequence) -> Self {
self.state = ChildLifecycleState::Ready;
self.readiness = ChildReadiness::Ready;
self.last_event_sequence = Some(sequence);
self
}
pub fn record_failure(mut self, failure: TaskFailure, sequence: EventSequence) -> Self {
self.state = ChildLifecycleState::Failed;
self.health = ChildHealth::Unhealthy;
self.last_failure = Some(failure);
self.last_event_sequence = Some(sequence);
self
}
pub fn with_policy_decision(mut self, decision: PolicyDecision, restart_count: u64) -> Self {
self.last_policy_decision = Some(decision);
self.restart_count = restart_count;
self
}
}