Skip to content
This repository has been archived by the owner on Jul 11, 2019. It is now read-only.

Commit

Permalink
Count Errors & Exit With Error Count
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Jun 9, 2017
1 parent 5300d5c commit 144eecc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parallel"
version = "0.11.2"
version = "0.11.3"
authors = ["Michael Aaron Murphy <mmstickman@gmail.com>"]
license = "MIT"
description = "Command-line CPU load balancer for executing jobs in parallel"
Expand Down
19 changes: 15 additions & 4 deletions src/execute/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ macro_rules! append_to_processed {

#[allow(cyclomatic_complexity)]
/// Tail and print the standard output and error of each process in the correct order
pub fn receive_messages(input_rx: Receiver<State>, args: Args, base: &str, processed_path: &Path,
errors_path: &Path)
{
pub fn receive_messages (
input_rx: Receiver<State>,
args: Args,
base: &str,
processed_path: &Path,
errors_path: &Path
) -> i32 {
let stdout = io::stdout();
let stderr = io::stderr();

// Store the flags value outside of the `args` structure
let flags = args.flags;
// Keeps track of which job is currently allowed to print to standard output/error.
let mut counter = 0;
// Keep a record of how many errors have occurred.
let mut error_count = 0;
// In the event that the joblog parameter was passed, a counter will be needed for jobs.
let mut job_counter = args.ninputs;
// The following `buffer` is used to store completed jobs that are awaiting processing.
Expand Down Expand Up @@ -139,13 +145,17 @@ pub fn receive_messages(input_rx: Receiver<State>, args: Args, base: &str, proce
// If an error occured and the id matches the counter, print the error immediately.
State::Error(id, ref message) if id == counter => {
counter += 1;
if error_count != 254 { error_count += 1; }
if let Err(why) = error_file.write(message.as_bytes()) {
let mut stderr = stderr.lock();
let _ = write!(stderr, "parallel: I/O error: {}", why);
}
},
// Otherwise add that error to the job complete buffer as well.
State::Error(id, message) => buffer.push(State::Error(id, message)),
State::Error(id, message) => {
buffer.push(State::Error(id, message));
if error_count != 254 { error_count += 1; }
},
// If the joblog parameter was set, a joblog signal can be received.
// If the job ID matches the current job counter, write the log to the job log.
State::JobLog(ref data) if data.job_id == job_counter => {
Expand Down Expand Up @@ -279,6 +289,7 @@ pub fn receive_messages(input_rx: Receiver<State>, args: Args, base: &str, proce
let mut stderr = stderr.lock();
let _ = write!(stderr, "parallel: I/O error: {}", why);
}
error_count
}

/// Drops states that have been processed and are no longer required
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn main() {
}

/// Prints messages from executed commands in the correct order.
execute::receive_messages(input_rx, args, &base_path, &processed_path, &errors_path);
let errors = execute::receive_messages(input_rx, args, &base_path, &processed_path, &errors_path);

/// Wait for all threads to exit before proceeding.
for thread in threads { thread.join().unwrap(); }
Expand All @@ -256,7 +256,7 @@ fn main() {
let _ = stderr.write(b"\n");
}
}
exit(1);
exit(errors);

This comment has been minimized.

Copy link
@s-d-m

s-d-m Feb 14, 2018

This is incorrect.
in shell, the exit value is a number between 0 and 255 (inclusive). This is a modulo 256 computation. Hence if the number of errors happens to be a multiple of 256, it would return 0 and be seen as no error occurred

You can test it simply by running the following script:

#!/bin/bash
exit "$1"

and then run the script with different values like 0, -1, 255, 256, 257, 512 ...

edit: nevermind: I just saw now that there the counter wouldn't be incremented above 254, hence this bug is not present. Sorry

}
}
}
Expand Down

0 comments on commit 144eecc

Please sign in to comment.