cancellation

This commit is contained in:
2026-02-04 03:31:17 -08:00
parent 156df9ae86
commit 08871a5782
10 changed files with 268 additions and 141 deletions
+44 -33
View File
@@ -1,6 +1,8 @@
use std::{fs::File, io::Read, path::PathBuf, sync::Arc};
use async_trait::async_trait;
use tokio::select;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};
use crate::{
@@ -106,50 +108,59 @@ impl ChimemonSource for HwmonSource {
let sensors = config
.sensors
.iter()
.map(|(k, v)| Arc::new(HwmonSensor::new(k, &v.name, &v.sensor)))
.map(|(k, v)| Arc::new(HwmonSensor::new(k, &v.device, &v.sensor)))
.collect();
debug!("config: {config:?}");
HwmonSource {
name: name.to_owned(),
config,
sensors,
}
}
async fn run(self, chan: ChimemonSourceChannel) {
async fn run(self, chan: ChimemonSourceChannel, cancel: CancellationToken) {
info!("hwmon task started");
let mut interval = tokio::time::interval(self.config.interval);
loop {
interval.tick().await;
let mut values = Vec::new();
for s in &self.sensors {
if let Ok(sensor_val) = HwmonSource::get_raw_value(s).await {
debug!(
"hwmon {} raw value {}",
s.value_path.to_string_lossy(),
sensor_val
);
if let Ok(parsed) = sensor_val.trim().parse::<f64>() {
values.push((s.clone(), parsed));
} else {
error!(
"Unable to parse sensor value {sensor_val} at {}",
s.value_path.to_string_lossy()
);
select! {
_ = cancel.cancelled() => { return; },
_ = interval.tick() => {
let mut values = Vec::new();
for s in &self.sensors {
debug!("Sensor {s:?}");
match HwmonSource::get_raw_value(s).await {
Ok(sensor_val) => {
debug!(
"hwmon {} raw value {}",
s.value_path.to_string_lossy(),
sensor_val
);
if let Ok(parsed) = sensor_val.trim().parse::<f64>() {
values.push((s.clone(), parsed));
} else {
error!(
"Unable to parse sensor value {sensor_val} at {}",
s.value_path.to_string_lossy()
);
}
},
Err(e) => {
error!("Unable to get hwmon sensor value ({})", e.to_string());
continue
}
}
}
let report = SourceReport {
name: self.name.clone(),
status: SourceStatus::Healthy,
details: Arc::new(HwmonReport { values }),
};
info!("Writing hwmon data");
match chan.send(report.into()) {
Ok(_) => {}
Err(e) => {
warn!("Unable to send to message channel ({e})")
}
}
} else {
error!("Unable to get hwmon sensor value");
}
}
let report = SourceReport {
name: self.name.clone(),
status: SourceStatus::Healthy,
details: Arc::new(HwmonReport { values }),
};
info!("Writing hwmon data");
match chan.send(report.into()) {
Ok(_) => {}
Err(e) => {
warn!("Unable to send to message channel ({e})")
}
}
}