mirror of
https://github.com/2e3s/awatcher.git
synced 2025-06-05 19:15:33 +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]]
|
[[package]]
|
||||||
name = "awatcher"
|
name = "awatcher"
|
||||||
version = "0.2.6"
|
version = "0.2.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"aw-datastore",
|
"aw-datastore",
|
||||||
@ -4147,7 +4147,7 @@ checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "watchers"
|
name = "watchers"
|
||||||
version = "0.2.6"
|
version = "0.2.7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
@ -18,7 +18,7 @@ image = { version = "0.25.1" }
|
|||||||
members = ["watchers"]
|
members = ["watchers"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.2.6"
|
version = "0.2.7"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
anyhow = "1.0.83"
|
anyhow = "1.0.83"
|
||||||
|
@ -18,7 +18,7 @@ mod x11_window;
|
|||||||
use crate::{config::Config, report_client::ReportClient};
|
use crate::{config::Config, report_client::ReportClient};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use std::{fmt::Display, sync::Arc};
|
use std::{fmt::Display, sync::Arc};
|
||||||
use tokio::time;
|
use tokio::time::{sleep, timeout, Duration};
|
||||||
|
|
||||||
pub enum WatcherType {
|
pub enum WatcherType {
|
||||||
Idle,
|
Idle,
|
||||||
@ -26,7 +26,7 @@ pub enum WatcherType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl WatcherType {
|
impl WatcherType {
|
||||||
fn sleep_time(&self, config: &Config) -> time::Duration {
|
fn sleep_time(&self, config: &Config) -> Duration {
|
||||||
match self {
|
match self {
|
||||||
WatcherType::Idle => config.poll_time_idle,
|
WatcherType::Idle => config.poll_time_idle,
|
||||||
WatcherType::ActiveWindow => config.poll_time_window,
|
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 {
|
if let Some(mut watcher) = supported_watcher {
|
||||||
info!("Starting {watcher_type} watcher");
|
info!("Starting {watcher_type} watcher");
|
||||||
loop {
|
loop {
|
||||||
if let Err(e) = watcher.run_iteration(&client).await {
|
let sleep_time = watcher_type.sleep_time(&client.config);
|
||||||
error!("Error on {watcher_type} iteration: {e}");
|
|
||||||
|
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