use crate::utils::error::gateway_error::Result;
use std::time::Duration;
use tracing::{debug, warn};
use super::system::MonitoringSystem;
impl MonitoringSystem {
pub(super) async fn start_background_tasks(&self) -> Result<()> {
let monitoring = self.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(60));
loop {
interval.tick().await;
if let Err(e) = monitoring.aggregate_metrics().await {
warn!("Failed to aggregate metrics: {}", e);
}
}
});
let monitoring = self.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(30));
loop {
interval.tick().await;
if let Err(e) = monitoring.run_health_checks().await {
warn!("Health check failed: {}", e);
}
}
});
if self.alerts.is_some() {
let monitoring = self.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(10));
loop {
interval.tick().await;
if let Err(e) = monitoring.process_alerts().await {
warn!("Failed to process alerts: {}", e);
}
}
});
}
Ok(())
}
pub(super) async fn aggregate_metrics(&self) -> Result<()> {
debug!("Aggregating metrics");
let _metrics = self.get_metrics().await?;
Ok(())
}
pub(super) async fn run_health_checks(&self) -> Result<()> {
debug!("Running health checks");
Ok(())
}
pub(super) async fn process_alerts(&self) -> Result<()> {
if let Some(alerts) = &self.alerts {
alerts.process_pending().await?;
}
Ok(())
}
}