Graceful shutdown with CTRL-C
This commit is contained in:
parent
dc40a057ac
commit
4ddc12ba28
1 changed files with 28 additions and 0 deletions
|
|
@ -14,6 +14,7 @@ use chrono::prelude::*;
|
|||
use clap::Parser;
|
||||
use config::Config;
|
||||
use itertools::Itertools;
|
||||
use tokio::signal;
|
||||
use tower::layer::Layer;
|
||||
use tower::{ServiceBuilder, buffer::BufferLayer, limit::RateLimitLayer};
|
||||
use tower_http::{
|
||||
|
|
@ -303,8 +304,11 @@ async fn main() {
|
|||
listener,
|
||||
app_with_url_rewriting.into_make_service_with_connect_info::<SocketAddr>(),
|
||||
)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
event!(Level::INFO, "Recipes stopped");
|
||||
}
|
||||
|
||||
async fn user_authentication(
|
||||
|
|
@ -506,3 +510,27 @@ async fn process_args() -> bool {
|
|||
|
||||
true
|
||||
}
|
||||
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
signal::ctrl_c()
|
||||
.await
|
||||
.expect("failed to install Ctrl+C handler");
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
let terminate = async {
|
||||
signal::unix::signal(signal::unix::SignalKind::terminate())
|
||||
.expect("failed to install signal handler")
|
||||
.recv()
|
||||
.await;
|
||||
};
|
||||
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
|
||||
tokio::select! {
|
||||
_ = ctrl_c => {},
|
||||
_ = terminate => {},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue