From fd9a1114b9f9ccc30919e1f3b3f90ecf6d03f8c4 Mon Sep 17 00:00:00 2001 From: JeremyLARDENOIS Date: Sun, 10 Aug 2025 17:41:10 +0200 Subject: [PATCH] feat: implement driving cron actor --- src/actors/driving/cron_alerts.rs | 21 +++++++++++++++++++ src/adapters/driving/for_monitoring_alerts.rs | 18 ++++++++++++++++ src/app/ports.rs | 1 + src/app/ports/driving.rs | 3 +++ src/configurator.rs | 12 ++++++++--- src/main.rs | 21 ++++++++++++------- 6 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 src/actors/driving/cron_alerts.rs create mode 100644 src/adapters/driving/for_monitoring_alerts.rs create mode 100644 src/app/ports/driving.rs diff --git a/src/actors/driving/cron_alerts.rs b/src/actors/driving/cron_alerts.rs new file mode 100644 index 0000000..58c9fc1 --- /dev/null +++ b/src/actors/driving/cron_alerts.rs @@ -0,0 +1,21 @@ +use crate::app::ports::driving::ForMonitoringAlerts; + +pub struct CronAlertsActor { + alert_monitor: Box, + period: std::time::Duration, +} + +impl CronAlertsActor { + pub fn new(alert_monitor: Box, period: std::time::Duration) -> Self { + Self { alert_monitor, period } + } + + pub fn start(&self) { + loop { + if let Err(e) = self.alert_monitor.alert_on_threshold_violation() { + eprintln!("Error occurred: {}", e); + } + std::thread::sleep(self.period); + } + } +} diff --git a/src/adapters/driving/for_monitoring_alerts.rs b/src/adapters/driving/for_monitoring_alerts.rs new file mode 100644 index 0000000..5448c61 --- /dev/null +++ b/src/adapters/driving/for_monitoring_alerts.rs @@ -0,0 +1,18 @@ +use crate::app::ports::driving::ForMonitoringAlerts; + +pub struct ForMonitoringAlertsAdapter +{ + app : crate::app::service::Service +} + +impl ForMonitoringAlertsAdapter { + pub fn new(app: crate::app::service::Service) -> Self { + ForMonitoringAlertsAdapter { app } + } +} + +impl ForMonitoringAlerts for ForMonitoringAlertsAdapter { + fn alert_on_threshold_violation(&self) -> Result<(), Box> { + self.app.alert_on_threshold_violation() + } +} diff --git a/src/app/ports.rs b/src/app/ports.rs index a8378da..4c76b42 100644 --- a/src/app/ports.rs +++ b/src/app/ports.rs @@ -1 +1,2 @@ pub mod driven; +pub mod driving; diff --git a/src/app/ports/driving.rs b/src/app/ports/driving.rs new file mode 100644 index 0000000..b6934a9 --- /dev/null +++ b/src/app/ports/driving.rs @@ -0,0 +1,3 @@ +pub trait ForMonitoringAlerts { + fn alert_on_threshold_violation(&self) -> Result<(), Box>; +} diff --git a/src/configurator.rs b/src/configurator.rs index 3b65630..0be264c 100644 --- a/src/configurator.rs +++ b/src/configurator.rs @@ -4,7 +4,9 @@ use system_monitor::SystemMonitor; use crate::{actors, adapters, app}; -pub fn get_run_service() -> app::service::Service { +pub fn get_cron_actor( + duration: std::time::Duration, +) -> actors::driving::cron_alerts::CronAlertsActor { dotenv().ok(); let notifier = DiscordNotifier::new( std::env::var("DISCORD_WEBHOOK").expect("DISCORD_WEBHOOK environment variable not set"), @@ -21,11 +23,15 @@ pub fn get_run_service() -> app::service::Service { let formatter = actors::driven::for_formatting_message::alert_formatter::AlertFormatter; - app::service::new( + let service = app::service::new( Box::new(discord_adapter), Box::new(system_monitor_adapter), Box::new(formatter), - ) + ); + let service_adapter = + crate::adapters::driving::for_monitoring_alerts::ForMonitoringAlertsAdapter::new(service); + + actors::driving::cron_alerts::CronAlertsActor::new(Box::new(service_adapter), duration) } pub fn get_get_service() -> app::service::Service { diff --git a/src/main.rs b/src/main.rs index 47fbd70..93f5ad9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,9 @@ mod adapters { pub mod for_monitoring_system; pub mod for_sending_notification; } + pub mod driving { + pub mod for_monitoring_alerts; + } } mod actors { @@ -22,6 +25,9 @@ mod actors { pub mod print; } } + pub mod driving { + pub mod cron_alerts; + } } mod configurator; @@ -37,7 +43,11 @@ struct Cli { #[derive(Subcommand)] enum Commands { /// Monitor system and alert on threshold violations - Run, + Run { + /// Duration between cron checks in seconds + #[arg(short, long, default_value = "300")] + duration: u64, + }, /// Send all system metrics notification Get, } @@ -45,13 +55,10 @@ enum Commands { fn main() { let cli = Cli::parse(); - match cli.command { - Commands::Run => { - let service = configurator::get_run_service(); - if let Err(e) = service.alert_on_threshold_violation() { - eprintln!("Error during threshold violation check: {e}"); - } + Commands::Run { duration } => { + let duration = std::time::Duration::from_secs(duration); + configurator::get_cron_actor(duration).start(); } Commands::Get => { let service = configurator::get_get_service();