Skip to content

Commit

Permalink
Move cleanup out of threads
Browse files Browse the repository at this point in the history
This ensures it gets done even when a thread panics.
  • Loading branch information
martindisch committed Nov 21, 2019
1 parent 613cc26 commit cef82d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
error::Error,
fs,
path::{Path, PathBuf},
process::Command,
thread,
time::Duration,
};
Expand Down Expand Up @@ -44,10 +45,23 @@ pub fn run(
fs::create_dir(&tmp_dir)?;

// Start the operation
let result = run_local(input.as_ref(), output.as_ref(), &tmp_dir, hosts);
let result = run_local(input.as_ref(), output.as_ref(), &tmp_dir, &hosts);

// Clean up infallibly
info!("Cleaning up");
// TODO: make sure this also happens when stopped with Ctrl + C
// Remove remote temporary directories
for &host in &hosts {
// Clean up temporary directory on host
let output = Command::new("ssh")
.args(&[&host, "rm", "-r", remote::TMP_DIR])
.output()
.expect("Failed executing ssh command");
if !output.status.success() {
error!("Failed removing remote temporary directory on {}", host);
}
}
// Remove local temporary directory
fs::remove_dir_all(&tmp_dir).ok();

result
Expand All @@ -61,7 +75,7 @@ fn run_local(
input: &Path,
output: &Path,
tmp_dir: &Path,
hosts: Vec<&str>,
hosts: &[&str],
) -> Result<()> {
// Build path to audio file
let mut audio = tmp_dir.to_path_buf();
Expand Down Expand Up @@ -101,7 +115,7 @@ fn run_local(
// Spawn threads for hosts
info!("Starting remote encoding");
let mut host_threads = Vec::with_capacity(hosts.len());
for &host in &hosts {
for &host in hosts {
// Create owned hostname to move into the thread
let host = host.to_string();
// Clone the queue receiver for the thread
Expand All @@ -119,6 +133,7 @@ fn run_local(
for handle in host_threads {
if let Err(e) = handle.join() {
error!("Thread for a host panicked: {:?}", e);
return Err("A host thread panicked".into());
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::{debug, info};
use std::{path::PathBuf, process::Command, thread};

/// The name of the temporary directory in the home directory of remote hosts.
static TMP_DIR: &str = "shepherd_tmp_remote";
pub static TMP_DIR: &str = "shepherd_tmp_remote";

/// The parent thread managing the operations for a host.
pub fn host_thread(
Expand Down Expand Up @@ -68,15 +68,6 @@ pub fn host_thread(
info!("{} returned encoded chunk {}", host, chunk);
}

// Clean up temporary directory on host
let output = Command::new("ssh")
.args(&[&host, "rm", "-r", TMP_DIR])
.output()
.expect("Failed executing ssh command");
assert!(
output.status.success(),
"Failed removing remote temporary directory"
);
debug!("Host thread {} exiting", host);
}

Expand Down

0 comments on commit cef82d2

Please sign in to comment.