1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#![doc = include_str!("../README.md")]
use log::warn;
/// Tokio signal handler that will wait for a user to press CTRL+C.
/// This signal handler can be used in our [`axum::Server`] method `with_graceful_shutdown`
/// and in our [`tonic::transport::Server`] method `serve_with_shutdown`.
///
/// # Examples
///
/// ## axum
/// ```
/// use svc_cargo::shutdown_signal;
/// pub async fn server() {
/// let app = axum::Router::new();
/// axum::Server::bind(&"0.0.0.0:8000".parse().unwrap())
/// .serve(app.into_make_service())
/// .with_graceful_shutdown(shutdown_signal("rest"));
/// }
/// ```
///
/// ## tonic
/// ```
/// use svc_cargo::shutdown_signal;
/// pub async fn server() {
/// let (_, health_service) = tonic_health::server::health_reporter();
/// tonic::transport::Server::builder()
/// .add_service(health_service)
/// .serve_with_shutdown("0.0.0.0:50051".parse().unwrap(), shutdown_signal("grpc"));
/// }
/// ```
#[cfg(not(tarpaulin_include))]
pub async fn shutdown_signal(server: &str) {
tokio::signal::ctrl_c()
.await
.expect("expect tokio signal ctrl-c");
warn!("(shutdown_signal) shutdown signal: {}", server);
}