use crate::task::TaskState;
use std::time::Instant;
#[derive(Debug, Clone)]
pub struct StateEvent {
pub task_name: String,
pub old_state: TaskState,
pub new_state: TaskState,
pub timestamp: Instant,
pub error: Option<String>,
}
impl StateEvent {
pub fn new(task_name: impl Into<String>, old_state: TaskState, new_state: TaskState) -> Self {
Self {
task_name: task_name.into(),
old_state,
new_state,
timestamp: Instant::now(),
error: None,
}
}
pub fn with_error(mut self, error: impl Into<String>) -> Self {
self.error = Some(error.into());
self
}
pub fn is_failure(&self) -> bool {
self.new_state == TaskState::Failed
}
pub fn is_terminal(&self) -> bool {
self.new_state.is_terminal()
}
}
#[derive(Debug, Clone)]
pub enum RuntimeEvent {
Startup {
name: String,
timestamp: Instant,
},
Shutdown {
name: String,
timestamp: Instant,
},
TaskStateChanged(StateEvent),
HealthCheckFailed {
check_name: String,
reason: String,
timestamp: Instant,
},
ServiceRegistered {
service_name: String,
service_id: String,
timestamp: Instant,
},
ServiceDeregistered {
service_name: String,
service_id: String,
timestamp: Instant,
},
}
impl RuntimeEvent {
pub fn timestamp(&self) -> Instant {
match self {
RuntimeEvent::Startup { timestamp, .. } => *timestamp,
RuntimeEvent::Shutdown { timestamp, .. } => *timestamp,
RuntimeEvent::TaskStateChanged(event) => event.timestamp,
RuntimeEvent::HealthCheckFailed { timestamp, .. } => *timestamp,
RuntimeEvent::ServiceRegistered { timestamp, .. } => *timestamp,
RuntimeEvent::ServiceDeregistered { timestamp, .. } => *timestamp,
}
}
}