Skip to content

Commit

Permalink
Add timeout to watcher iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed May 24, 2024
1 parent 1a222c3 commit 13cea8b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 14 additions & 5 deletions watchers/src/watchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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,
ActiveWindow,
}

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,
Expand Down Expand Up @@ -130,10 +130,19 @@ pub async fn run_first_supported(client: Arc<ReportClient>, 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;
}
}

Expand Down

0 comments on commit 13cea8b

Please sign in to comment.