feat: implement StdoutNotificationActor for get command

This commit is contained in:
2025-08-10 16:58:40 +02:00
parent dac46a1652
commit 91c5209266
3 changed files with 44 additions and 6 deletions

View File

@@ -0,0 +1,16 @@
use crate::app::ports::driven::ForSendingNotification;
pub struct StdoutNotificationActor;
impl StdoutNotificationActor {
pub fn new() -> Self {
StdoutNotificationActor
}
}
impl ForSendingNotification for StdoutNotificationActor {
fn send_notification(&self, message: &str) -> Result<(), Box<dyn std::error::Error>> {
println!("Sending notification: {message}");
Ok(())
}
}

View File

@@ -4,20 +4,21 @@ use system_monitor::SystemMonitor;
use crate::{actors, adapters, app};
pub fn get_service() -> app::service::Service {
pub fn get_run_service() -> app::service::Service {
dotenv().ok();
let notifier = DiscordNotifier::new(
std::env::var("DISCORD_WEBHOOK").expect("DISCORD_WEBHOOK environment variable not set"),
"System Monitor".to_string(),
"https://cdn.shopify.com/s/files/1/0262/1423/6212/files/Lord_of_the_Rings_eye_of_Sauron_-_Ghtic.com_-_Blog.png?v=1579680018".to_string(),
);
let monitor =
SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds());
let discord_adapter =
adapters::driven::for_sending_notification::discord_client::DiscordAdapter::new(notifier);
let monitor =
SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds());
let system_monitor_adapter =
adapters::driven::for_monitoring_system::system_monitor::SystemMonitorAdapter::new(monitor);
let formatter = actors::driven::for_formatting_message::alert_formatter::AlertFormatter;
app::service::new(
@@ -26,3 +27,20 @@ pub fn get_service() -> app::service::Service {
Box::new(formatter),
)
}
pub fn get_get_service() -> app::service::Service {
let notifier = actors::driven::for_sending_notification::print::StdoutNotificationActor::new();
let monitor =
SystemMonitor::new(system_monitor::resource_threshold::get_default_resource_thresholds());
let system_monitor_adapter =
adapters::driven::for_monitoring_system::system_monitor::SystemMonitorAdapter::new(monitor);
let formatter = actors::driven::for_formatting_message::alert_formatter::AlertFormatter;
app::service::new(
Box::new(notifier),
Box::new(system_monitor_adapter),
Box::new(formatter),
)
}

View File

@@ -18,6 +18,9 @@ mod actors {
pub mod for_formatting_message {
pub mod alert_formatter;
}
pub mod for_sending_notification {
pub mod print;
}
}
}
@@ -42,15 +45,16 @@ enum Commands {
fn main() {
let cli = Cli::parse();
let service = configurator::get_service();
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::Get => {
let service = configurator::get_get_service();
if let Err(e) = service.send_all_metrics_notification() {
eprintln!("Error sending final notification: {e}");
}