running parallel tokio tasks for heartbeat and metrics
This commit is contained in:
@@ -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")]
|
||||
|
@@ -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) {
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -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,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user