graceful shutdown

This commit is contained in:
Igor Katson 2024-08-26 18:45:17 +01:00
parent bf9d75e748
commit 33a171147d
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -18,7 +18,7 @@ use librqbit::{
use size_format::SizeFormatterBinary as SF; use size_format::SizeFormatterBinary as SF;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{debug, error, error_span, info, trace_span, warn}; use tracing::{error, error_span, info, trace_span, warn};
#[derive(Debug, Clone, Copy, ValueEnum)] #[derive(Debug, Clone, Copy, ValueEnum)]
enum LogLevel { enum LogLevel {
@ -365,20 +365,24 @@ fn main() -> anyhow::Result<()> {
let mut signals = Signals::new([SIGINT, SIGTERM])?; let mut signals = Signals::new([SIGINT, SIGTERM])?;
thread::spawn(move || { thread::spawn(move || {
if let Some(sig) = signals.forever().next() { if let Some(sig) = signals.forever().next() {
warn!("Received signal {:?}", sig); warn!("received signal {:?}, shutting down", sig);
token.cancel(); token.cancel();
std::thread::sleep(Duration::from_secs(5));
warn!("could not shutdown in time, killing myself");
std::process::exit(1)
} }
}); });
} }
rt.block_on(async move { let result = rt.block_on(async_main(opts, token.clone()));
let res = async_main(opts, token.clone()).await; if let Err(e) = result.as_ref() {
if let Err(e) = res { error!("error running rqbit: {e:?}");
error!("error running rqbit: {e:?}"); }
std::process::exit(1); rt.shutdown_timeout(Duration::from_secs(1));
} match result {
std::process::exit(0); Ok(_) => std::process::exit(0),
}) Err(_) => std::process::exit(1),
}
} }
async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()> { async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()> {
@ -586,7 +590,7 @@ async fn async_main(opts: Opts, cancel: CancellationToken) -> anyhow::Result<()>
}, },
}; };
res.context("error running rqbit server") res.context("error running server")
} }
}, },
SubCommand::Download(download_opts) => { SubCommand::Download(download_opts) => {