graceful shutdown
This commit is contained in:
parent
bf9d75e748
commit
33a171147d
1 changed files with 15 additions and 11 deletions
|
|
@ -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) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue