use crate::TRon;
use std::sync::Arc;
#[cfg(unix)]
pub fn spawn_sighup_handler(tron: Arc<TRon>) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut sig = match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup()) {
Ok(s) => s,
Err(e) => {
tracing::error!(error = %e, "failed to register SIGHUP handler");
return;
}
};
loop {
sig.recv().await;
tracing::info!("SIGHUP received, reloading policy");
match tron.reload_policy() {
Ok(()) => tracing::info!("policy reloaded successfully via SIGHUP"),
Err(e) => tracing::error!(error = %e, "policy reload failed on SIGHUP"),
}
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TRonConfig;
#[tokio::test]
async fn sighup_handler_starts() {
let tron = Arc::new(TRon::new(TRonConfig::default()));
let handle = spawn_sighup_handler(tron);
handle.abort();
let _ = handle.await;
}
}