#![allow(non_snake_case)]
use crate::core::{RiResult, RiServiceContext, ServiceModule};
use crate::hooks::{RiHookBus, RiHookEvent, RiHookKind};
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiLifecycleObserver;
impl Default for RiLifecycleObserver {
fn default() -> Self {
Self::new()
}
}
impl RiLifecycleObserver {
pub fn new() -> Self {
RiLifecycleObserver
}
}
impl ServiceModule for RiLifecycleObserver {
fn name(&self) -> &str {
"Ri.LifecycleObserver"
}
fn is_critical(&self) -> bool {
false
}
fn init(&mut self, ctx: &mut RiServiceContext) -> RiResult<()> {
let hooks: &mut RiHookBus = ctx.hooks_mut();
let all_kinds = [
RiHookKind::Startup,
RiHookKind::Shutdown,
RiHookKind::BeforeModulesInit,
RiHookKind::AfterModulesInit,
RiHookKind::BeforeModulesStart,
RiHookKind::AfterModulesStart,
RiHookKind::BeforeModulesShutdown,
RiHookKind::AfterModulesShutdown,
RiHookKind::ConfigReload,
];
for &kind in &all_kinds {
let kind_str = match kind {
RiHookKind::Startup => "Startup",
RiHookKind::Shutdown => "Shutdown",
RiHookKind::BeforeModulesInit => "BeforeModulesInit",
RiHookKind::AfterModulesInit => "AfterModulesInit",
RiHookKind::BeforeModulesStart => "BeforeModulesStart",
RiHookKind::AfterModulesStart => "AfterModulesStart",
RiHookKind::BeforeModulesShutdown => "BeforeModulesShutdown",
RiHookKind::AfterModulesShutdown => "AfterModulesShutdown",
RiHookKind::ConfigReload => "ConfigReload",
};
let handler_name = format!("dms.lifecycle.{}", kind_str.to_lowercase());
hooks.register(kind, handler_name, move |_ctx, event: &RiHookEvent| {
let logger = _ctx.logger();
let module = event.module.as_deref().unwrap_or("-");
let phase = event.phase.map(|p| p.as_str()).unwrap_or("-");
let message = format!("kind={} module={} phase={}", kind_str, module, phase);
let _ = logger.info("Ri.Lifecycle", message);
Ok(())
});
}
Ok(())
}
}
#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiLifecycleObserver {
#[new]
fn new_py() -> Self {
Self::new()
}
fn name(&self) -> String {
"Ri.LifecycleObserver".to_string()
}
fn is_critical(&self) -> bool {
false
}
fn __repr__(&self) -> String {
"RiLifecycleObserver".to_string()
}
}