polyc-runtime 0.1.3

Shared Unix-coherence runtime for polychrome binaries: logging, health/metrics side-server, signals.
Documentation
//! Signal-driven graceful shutdown (PRD ยง11).

use tokio::signal::unix::{SignalKind, signal};
use tokio_util::sync::CancellationToken;

/// Spawn a task that cancels `token` on SIGTERM or SIGINT, and logs SIGHUP
/// (config reload is not yet implemented).
///
/// # Panics
///
/// Panics if the process cannot install the SIGTERM/SIGINT/SIGHUP handlers
/// (e.g. the signal already has a conflicting handler). This is a fatal
/// startup condition.
pub fn spawn_handler(token: CancellationToken) {
    tokio::spawn(async move {
        let mut term = signal(SignalKind::terminate()).expect("install SIGTERM handler");
        let mut int = signal(SignalKind::interrupt()).expect("install SIGINT handler");
        let mut hup = signal(SignalKind::hangup()).expect("install SIGHUP handler");
        loop {
            tokio::select! {
                _ = term.recv() => { tracing::info!("received SIGTERM"); token.cancel(); break; }
                _ = int.recv() => { tracing::info!("received SIGINT"); token.cancel(); break; }
                _ = hup.recv() => tracing::info!("received SIGHUP (config reload not yet implemented)"),
            }
        }
    });
}