topiq-server 0.1.3

Topiq message broker server binary
use tokio::signal;
use tokio_util::sync::CancellationToken;
use tracing::info;

/// Wait for SIGINT (Ctrl+C) or SIGTERM, then cancel the token.
pub async fn wait_for_shutdown(token: CancellationToken) {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .expect("failed to install SIGINT handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("failed to install SIGTERM handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => info!("received SIGINT"),
        _ = terminate => info!("received SIGTERM"),
    }

    token.cancel();
}