librt-rs 0.1.0

Runtime primitives
Documentation
use tokio::signal::unix::{SignalKind, signal};

#[cfg(feature = "tracing")]
use tracing::info;

pub async fn shutdown() {
    tokio::select! {
        // SIGINT  - To allow Ctrl-c to emulate SIGTERM while developing.
        () = sig(SignalKind::interrupt(), "SIGINT") => {
            std::process::exit(0);
        }
        // SIGTERM - Kubernetes sends this to start a graceful shutdown.
        () = sig(SignalKind::terminate(), "SIGTERM") => {}
    };
}

async fn sig(kind: SignalKind, name: &'static str) {
    // Create a Future that completes the first
    // time the process receives 'sig'.
    signal(kind)
        .expect("Failed to register signal handler")
        .recv()
        .await;
    info!(
        // use target to remove 'imp' from output
        target: "rt::signal",
        "received {}, starting shutdown",
        name,
    );
}