Skip to content
Myr

job.rs

This file

Path

src/exec/job.rs

67 lines · 2.1 KiB · Rust

Last changed by

Merge pull request #1422 from tavianator/batchTavian Barnesbefore adoption84f032e

History of this file

Take it

1use std::sync::Mutex;23use crate::config::Config;4use crate::error::print_error;5use crate::exit_codes::{merge_exitcodes, ExitCode};6use crate::walk::WorkerResult;78use super::CommandSet;910/// An event loop that listens for inputs from the `rx` receiver. Each received input will11/// generate a command with the supplied command template. The generated command will then12/// be executed, and this process will continue until the receiver's sender has closed.13pub fn job(14    results: impl IntoIterator<Item = WorkerResult>,15    cmd: &CommandSet,16    out_perm: &Mutex<()>,17    config: &Config,18) -> ExitCode {19    // Output should be buffered when only running a single thread20    let buffer_output: bool = config.threads > 1;2122    let mut ret = ExitCode::Success;23    for result in results {24        // Obtain the next result from the receiver, else if the channel25        // has closed, exit from the loop26        let dir_entry = match result {27            WorkerResult::Entry(dir_entry) => dir_entry,28            WorkerResult::Error(err) => {29                if config.show_filesystem_errors {30                    print_error(err.to_string());31                }32                continue;33            }34        };3536        // Generate a command, execute it and store its exit code.37        let code = cmd.execute(38            dir_entry.stripped_path(config),39            config.path_separator.as_deref(),40            out_perm,41            buffer_output,42        );43        ret = merge_exitcodes([ret, code]);44    }45    // Returns error in case of any error.46    ret47}4849pub fn batch(50    results: impl IntoIterator<Item = WorkerResult>,51    cmd: &CommandSet,52    config: &Config,53) -> ExitCode {54    let paths = results55        .into_iter()56        .filter_map(|worker_result| match worker_result {57            WorkerResult::Entry(dir_entry) => Some(dir_entry.into_stripped_path(config)),58            WorkerResult::Error(err) => {59                if config.show_filesystem_errors {60                    print_error(err.to_string());61                }62                None63            }64        });6566    cmd.execute_batch(paths, config.batch_size, config.path_separator.as_deref())67}