nautalid 0.1.0

Scratch container substrate — TLS 1.3 HTTP/2+3 kernel, LID/AetherDB, optional filter bus (GPL-3.0-or-later).
use std::sync::{Arc, OnceLock};

use tracing::info;

#[cfg(unix)]
use tokio::signal::unix::{SignalKind, signal};
use tokio::sync::Notify;

static SHUTDOWN: OnceLock<Arc<Notify>> = OnceLock::new();

/// Returns the process-wide shutdown [`Notify`] (SIGTERM/SIGINT).
pub(crate) fn shutdown_notify() -> Arc<Notify> {
    SHUTDOWN
        .get_or_init(|| {
            let notify = Arc::new(Notify::new());
            let wake = Arc::clone(&notify);
            tokio::spawn(async move {
                #[cfg(unix)]
                {
                    let mut sigterm = signal(SignalKind::terminate())
                        .expect("register SIGTERM handler (required for PID 1 on Linux)");
                    let mut sigint =
                        signal(SignalKind::interrupt()).expect("register SIGINT handler");

                    tokio::select! {
                        _ = sigterm.recv() => info!("received SIGTERM"),
                        _ = sigint.recv() => info!("received SIGINT"),
                    }
                }
                #[cfg(not(unix))]
                {
                    tokio::signal::ctrl_c()
                        .await
                        .expect("register Ctrl+C handler");
                    info!("received Ctrl+C");
                }
                wake.notify_waiters();
            });
            notify
        })
        .clone()
}

/// Waits for SIGTERM or SIGINT so `docker stop` and Ctrl+C drain in-process work cleanly.
pub async fn wait_for_shutdown() {
    shutdown_notify().notified().await;
}