pub use api::objects::HealthState;
use conjure_object::Any;
pub use registry::HealthCheckRegistry;
use serde::Serialize;
use staged_builder::staged_builder;
use std::any::TypeId;
use std::collections::BTreeMap;
use std::sync::Arc;
#[allow(warnings)]
#[rustfmt::skip]
pub(crate) mod api;
pub(crate) mod config_reload;
pub(crate) mod endpoint_500s;
pub(crate) mod minidump;
pub(crate) mod panics;
mod registry;
pub(crate) mod service_dependency;
mod private {
pub struct PrivacyToken;
}
pub trait HealthCheck: 'static + Sync + Send {
fn type_(&self) -> &str;
fn result(&self) -> HealthCheckResult;
#[doc(hidden)]
fn __private_api_type_id(&self, _: private::PrivacyToken) -> TypeId
where
Self: 'static,
{
TypeId::of::<Self>()
}
}
impl dyn HealthCheck {
pub fn is<T>(&self) -> bool
where
T: HealthCheck,
{
self.__private_api_type_id(private::PrivacyToken) == TypeId::of::<T>()
}
pub fn downcast_ref<T>(&self) -> Option<&T>
where
T: HealthCheck,
{
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn HealthCheck as *const T)) }
} else {
None
}
}
pub fn downcast_arc<T>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
where
T: HealthCheck,
{
if self.is::<T>() {
unsafe { Ok(Arc::from_raw(Arc::into_raw(self).cast::<T>())) }
} else {
Err(self)
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[staged_builder]
pub struct HealthCheckResult {
state: HealthState,
#[builder(default, into)]
message: Option<String>,
#[builder(map(key(type = String, into), value(custom(type = impl Serialize, convert = serialize))))]
params: BTreeMap<String, Any>,
}
fn serialize(arg: impl Serialize) -> Any {
Any::new(arg).expect("value failed to serialize")
}
impl HealthCheckResult {
#[inline]
pub fn state(&self) -> &HealthState {
&self.state
}
#[inline]
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
#[inline]
pub fn params(&self) -> &BTreeMap<String, Any> {
&self.params
}
}