mirror of
https://github.com/2e3s/awatcher.git
synced 2025-06-04 02:20:15 +00:00
Add timeout to watcher iteration
This commit is contained in:
parent
1a222c35c9
commit
13cea8bed6
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -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",
|
||||
|
@ -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"
|
||||
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user