stop on ctrl+c
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -160,13 +160,25 @@ async fn restart_task(connection: Arc<Docker>, mut rx: mpsc::Receiver<Containers
|
||||
log::warn!("restart -> container: {}...", &container_id);
|
||||
let res = restart_container(&connection, container_id.as_str()).await;
|
||||
match res {
|
||||
Ok(_) => log::info!("ok\n"),
|
||||
Err(e) => log::error!("error: \n{e:?}\n"),
|
||||
Ok(_) => log::info!("ok"),
|
||||
Err(e) => log::error!("error: \n{e:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn shutdown_control(shutdown: Option<mpsc::Sender<()>>) {
|
||||
let mut shutdown = shutdown;
|
||||
let res = ctrlc::set_handler(move || {
|
||||
log::info!("recieved Ctrl-C");
|
||||
shutdown.take();
|
||||
});
|
||||
|
||||
if res.is_ok() {
|
||||
log::info!("Press Ctrl-C to stop");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let logger = create_logger()?;
|
||||
@@ -190,6 +202,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
let interval = cli.interval;
|
||||
let unhealthy_timeout = cli.unhealthy_timeout;
|
||||
|
||||
shutdown_control(Some(shutdown_tx));
|
||||
|
||||
tokio::try_join!(
|
||||
tokio::spawn(query_task(
|
||||
query_connection,
|
||||
@@ -199,13 +213,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
cli
|
||||
)),
|
||||
tokio::spawn(filter_task(unhealthy_timeout, filter_rx, filter_tx)),
|
||||
tokio::spawn(restart_task(restart_connection, restart_rx)),
|
||||
tokio::spawn(async {
|
||||
log::debug!("shutdown -> sleep");
|
||||
tokio::time::sleep(Duration::from_secs(80)).await;
|
||||
log::warn!("shutdown -> drop");
|
||||
drop(shutdown_tx);
|
||||
})
|
||||
tokio::spawn(restart_task(restart_connection, restart_rx))
|
||||
)?;
|
||||
|
||||
drop(logger);
|
||||
|
Reference in New Issue
Block a user