feat: implement driving cron actor
This commit is contained in:
21
src/actors/driving/cron_alerts.rs
Normal file
21
src/actors/driving/cron_alerts.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::app::ports::driving::ForMonitoringAlerts;
|
||||
|
||||
pub struct CronAlertsActor {
|
||||
alert_monitor: Box<dyn ForMonitoringAlerts>,
|
||||
period: std::time::Duration,
|
||||
}
|
||||
|
||||
impl CronAlertsActor {
|
||||
pub fn new(alert_monitor: Box<dyn ForMonitoringAlerts>, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/adapters/driving/for_monitoring_alerts.rs
Normal file
18
src/adapters/driving/for_monitoring_alerts.rs
Normal file
@@ -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<dyn std::error::Error>> {
|
||||
self.app.alert_on_threshold_violation()
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
pub mod driven;
|
||||
pub mod driving;
|
||||
|
||||
3
src/app/ports/driving.rs
Normal file
3
src/app/ports/driving.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub trait ForMonitoringAlerts {
|
||||
fn alert_on_threshold_violation(&self) -> Result<(), Box<dyn std::error::Error>>;
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
21
src/main.rs
21
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();
|
||||
|
||||
Reference in New Issue
Block a user