running parallel tokio tasks for heartbeat and metrics

This commit is contained in:
2025-08-09 22:00:42 +02:00
parent a1bbbedd75
commit ac2fce75a0
7 changed files with 93 additions and 28 deletions

View File

@@ -11,7 +11,7 @@ pub struct CpuInfo {
pub current_temp: Option<f64>,
}
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error>> {
pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error + Send + Sync>> {
let mut sys = System::new();
sys.refresh_cpu_all();
@@ -28,13 +28,13 @@ pub async fn get_cpu_info() -> Result<CpuInfo, Box<dyn Error>> {
})
}
pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error>> {
pub async fn get_cpu_load(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_cpu_all();
tokio::task::yield_now().await; // Allow other tasks to run
Ok(sys.global_cpu_usage() as f64)
}
pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error>> {
pub async fn get_cpu_temp() -> Result<f64, Box<dyn Error + Send + Sync>> {
println!("Attempting to get CPU temperature...");
#[cfg(target_os = "linux")]

View File

@@ -11,7 +11,7 @@ pub struct GpuInfo {
pub vram_used: Option<f64>,
}
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error + Send + Sync>> {
match get_gpu_metrics() {
Ok((gpu_temp, gpu_load, vram_used, vram_total)) => {
let gpu_name = detect_gpu_name();
@@ -37,7 +37,7 @@ pub async fn get_gpu_info() -> Result<GpuInfo, Box<dyn Error>> {
}
}
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error>> {
pub fn get_gpu_metrics() -> Result<(f64, f64, f64, f64), Box<dyn Error + Send + Sync>> {
let nvml = Nvml::init();
if let Ok(nvml) = nvml {
if let Ok(device) = nvml.device_by_index(0) {

View File

@@ -21,7 +21,7 @@ pub async fn get_memory_info() -> Result<MemoryInfo> {
})
}
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error>> {
pub fn _get_memory_usage(sys: &mut System) -> Result<f64, Box<dyn Error + Send + Sync>> {
sys.refresh_memory();
Ok((sys.used_memory() as f64 / sys.total_memory() as f64) * 100.0)
}

View File

@@ -25,14 +25,24 @@ pub struct HardwareInfo {
}
impl HardwareInfo {
pub async fn collect() -> anyhow::Result<Self, Box<dyn Error>> {
pub async fn collect() -> Result<Self, Box<dyn Error + Send + Sync>> {
let mut network_monitor = network::NetworkMonitor::new();
Ok(Self {
cpu: get_cpu_info().await?,
gpu: get_gpu_info().await?,
memory: get_memory_info().await?,
disk: get_disk_info().await?,
network: get_network_info(&mut network_monitor).await?,
network: match get_network_info(&mut network_monitor).await {
Ok(info) => info,
Err(e) => {
eprintln!("Error collecting network info: {}", e);
network::NetworkInfo {
interfaces: None,
rx_rate: None,
tx_rate: None,
}
}
},
network_monitor,
})
}