Add graceful shutdown on SIGTERM (#1014)

* Add graceful shutdown on SIGTERM

* Fix linter
This commit is contained in:
Marijan Smetko 2023-05-30 06:51:16 +02:00 committed by GitHub
parent 9e3fa8b88a
commit 5dc189cf83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,6 +8,8 @@ use eyre::{Context, Result};
use crate::settings::Settings; use crate::settings::Settings;
use tokio::signal;
pub mod auth; pub mod auth;
pub mod calendar; pub mod calendar;
pub mod database; pub mod database;
@ -17,6 +19,20 @@ pub mod router;
pub mod settings; pub mod settings;
pub mod utils; pub mod utils;
async fn shutdown_signal() {
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to register signal handler")
.recv()
.await;
};
tokio::select! {
_ = terminate => (),
}
eprintln!("Shutting down gracefully...");
}
pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> { pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
let host = host.parse::<IpAddr>()?; let host = host.parse::<IpAddr>()?;
@ -28,6 +44,7 @@ pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
Server::bind(&SocketAddr::new(host, port)) Server::bind(&SocketAddr::new(host, port))
.serve(r.into_make_service()) .serve(r.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await?; .await?;
Ok(()) Ok(())