1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

config: modularize; add config watcher and move error variants

This commit is contained in:
NotAShelf 2025-05-14 01:17:42 +03:00
parent dde938b638
commit 9f7d86ff01
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
10 changed files with 349 additions and 126 deletions

View file

@ -1,4 +1,5 @@
use crate::config::{AppConfig, LogLevel};
use crate::config::watcher::ConfigWatcher;
use crate::conflict;
use crate::core::SystemReport;
use crate::engine;
@ -10,7 +11,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
/// Run the daemon
pub fn run_daemon(config: AppConfig, verbose: bool) -> Result<(), Box<dyn std::error::Error>> {
pub fn run_daemon(mut config: AppConfig, verbose: bool) -> Result<(), Box<dyn std::error::Error>> {
// Set effective log level based on config and verbose flag
let effective_log_level = if verbose {
LogLevel::Debug
@ -63,6 +64,34 @@ pub fn run_daemon(config: AppConfig, verbose: bool) -> Result<(), Box<dyn std::e
);
}
// Initialize config file watcher if a path is available
let config_file_path = if let Ok(path) = std::env::var("SUPERFREQ_CONFIG") { Some(path) } else {
let default_paths = [
"/etc/superfreq/config.toml",
"/etc/superfreq.toml",
];
default_paths.iter()
.find(|&path| std::path::Path::new(path).exists())
.map(|path| (*path).to_string())
};
let config_watcher: Option<ConfigWatcher> = match config_file_path {
Some(path) => {
match ConfigWatcher::new(&path) {
Ok(watcher) => {
println!("Watching config file: {path}");
Some(watcher)
},
Err(e) => {
eprintln!("Failed to initialize config file watcher: {e}");
None
}
}
},
None => None,
};
// Variables for adaptive polling
let mut current_poll_interval = config.daemon.poll_interval_sec;
let mut last_settings_change = Instant::now();
@ -72,6 +101,24 @@ pub fn run_daemon(config: AppConfig, verbose: bool) -> Result<(), Box<dyn std::e
while running.load(Ordering::SeqCst) {
let start_time = Instant::now();
// Check for configuration changes
if let Some(watcher) = &config_watcher {
if let Some(config_result) = watcher.check_for_changes() {
match config_result {
Ok(new_config) => {
if verbose {
println!("Config file changed, updating configuration");
}
config = new_config;
},
Err(e) => {
eprintln!("Error loading new configuration: {e}");
// Continue with existing config
}
}
}
}
match monitor::collect_system_report(&config) {
Ok(report) => {
log_message(