use crate::health::{HealthCheck, HealthCheckResult, HealthState};
use std::panic;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub struct PanicsHealthCheck {
panicked: Arc<AtomicBool>,
}
impl PanicsHealthCheck {
pub fn new() -> Self {
let panicked = Arc::new(AtomicBool::new(false));
let hook = panic::take_hook();
panic::set_hook({
let panicked = panicked.clone();
Box::new(move |payload| {
panicked.store(true, Ordering::Relaxed);
hook(payload)
})
});
PanicsHealthCheck { panicked }
}
}
impl HealthCheck for PanicsHealthCheck {
fn type_(&self) -> &str {
"PANICS"
}
fn result(&self) -> HealthCheckResult {
if self.panicked.load(Ordering::Relaxed) {
HealthCheckResult::builder()
.state(HealthState::Warning)
.message("A thread has panicked".to_string())
.build()
} else {
HealthCheckResult::builder()
.state(HealthState::Healthy)
.message("No thread has panicked".to_string())
.build()
}
}
}