add graceful shutdown handler
This commit is contained in:
parent
a8fdedfe1b
commit
c4f599e394
3 changed files with 40 additions and 2 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -1481,6 +1481,15 @@ dependencies = [
|
||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signal-hook-registry"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signature"
|
name = "signature"
|
||||||
version = "2.2.0"
|
version = "2.2.0"
|
||||||
|
@ -1888,6 +1897,7 @@ dependencies = [
|
||||||
"mio",
|
"mio",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
|
|
|
@ -20,7 +20,7 @@ serde_json = { version = "1", default-features = false }
|
||||||
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "sqlite", "tls-none", "migrate", "macros"] }
|
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "sqlite", "tls-none", "migrate", "macros"] }
|
||||||
thiserror = { version = "1" }
|
thiserror = { version = "1" }
|
||||||
time = { version = "0.3", default-features = false }
|
time = { version = "0.3", default-features = false }
|
||||||
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
|
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "signal"] }
|
||||||
tower-http = { version = "0.5", default-features = false, features = ["fs"] }
|
tower-http = { version = "0.5", default-features = false, features = ["fs"] }
|
||||||
tower-sessions = { version = "0.11", default-features = false, features = ["axum-core"] }
|
tower-sessions = { version = "0.11", default-features = false, features = ["axum-core"] }
|
||||||
tower-sessions-sqlx-store = { version = "0.11", default-features = false, features = ["sqlite"] }
|
tower-sessions-sqlx-store = { version = "0.11", default-features = false, features = ["sqlite"] }
|
||||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -55,7 +55,10 @@ async fn main() {
|
||||||
.with_state(pool)
|
.with_state(pool)
|
||||||
.into_make_service();
|
.into_make_service();
|
||||||
let listener = mklistener().await;
|
let listener = mklistener().await;
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app)
|
||||||
|
.with_graceful_shutdown(shutdown_signal())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
@ -114,3 +117,28 @@ where
|
||||||
.route(path.trim_end_matches('/'), method_router)
|
.route(path.trim_end_matches('/'), method_router)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn shutdown_signal() {
|
||||||
|
use tokio::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…
Reference in a new issue