Add collection of job & thread exit codes + default to ExitCode::Error if any ExitCode::Error

This commit is contained in:
fusillicode 2020-01-25 17:13:47 +01:00 committed by David Peter
parent fa7d49282b
commit 7213f5a88e
3 changed files with 25 additions and 9 deletions

View file

@ -21,7 +21,8 @@ pub fn job(
cmd: Arc<CommandTemplate>,
out_perm: Arc<Mutex<()>>,
show_filesystem_errors: bool,
) {
) -> ExitCode {
let mut results: Vec<ExitCode> = Vec::new();
loop {
// Create a lock on the shared receiver for this thread.
let lock = rx.lock().unwrap();
@ -39,11 +40,13 @@ pub fn job(
Err(_) => break,
};
// Drop the lock so that other threads can read from the the receiver.
// Drop the lock so that other threads can read from the receiver.
drop(lock);
// Generate a command and execute it.
cmd.generate_and_execute(&value, Arc::clone(&out_perm));
// Generate a command, executes it and store its exit code.
results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))
}
// Returns error in case of any error.
ExitCode::error_if_any_error(results)
}
pub fn batch(

View file

@ -7,9 +7,21 @@ pub enum ExitCode {
impl Into<i32> for ExitCode {
fn into(self) -> i32 {
match self {
ExitCode::Success => 0,
ExitCode::GeneralError => 1,
ExitCode::KilledBySigint => 130,
Self::Success => 0,
Self::GeneralError => 1,
Self::KilledBySigint => 130,
}
}
}
impl ExitCode {
pub fn error_if_any_error(results: Vec<Self>) -> Self {
if results.iter().any(|s| match s {
Self::GeneralError => true,
_ => false,
}) {
return Self::GeneralError;
}
Self::Success
}
}

View file

@ -177,11 +177,12 @@ fn spawn_receiver(
}
// Wait for all threads to exit before exiting the program.
let mut results: Vec<ExitCode> = Vec::new();
for h in handles {
h.join().unwrap();
results.push(h.join().unwrap());
}
ExitCode::Success
ExitCode::error_if_any_error(results)
}
} else {
let start = time::Instant::now();