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

Add --delay-startup argument to Ark #35

Merged
merged 5 commits into from
Jun 15, 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
80 changes: 80 additions & 0 deletions 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 crates/ark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ libR-sys = "0.5.0"
libc = "0.2"
log = "0.4.17"
nix = { version = "0.26.2", features = ["signal"] }
notify = "6.0.0"
once_cell = "1.17.1"
parking_lot = "0.12.1"
regex = "1.7.1"
Expand Down
73 changes: 70 additions & 3 deletions crates/ark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ use ark::shell::Shell;
use ark::version::detect_r;
use bus::Bus;
use crossbeam::channel::bounded;
use crossbeam::channel::unbounded;
use log::*;
use nix::sys::signal::*;
use notify::Watcher;
use stdext::unwrap;

fn start_kernel(connection_file: ConnectionFile, capture_streams: bool) {
Expand Down Expand Up @@ -203,6 +205,8 @@ fn main() {

let mut connection_file: Option<String> = None;
let mut log_file: Option<String> = None;
let mut startup_notifier_file: Option<String> = None;
let mut startup_delay: Option<std::time::Duration> = None;
let mut has_action = false;
let mut capture_streams = true;

Expand Down Expand Up @@ -241,23 +245,86 @@ fn main() {
break;
}
},
"--startup-notifier-file" => {
if let Some(file) = argv.next() {
startup_notifier_file = Some(file);
} else {
eprintln!(
"A notification file must be specified with the --startup-notifier-file argument."
);
break;
}
},
"--startup-delay" => {
if let Some(delay_arg) = argv.next() {
if let Ok(delay) = delay_arg.parse::<u64>() {
startup_delay = Some(std::time::Duration::from_millis(delay));
} else {
eprintln!("Can't parse delay in milliseconds");
break;
}
} else {
eprintln!(
"A delay in milliseconds must be specified with the --startup-delay argument."
);
break;
}
},
other => {
eprintln!("Argument '{}' unknown", other);
break;
},
}
}

// Initialize the logger.
logger::initialize(log_file.as_deref());

if let Some(file) = startup_notifier_file {
let path = std::path::Path::new(&file);
let (tx, rx) = unbounded();

if let Err(err) = (|| -> anyhow::Result<()> {
let config = notify::Config::default()
.with_poll_interval(std::time::Duration::from_millis(2))
.with_compare_contents(false);

let mut watcher = notify::RecommendedWatcher::new(tx, config).unwrap();
watcher.watch(path, notify::RecursiveMode::NonRecursive)?;

loop {
let ev = rx.recv()?;
match ev.unwrap().kind {
notify::event::EventKind::Modify(_) => {
break;
},
notify::event::EventKind::Remove(_) => {
break;
},
_ => {
continue;
},
}
}

watcher.unwatch(path)?;
Ok(())
})() {
eprintln!("Problem with the delay file: {:?}", err);
}
}

if let Some(delay) = startup_delay {
std::thread::sleep(delay);
}

// If the user didn't specify an action, print the usage instructions and
// exit
if !has_action {
print_usage();
return;
}

// Initialize the logger.
logger::initialize(log_file.as_deref());

// Register segfault handler to get a backtrace. Should be after
// initialising `log!`. Note that R will not override this handler
// because we set `R_SignalHandlers` to 0 before startup.
Expand Down