use crate::event::time::EventSequence;
use crate::id::types::SupervisorPath;
use crate::state::child::ChildState;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ShutdownState {
Idle,
RequestStop,
GracefulDrain,
AbortStragglers,
Reconcile,
Completed,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MeltdownState {
Clear,
ChildFuseTripped {
path: SupervisorPath,
},
SupervisorFuseTripped {
path: SupervisorPath,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SupervisorState {
pub root_path: SupervisorPath,
pub generated_at_unix_nanos: u128,
pub sequence: EventSequence,
pub config_version: u64,
pub children: BTreeMap<String, ChildState>,
pub meltdown_state: MeltdownState,
pub shutdown_state: ShutdownState,
pub journal_sequence: Option<EventSequence>,
}
impl SupervisorState {
pub fn new(root_path: SupervisorPath, sequence: EventSequence, config_version: u64) -> Self {
Self {
root_path,
generated_at_unix_nanos: crate::state::supervisor::unix_nanos_now(),
sequence,
config_version,
children: BTreeMap::new(),
meltdown_state: MeltdownState::Clear,
shutdown_state: ShutdownState::Idle,
journal_sequence: None,
}
}
pub fn with_child(mut self, child: ChildState) -> Self {
self.children.insert(child.path.to_string(), child);
self
}
pub fn with_shutdown_state(mut self, shutdown_state: ShutdownState) -> Self {
self.shutdown_state = shutdown_state;
self
}
pub fn with_meltdown_state(mut self, meltdown_state: MeltdownState) -> Self {
self.meltdown_state = meltdown_state;
self
}
pub fn with_journal_sequence(mut self, journal_sequence: EventSequence) -> Self {
self.journal_sequence = Some(journal_sequence);
self
}
}
fn unix_nanos_now() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or(std::time::Duration::ZERO)
.as_nanos()
}