diff --git a/Cargo.lock b/Cargo.lock index 467382f..ab89d54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,7 +504,7 @@ dependencies = [ [[package]] name = "awatcher" -version = "0.2.6" +version = "0.2.7" dependencies = [ "anyhow", "aw-datastore", @@ -4147,7 +4147,7 @@ checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" [[package]] name = "watchers" -version = "0.2.6" +version = "0.2.7" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 8a860d9..e6d92ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ image = { version = "0.25.1" } members = ["watchers"] [workspace.package] -version = "0.2.6" +version = "0.2.7" [workspace.dependencies] anyhow = "1.0.83" diff --git a/watchers/src/watchers.rs b/watchers/src/watchers.rs index f4e5ddd..c1137ce 100644 --- a/watchers/src/watchers.rs +++ b/watchers/src/watchers.rs @@ -18,7 +18,7 @@ mod x11_window; use crate::{config::Config, report_client::ReportClient}; use async_trait::async_trait; use std::{fmt::Display, sync::Arc}; -use tokio::time; +use tokio::time::{sleep, timeout, Duration}; pub enum WatcherType { Idle, @@ -26,7 +26,7 @@ pub enum WatcherType { } impl WatcherType { - fn sleep_time(&self, config: &Config) -> time::Duration { + fn sleep_time(&self, config: &Config) -> Duration { match self { WatcherType::Idle => config.poll_time_idle, WatcherType::ActiveWindow => config.poll_time_window, @@ -130,10 +130,19 @@ pub async fn run_first_supported(client: Arc, watcher_type: &Watch if let Some(mut watcher) = supported_watcher { info!("Starting {watcher_type} watcher"); loop { - if let Err(e) = watcher.run_iteration(&client).await { - error!("Error on {watcher_type} iteration: {e}"); + let sleep_time = watcher_type.sleep_time(&client.config); + + match timeout(sleep_time, watcher.run_iteration(&client)).await { + Ok(Ok(())) => { /* Successfully completed. */ } + Ok(Err(e)) => { + error!("Error on {watcher_type} iteration: {e}"); + } + Err(_) => { + error!("Timeout on {watcher_type} iteration after {:?}", sleep_time); + } } - time::sleep(watcher_type.sleep_time(&client.config)).await; + + sleep(sleep_time).await; } }