Extract file IO from mutex lock zone.

This commit is contained in:
Félix Baylac-Jacqué 2022-06-17 12:21:36 +02:00
parent a22dec763c
commit 4cf347c5eb
Signed by: picnoir
GPG Key ID: EFD315F31848DBA4
1 changed files with 8 additions and 7 deletions

15
main.rs
View File

@ -33,19 +33,20 @@ fn main() -> std::io::Result<()> {
let thread_metrics = metrics.clone();
thread::spawn(move || {
let filepath = prometheus_file_path.to_owned();
let mut prom_str: String = "".to_owned();
loop {
{
match File::create(&filepath) {
Ok(mut file) => {
if let Ok(m) = thread_metrics.lock() {
let prom_str = format!("officetemp_humidity {}\nofficetemp_temperature {}\n", m.humidity, m.temperature);
match file.write_all(prom_str.as_bytes()) {
Ok(()) => (),
Err(err) => {
eprintln!("Cannot write to {}: {}", prometheus_file_path, err);
}
};
prom_str = format!("officetemp_humidity {}\nofficetemp_temperature {}\n", m.humidity, m.temperature);
}
match file.write_all((&prom_str).as_bytes()) {
Ok(()) => (),
Err(err) => {
eprintln!("Cannot write to {}: {}", prometheus_file_path, err);
}
};
},
Err(err) => {
eprintln!("Cannot write to {}: {}", prometheus_file_path, err);