job.rs
This file
Path
src/exec/job.rs
62 lines · 2.0 KiB · Rust
Last changed by
Proper error handling within walk.rssharkdpbefore adoption4590ae8
History of this fileTake it
The walk stopped after 200 commits: lines older than 1d16cd8 point at it.
Fix warningssharkdpbefore adoption1d16cd8
lines 1–4why, one step back1use std::path::PathBuf;2use std::sync::mpsc::Receiver;3use std::sync::{Arc, Mutex};4Proper error handling within walk.rssharkdpbefore adoption4590ae8
lines 5–10why, one step back5use crate::error::print_error;6use crate::exit_codes::{merge_exitcodes, ExitCode};7use crate::walk::WorkerResult;89use super::CommandTemplate;10Fix warningssharkdpbefore adoption1d16cd8
lines 11–18why, one step back11/// An event loop that listens for inputs from the `rx` receiver. Each received input will12/// generate a command with the supplied command template. The generated command will then13/// be executed, and this process will continue until the receiver's sender has closed.14pub fn job(15 rx: Arc<Mutex<Receiver<WorkerResult>>>,16 cmd: Arc<CommandTemplate>,17 out_perm: Arc<Mutex<()>>,18 show_filesystem_errors: bool,Add collection of job & thread exit codes + default to ExitCode::Error if any ExitCode::Errorfusillicodebefore adoption7213f5a
lines 19–20why, one step back19) -> ExitCode {20 let mut results: Vec<ExitCode> = Vec::new();Fix warningssharkdpbefore adoption1d16cd8
lines 21–30why, one step back21 loop {22 // Create a lock on the shared receiver for this thread.23 let lock = rx.lock().unwrap();2425 // Obtain the next result from the receiver, else if the channel26 // has closed, exit from the loop27 let value: PathBuf = match lock.recv() {28 Ok(WorkerResult::Entry(val)) => val,29 Ok(WorkerResult::Error(err)) => {30 if show_filesystem_errors {Proper error handling within walk.rssharkdpbefore adoption4590ae8
line 31why, one step back31 print_error(err.to_string());Fix warningssharkdpbefore adoption1d16cd8
lines 32–37why, one step back32 }33 continue;34 }35 Err(_) => break,36 };37Add collection of job & thread exit codes + default to ExitCode::Error if any ExitCode::Errorfusillicodebefore adoption7213f5a
line 38why, one step back38 // Drop the lock so that other threads can read from the receiver.Fix warningssharkdpbefore adoption1d16cd8
line 39why, one step back39 drop(lock);Fix typo in a job.rs commentfusillicodebefore adoptione469982
line 40why, one step back40 // Generate a command, execute it and store its exit code.Add collection of job & thread exit codes + default to ExitCode::Error if any ExitCode::Errorfusillicodebefore adoption7213f5a
line 41why, one step back41 results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))Fix warningssharkdpbefore adoption1d16cd8
line 42why, one step back42 }Add collection of job & thread exit codes + default to ExitCode::Error if any ExitCode::Errorfusillicodebefore adoption7213f5a
line 43why, one step back43 // Returns error in case of any error.Clean up exit code handlingsharkdpbefore adoptione42ddda
line 44why, one step back44 merge_exitcodes(&results)Fix warningssharkdpbefore adoption1d16cd8
lines 45–55why, one step back45}4647pub fn batch(48 rx: Receiver<WorkerResult>,49 cmd: &CommandTemplate,50 show_filesystem_errors: bool,51) -> ExitCode {52 let paths = rx.iter().filter_map(|value| match value {53 WorkerResult::Entry(val) => Some(val),54 WorkerResult::Error(err) => {55 if show_filesystem_errors {Proper error handling within walk.rssharkdpbefore adoption4590ae8
line 56why, one step back56 print_error(err.to_string());Fix warningssharkdpbefore adoption1d16cd8
lines 57–62why, one step back57 }58 None59 }60 });61 cmd.generate_and_execute_batch(paths)62}