Add settings to disable idle or window watcher

This commit is contained in:
Demmie 2025-02-08 02:27:52 -05:00
parent b07d3958bb
commit 88d1920417
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
4 changed files with 59 additions and 3 deletions

View File

@ -74,6 +74,8 @@ host = "127.0.0.1"
idle-timeout-seconds=180
poll-time-idle-seconds=4
poll-time-window-seconds=1
disable-idle-watcher=false
disable-window-watcher=false
[[awatcher.filters]]
# match only "navigator"

View File

@ -15,6 +15,8 @@ pub struct RunnerConfig {
pub config_file: PathBuf,
#[cfg(feature = "bundle")]
pub no_tray: bool,
pub disable_idle_watcher: bool,
pub disable_window_watcher: bool,
}
pub fn setup_logger(verbosity: LevelFilter) -> Result<(), fern::InitError> {
@ -70,6 +72,12 @@ pub fn from_cli() -> anyhow::Result<RunnerConfig> {
arg!(--"no-server" "Don't send data to the ActivityWatch server")
.value_parser(value_parser!(bool))
.action(ArgAction::SetTrue),
arg!(--"disable-idle-watcher" "Don't watch for idle activity")
.value_parser(value_parser!(bool))
.action(ArgAction::SetTrue),
arg!(--"disable-window-watcher" "Don't watch for window activity")
.value_parser(value_parser!(bool))
.action(ArgAction::SetTrue),
#[cfg(feature = "bundle")]
arg!(--"no-tray" "Don't use the bundled tray, run only server and watchers in the background")
.value_parser(value_parser!(bool))
@ -106,6 +114,8 @@ pub fn from_cli() -> anyhow::Result<RunnerConfig> {
config_file: config.config_file,
#[cfg(feature = "bundle")]
no_tray: *matches.get_one("no-tray").unwrap(),
disable_idle_watcher: *matches.get_one("disable-idle-watcher").unwrap(),
disable_window_watcher: *matches.get_one("disable-window-watcher").unwrap(),
})
}
@ -143,6 +153,17 @@ fn merge_cli(config: &mut FileConfig, matches: &ArgMatches) {
get_arg_value("port", matches, &mut config.server.port);
#[cfg(not(feature = "bundle"))]
get_arg_value("host", matches, &mut config.server.host);
get_arg_value(
"disable-idle-watcher",
matches,
&mut config.client.disable_idle_watcher,
);
get_arg_value(
"disable-window-watcher",
matches,
&mut config.client.disable_window_watcher,
);
}
fn get_arg_value<T>(id: &str, matches: &ArgMatches, config_value: &mut T)

View File

@ -8,8 +8,9 @@ extern crate log;
mod bundle;
mod config;
use std::error::Error;
use std::pin::Pin;
use std::sync::Arc;
use std::{error::Error, future::Future};
use tokio::signal::unix::{signal, SignalKind};
#[cfg(feature = "bundle")]
use tokio::sync::mpsc;
@ -22,8 +23,20 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
let no_tray = config.no_tray;
#[cfg(feature = "bundle")]
let config_file = config.config_file;
let disable_idle_watcher = config.disable_idle_watcher;
let disable_window_watcher = config.disable_window_watcher;
let config = config.watchers_config;
#[cfg(not(feature = "bundle"))]
if disable_idle_watcher && disable_window_watcher {
error!("Both watchers are disabled");
return Err("At least one watcher must be enabled".into());
}
#[cfg(feature = "bundle")]
if disable_idle_watcher && disable_window_watcher {
warn!("Both watchers are disabled");
}
if config.no_server {
warn!(
"Not sending to server {}:{}",
@ -58,8 +71,22 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
let client = Arc::new(ReportClient::new(config).await?);
let idle_future = run_first_supported(Arc::clone(&client), &WatcherType::Idle);
let active_window_future = run_first_supported(Arc::clone(&client), &WatcherType::ActiveWindow);
let idle_future: Pin<Box<dyn Future<Output = bool> + Send>> = if disable_idle_watcher {
Box::pin(std::future::pending())
} else {
Box::pin(run_first_supported(Arc::clone(&client), &WatcherType::Idle))
};
let active_window_future: Pin<Box<dyn Future<Output = bool> + Send>> = if disable_window_watcher
{
Box::pin(std::future::pending::<bool>())
} else {
Box::pin(run_first_supported(
Arc::clone(&client),
&WatcherType::ActiveWindow,
))
};
let sigterm = async {
signal(SignalKind::terminate()).unwrap().recv().await;
warn!("Caught SIGTERM, shutting down...");

View File

@ -69,6 +69,10 @@ pub struct ClientConfig {
pub poll_time_window_seconds: u32,
#[serde(default)]
pub filters: Vec<Filter>,
#[serde(default)]
pub disable_idle_watcher: bool,
#[serde(default)]
pub disable_window_watcher: bool,
}
impl ClientConfig {
@ -168,6 +172,8 @@ host = "http://address.com"
idle-timeout-seconds=14
poll-time-idle-seconds=13
poll-time-window-seconds=12
disable-idle-watcher=false
disable-window-watcher=false
# Add as many filters as needed.
# There should be at least 1 match field, and at least 1 replace field.