Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 3c87952 point at it.

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 1–4why, one step back
1use std::path::PathBuf;
2use std::sync::mpsc::Receiver;3use std::sync::{Arc, Mutex};4

Proper error handling within walk.rssharkdpbefore adoption4590ae8

lines 5–10why, one step back
5use crate::error::print_error;
6use crate::exit_codes::{merge_exitcodes, ExitCode};7use crate::walk::WorkerResult;89use super::CommandTemplate;10

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 11–18why, one step back
11/// An event loop that listens for inputs from the `rx` receiver. Each received input will
12/// 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 back
19) -> ExitCode {
20 let mut results: Vec<ExitCode> = Vec::new();

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 21–30why, one step back
21 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 back
31 print_error(err.to_string());

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 32–37why, one step back
32 }
33 continue;34 }35 Err(_) => break,36 };37

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

line 38why, one step back
38 // Drop the lock so that other threads can read from the receiver.

Bump minimum required Rust versionsharkdpbefore adoption3c87952

line 39why, one step back
39 drop(lock);

Fix typo in a job.rs commentfusillicodebefore adoptione469982

line 40why, one step back
40 // 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 back
41 results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))

Bump minimum required Rust versionsharkdpbefore adoption3c87952

line 42why, one step back
42 }

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

line 43why, one step back
43 // Returns error in case of any error.

Clean up exit code handlingsharkdpbefore adoptione42ddda

line 44why, one step back
44 merge_exitcodes(&results)

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 45–46why, one step back
45}
46

Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd

lines 47–51why, one step back
47pub fn batch(
48 rx: Receiver<WorkerResult>,49 cmd: &CommandTemplate,50 show_filesystem_errors: bool,51) -> ExitCode {

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 52–55why, one step back
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 back
56 print_error(err.to_string());

Bump minimum required Rust versionsharkdpbefore adoption3c87952

lines 57–60why, one step back
57 }
58 None59 }60 });

Expose exit status from `--exec-batch <cmd>`sharkdpbefore adoptiona0505bd

line 61why, one step back
61 cmd.generate_and_execute_batch(paths)

Bump minimum required Rust versionsharkdpbefore adoption3c87952

line 62why, one step back
62}