use crate::error::types::TaskFailure;
use crate::event::time::{CorrelationId, EventSequence, When};
use crate::id::types::{ChildId, SupervisorPath};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Where {
pub supervisor_path: SupervisorPath,
pub parent_id: Option<ChildId>,
pub child_id: Option<ChildId>,
pub child_name: Option<String>,
pub tokio_task_id: Option<String>,
pub host: Option<String>,
pub pid: u32,
pub thread_name: Option<String>,
pub module_path: Option<String>,
pub source_file: Option<String>,
pub source_line: Option<u32>,
}
impl Where {
pub fn new(supervisor_path: SupervisorPath) -> Self {
Self {
supervisor_path,
parent_id: None,
child_id: None,
child_name: None,
tokio_task_id: None,
host: None,
pid: std::process::id(),
thread_name: std::thread::current().name().map(ToOwned::to_owned),
module_path: None,
source_file: None,
source_line: None,
}
}
pub fn with_child(mut self, child_id: ChildId, child_name: impl Into<String>) -> Self {
self.child_id = Some(child_id);
self.child_name = Some(child_name.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StateTransition {
pub from: String,
pub to: String,
}
impl StateTransition {
pub fn new(from: impl Into<String>, to: impl Into<String>) -> Self {
Self {
from: from.into(),
to: to.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyDecision {
pub decision: String,
pub delay_ms: Option<u64>,
pub reason: Option<String>,
}
impl PolicyDecision {
pub fn new(decision: impl Into<String>, delay_ms: Option<u64>, reason: Option<String>) -> Self {
Self {
decision: decision.into(),
delay_ms,
reason,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandAudit {
pub command_id: String,
pub requested_by: String,
pub reason: String,
pub target_path: SupervisorPath,
pub accepted_at_unix_nanos: u128,
pub result: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum What {
ChildStarting {
transition: Option<StateTransition>,
},
ChildRunning {
transition: Option<StateTransition>,
},
ChildReady {
transition: Option<StateTransition>,
},
ChildHeartbeat {
age_ms: u64,
},
ChildFailed {
failure: TaskFailure,
},
ChildPanicked {
category: String,
},
BackoffScheduled {
delay_ms: u64,
},
ChildRestarting {
generation: u64,
},
ChildRestarted {
restart_count: u64,
},
ChildQuarantined {
reason: String,
},
ChildStopped {
reason: String,
},
ChildUnhealthy {
reason: String,
},
Meltdown {
scope: String,
},
ShutdownRequested {
cause: String,
},
ShutdownPhaseChanged {
from: String,
to: String,
},
ShutdownCompleted {
result: String,
},
CommandAccepted {
audit: CommandAudit,
},
CommandCompleted {
audit: CommandAudit,
},
SubscriberLagged {
missed: u64,
},
}
impl What {
pub fn name(&self) -> &'static str {
match self {
Self::ChildStarting { .. } => "ChildStarting",
Self::ChildRunning { .. } => "ChildRunning",
Self::ChildReady { .. } => "ChildReady",
Self::ChildHeartbeat { .. } => "ChildHeartbeat",
Self::ChildFailed { .. } => "ChildFailed",
Self::ChildPanicked { .. } => "ChildPanicked",
Self::BackoffScheduled { .. } => "BackoffScheduled",
Self::ChildRestarting { .. } => "ChildRestarting",
Self::ChildRestarted { .. } => "ChildRestarted",
Self::ChildQuarantined { .. } => "ChildQuarantined",
Self::ChildStopped { .. } => "ChildStopped",
Self::ChildUnhealthy { .. } => "ChildUnhealthy",
Self::Meltdown { .. } => "Meltdown",
Self::ShutdownRequested { .. } => "ShutdownRequested",
Self::ShutdownPhaseChanged { .. } => "ShutdownPhaseChanged",
Self::ShutdownCompleted { .. } => "ShutdownCompleted",
Self::CommandAccepted { .. } => "CommandAccepted",
Self::CommandCompleted { .. } => "CommandCompleted",
Self::SubscriberLagged { .. } => "SubscriberLagged",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SupervisorEvent {
pub when: When,
pub r#where: Where,
pub what: What,
pub policy: Option<PolicyDecision>,
pub sequence: EventSequence,
pub correlation_id: CorrelationId,
pub config_version: u64,
}
impl SupervisorEvent {
pub fn new(
when: When,
r#where: Where,
what: What,
sequence: EventSequence,
correlation_id: CorrelationId,
config_version: u64,
) -> Self {
Self {
when,
r#where,
what,
policy: None,
sequence,
correlation_id,
config_version,
}
}
pub fn with_policy(mut self, policy: PolicyDecision) -> Self {
self.policy = Some(policy);
self
}
}