add restart task

This commit is contained in:
Dmitry Belyaev 2024-09-12 16:55:40 +03:00
parent f6d99b224e
commit 8fc3e5a855
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -115,38 +115,44 @@ async fn filter_task(
} }
} }
async fn log_task(mut rx: mpsc::Receiver<Containers>) { async fn restart_task(connection: Arc<Docker>, mut rx: mpsc::Receiver<Containers>) {
println!("log start"); println!("restart task start");
while let Some(containers) = rx.recv().await { while let Some(containers) = rx.recv().await {
println!("log -> found: {}", containers.len()); println!("restart -> found: {}", containers.len());
for container_id in containers { for container_id in containers {
println!("log -> container: {}", container_id); print!("restart -> container: {}...", &container_id);
let res = restart_container(&connection, container_id.as_str()).await;
match res {
Ok(_) => println!("ok\n"),
Err(e) => println!("error: \n{e:?}\n")
}
} }
println!("")
} }
} }
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let connection = Arc::new(Docker::connect_with_defaults()?); let connection = Arc::new(Docker::connect_with_defaults()?);
let query_connection = connection.clone();
let restart_connection = connection.clone();
let (shutdown_tx, shutdown_rx) = mpsc::channel::<()>(1); let (shutdown_tx, shutdown_rx) = mpsc::channel::<()>(1);
let (query_tx, filter_rx) = mpsc::channel::<Containers>(1); let (query_tx, filter_rx) = mpsc::channel::<Containers>(1);
let (filter_tx, log_rx) = mpsc::channel::<Containers>(1); let (filter_tx, restart_rx) = mpsc::channel::<Containers>(1);
let interval = Duration::from_secs(10); let interval = Duration::from_secs(10);
let unhealthy_timeout = Duration::from_secs(35); let unhealthy_timeout = Duration::from_secs(35);
tokio::try_join!( tokio::try_join!(
tokio::spawn(query_task( tokio::spawn(query_task(
connection.clone(), query_connection,
interval, interval,
query_tx, query_tx,
shutdown_rx shutdown_rx
)), )),
tokio::spawn(filter_task(unhealthy_timeout, filter_rx, filter_tx)), tokio::spawn(filter_task(unhealthy_timeout, filter_rx, filter_tx)),
tokio::spawn(log_task(log_rx)), tokio::spawn(restart_task(restart_connection, restart_rx)),
tokio::spawn(async { tokio::spawn(async {
println!("shutdown -> sleep"); println!("shutdown -> sleep");
tokio::time::sleep(Duration::from_secs(80)).await; tokio::time::sleep(Duration::from_secs(80)).await;