Skip to content

Commit

Permalink
introduce while_run_outside for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
TyberiusPrime committed Dec 5, 2023
1 parent 548e89a commit 385ae48
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 11 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 = "anysnake2"
version = "1.14.0"
version = "1.15.0"
authors = ["Florian Finkernagel <[email protected]>"]
edition = "2021"

Expand Down
19 changes: 19 additions & 0 deletions examples/basic/anysnake2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ cd /project
jupyter notebook
"""

[cmd.test_pre_post]
pre_run_outside = """
echo "pre_run"
"""
while_run_outside ="""
while :
do
# write pid to pre_run.txt
echo "$BASHPID" > while_run.txt
sleep 1;
done
"""
run = """
echo "run"
"""
post_run_outside = """
echo "post_run"
"""

[cmd.shell]
run = """fish
"""
13 changes: 12 additions & 1 deletion examples/full/anysnake2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,22 @@ pre_run_outside = """
echo "pre run outside"
"""

# runs independend of the sucess inside
# runs independend of the sucess inside (same as post_run_inside)
post_run_outside = """
echo 'post run outside'
"""

# this runs *while* the singularity process is running,
# and get's killed() when the singularity process finishes
while_run_outside ="""
while :
do
# usage idea: snapshot zfs
sleep 100;
done
"""


[cmd.shell]
run = """fish
"""
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ impl Default for FlakeUtil {
pub struct Cmd {
pub run: String,
pub pre_run_outside: Option<String>,
pub while_run_outside: Option<String>,
pub post_run_inside: Option<String>,
pub post_run_outside: Option<String>,
}
Expand Down
41 changes: 33 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ fn inner_main() -> Result<()> {
fs::create_dir_all(&run_dir).context("Failed to create run dir for scripts")?;
let post_run_sh: PathBuf = run_dir.join("post_run.sh");
let mut post_run_outside: Option<String> = None;
let mut parallel_running_child: Option<std::process::Child> = None;

if cmd == "run" {
let slop = matches.subcommand().unwrap().1.get_many::<String>("slop");
Expand All @@ -572,10 +573,15 @@ fn inner_main() -> Result<()> {
match &cmd_info.pre_run_outside {
Some(bash_script) => {
info!("Running pre_run_outside for cmd - cmd {}", cmd);
run_bash(bash_script).context("pre run outside failed")?;
run_bash(bash_script).with_context(|| format!("pre run outside failed. Script:\n{}", add_line_numbers(bash_script)))?;
}
None => {}
};
if let Some(while_run_outside) = &cmd_info.while_run_outside {
parallel_running_child = Some(spawn_bash(while_run_outside)?);


}
info!("Running singularity - cmd {}", cmd);
let run_template = std::include_str!("run.sh");
let run_script = run_template.replace("%RUN%", &cmd_info.run);
Expand Down Expand Up @@ -735,11 +741,15 @@ fn inner_main() -> Result<()> {
if let Some(bash_script) = post_run_outside {
if let Err(e) = run_bash(&bash_script) {
warn!(
"An error occured when running the post_run_outside bash script: {}",
e
"An error occured when running the post_run_outside bash script: {}\nScript: {}",
e,
add_line_numbers(&bash_script)
)
}
};
if let Some(mut parallel_running_child) = parallel_running_child {
parallel_running_child.kill().context("Failed to kill parallel running child")?;
}
std::process::exit(
singularity_result
.code()
Expand Down Expand Up @@ -1114,13 +1124,19 @@ fn rebuild_flake(
}
}

fn spawn_bash(script: &str) -> Result<std::process::Child> {
let mut child = Command::new("bash").stdin(Stdio::piped()).spawn()?;
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin.write_all(b"set -euo pipefail\n")?;
child_stdin.write_all(script.as_bytes())?;
child_stdin.write_all(b"\n")?;
Ok(child)

}

fn run_bash(script: &str) -> Result<()> {
run_without_ctrl_c(|| {
let mut child = Command::new("bash").stdin(Stdio::piped()).spawn()?;
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin.write_all(b"set -euo pipefail\n")?;
child_stdin.write_all(script.as_bytes())?;
child_stdin.write_all(b"\n")?;
let mut child = spawn_bash(script)?;
let ecode = child.wait().context("Failed to wait on bash")?; // closes stdin
if ecode.success() {
Ok(())
Expand Down Expand Up @@ -2277,3 +2293,12 @@ fn discover_newest_rev_hg(url: &str) -> Result<String> {
url
))
}


fn add_line_numbers(s: &str) -> String {
let mut out = String::new();
for (i, line) in s.lines().enumerate() {
out.push_str(&format!("{:>4} | {}\n", i + 1, line));
}
out
}
22 changes: 22 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@ fn test_basic_python() {
assert!(stdout.contains("3.9.4"));
}

#[test]
fn test_basic_pre_post_run() {
let ((_code, stdout, _stderr), td) = run_test_tempdir(
"examples/basic",
&["test_pre_post"]
);
assert!(stdout.contains("pre_run"));
assert!(stdout.contains("run"));
assert!(stdout.contains("post_run"));
//assert order
assert!(stdout.find("pre_run").unwrap() < stdout.find("run").unwrap());
assert!(stdout.find("run").unwrap() < stdout.find("post_run").unwrap());

let while_run_file = td.path().join("while_run.txt");
assert!(while_run_file.exists());
let raw = std::fs::read_to_string(while_run_file).unwrap();
// now make sure that that pid's not in existance
let pid = raw.trim().parse::<u32>().unwrap();
assert!(!PathBuf::from(format!("/proc/{}", pid)).exists());

}

#[test]
fn test_basic_jupyter() {
let (_code, stdout, _stderr) = run_test(
Expand Down

0 comments on commit 385ae48

Please sign in to comment.