Skip to content

Commit

Permalink
Another attempt at fixing lock file issues
Browse files Browse the repository at this point in the history
  • Loading branch information
WombatFromHell committed Jun 21, 2024
1 parent 12daba8 commit 12e5599
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "veridian-controller"
version = "0.2.2"
version = "0.2.3"
edition = "2021"

[dependencies]
Expand Down
132 changes: 72 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clap::Parser;
use signal_hook::flag;
use std::error::Error;
use std::panic::catch_unwind;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -30,70 +29,83 @@ fn cleanup() -> Result<(), Box<dyn Error>> {

fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let terminate = Arc::new(AtomicBool::new(false));
let terminate_clone = Arc::clone(&terminate);

// ensure only one copy of the program is running at a time
match filelock::acquire_lock() {
Ok(_) => {
// try to disable fan control if we panic for whatever reason
std::panic::set_hook(Box::new(|panic_info| {
// capture the panic cause and try to pass it along
// capture the panic cause and try to pass it along
if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
eprintln!("Panic: {}", payload);
} else if let Some(payload) = panic_info.payload().downcast_ref::<String>() {
eprintln!("Panic: {}", payload);
} else {
eprintln!("Panic: {:?}", panic_info.payload());
}
}));

let terminate = Arc::new(AtomicBool::new(false));

// register some common termination signals for use with Ctrl+C and SystemD
flag::register(signal_hook::consts::SIGTERM, Arc::clone(&terminate))?;
flag::register(signal_hook::consts::SIGABRT, Arc::clone(&terminate))?;
flag::register(signal_hook::consts::SIGINT, Arc::clone(&terminate))?;

commands::set_fan_control(1)?;
let shared_config = Arc::new(Mutex::new(config::load_config_from_env(args.file)?));

let thermal_thread = thread::spawn(move || {
let config = shared_config.lock().unwrap();
let thermal_manager =
Arc::new(Mutex::new(thermalmanager::ThermalManager::new(&config)));

while !terminate.load(Ordering::Relaxed) {
let result = catch_unwind(|| {
thermal_manager.lock().unwrap().update_temperature();
thermal_manager
.lock()
.unwrap()
.set_target_fan_speed()
.unwrap();
});
match result {
Ok(_) => {}
Err(e) => {
eprintln!("Error: {:?}", e);
std::process::exit(1);
}
}
let _ = filelock::acquire_lock()?;

thread::sleep(Duration::from_secs(config.global_delay));
}
});
for sig in &[
signal_hook::consts::SIGTERM,
signal_hook::consts::SIGINT,
signal_hook::consts::SIGABRT,
] {
signal_hook::flag::register(*sig, Arc::clone(&terminate))?;
}

let _ = thermal_thread.join().unwrap_or_else(|err| {
eprintln!("Error in thread: {:?}", err);
});
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
eprintln!("Panic occurred: {:?}", panic_info);
default_panic(panic_info);
// try to ensure cleanup is called
cleanup().unwrap();
std::process::exit(1);
}));

cleanup()?;
thread::spawn(move || {
while !terminate_clone.load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(100));
}
Err(err) => {
eprintln!("Error: {}", err);
std::process::exit(1);
eprintln!("Termination signal received");
if let Err(e) = cleanup() {
eprintln!("Error during cleanup: {:?}", e);
}
}
std::process::exit(0);
});

Ok(())
let result = std::panic::catch_unwind(|| {
commands::set_fan_control(1)?;
let shared_config = Arc::new(Mutex::new(config::load_config_from_env(args.file)?));

let thermal_thread = thread::spawn(move || {
let config = shared_config.lock().unwrap();
let thermal_manager =
Arc::new(Mutex::new(thermalmanager::ThermalManager::new(&config)));

while !terminate.load(Ordering::Relaxed) {
let result = catch_unwind(|| {
thermal_manager.lock().unwrap().update_temperature();
thermal_manager
.lock()
.unwrap()
.set_target_fan_speed()
.unwrap();
});
match result {
Ok(_) => {}
Err(e) => {
eprintln!("Error: {:?}", e);
std::process::exit(1);
}
}

thread::sleep(Duration::from_secs(config.global_delay));
}
});

let _ = thermal_thread.join().unwrap_or_else(|err| {
eprintln!("Error in thread: {:?}", err);
});

cleanup()?;

Ok(())
});

match result {
Ok(inner_result) => inner_result,
Err(_) => {
eprintln!("Program panicked");
Err("Program panicked".into())
}
}
}

0 comments on commit 12e5599

Please sign in to comment.