Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust scheduler: termination flag #346

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions v2/rust/Cargo.lock

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

1 change: 1 addition & 0 deletions v2/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = { version = "*", features = ["backtrace"] }
atomicwrites = "*"
chrono = "0.4.31"
clap = { version = "*", features = ["derive"] }
ctrlc = "*"
flexi_logger = "*"
log = "*"
quick-xml = { version = "0.30.0", features = ["serialize"] }
Expand Down
19 changes: 17 additions & 2 deletions v2/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ mod logging;
pub mod parse_xml;
mod results;
mod setup;
mod termination;

use anyhow::{Context, Result};
use clap::Parser;
use log::{debug, info};
use log::{debug, info, warn};
use logging::log_and_return_error;
use std::process::exit;
use std::thread::sleep;
use std::time::Duration;

fn main() -> Result<()> {
let args = cli::Args::parse();
Expand All @@ -28,5 +32,16 @@ fn main() -> Result<()> {
.map_err(log_and_return_error)?;
debug!("Setup completed");

Ok(())
let termination_flag = termination::start_termination_control()
.context("Failed to set up termination control")
.map_err(log_and_return_error)?;
debug!("Termination control set up");

loop {
if termination_flag.should_terminate() {
warn!("Termination signal received, shutting down");
exit(1);
}
sleep(Duration::from_millis(100))
}
}
22 changes: 22 additions & 0 deletions v2/rust/src/termination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::{Context, Result};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

pub fn start_termination_control() -> Result<TerminationFlag> {
let raw_flag = Arc::new(AtomicBool::new(false));
let raw_flag_clone = raw_flag.clone();
ctrlc::set_handler(move || {
raw_flag_clone.store(true, Ordering::Relaxed);
})
.context("Failed to register signal handler for CTRL+C")?;
Ok(TerminationFlag(raw_flag))
}

#[derive(Clone)]
pub struct TerminationFlag(Arc<AtomicBool>);

impl TerminationFlag {
pub fn should_terminate(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}