From 1cf7cec8bd95512c79b6d256ab22460c79c0578c Mon Sep 17 00:00:00 2001 From: Thomas Heck Date: Thu, 22 Apr 2021 21:05:34 +0200 Subject: [PATCH] feat: add "jobs" cli argument --- CHANGELOG.md | 1 + src/config.rs | 33 ++++++++++++++++++++++++++++++--- src/main.rs | 4 +++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3869c6b..8844781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog * "copy" encoding format added +* "jobs" cli argument added, that lets you set the number of concurrent transcodes ## v1.1.0 diff --git a/src/config.rs b/src/config.rs index b6412da..21aef7e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ pub struct Config { pub from: PathBuf, pub to: PathBuf, pub matches: Vec, + pub jobs: Option, } #[derive(Debug)] @@ -127,7 +128,7 @@ pub fn config() -> Result { .long("config") .required(false) .takes_value(true) - .help("path to an audio-conv config file, defaults to \"audio-conv.yaml\""), + .help("Path to an audio-conv config file, defaults to \"audio-conv.yaml\""), ) .arg( Arg::with_name("from") @@ -135,7 +136,7 @@ pub fn config() -> Result { .long("from") .required(false) .takes_value(true) - .help("from directory path"), + .help("\"from\" directory path"), ) .arg( Arg::with_name("to") @@ -143,7 +144,15 @@ pub fn config() -> Result { .long("to") .required(false) .takes_value(true) - .help("to directory path"), + .help("\"to\" directory path"), + ) + .arg( + Arg::with_name("jobs") + .short("j") + .long("jobs") + .required(false) + .takes_value(true) + .help("Allow N jobs/transcodes at once. Defaults to number of logical cores"), ) .subcommand(SubCommand::with_name("init").about("writes an example config")) .get_matches(); @@ -278,6 +287,24 @@ pub fn config() -> Result { .canonicalize() .context("Could not canonicalize \"to\" path")?, matches: transcode_matches, + jobs: arg_matches + .value_of_os("jobs") + .map(|jobs_os_str| { + let jobs_str = jobs_os_str.to_str().with_context(|| { + // TODO: use `OsStr.display` when it lands + // https://github.com/rust-lang/rust/pull/80841 + format!( + "Could not convert \"jobs\" argument to string due to invalid characters", + ) + })?; + jobs_str.parse().with_context(|| { + format!( + "Could not parse \"jobs\" argument \"{}\" to a number", + &jobs_str + ) + }) + }) + .transpose()?, }) } diff --git a/src/main.rs b/src/main.rs index 5440796..485d5ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -192,9 +192,11 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> { log_path: log_path.clone(), }); + let concurrent_jobs = config.jobs.unwrap_or_else(|| num_cpus::get()); + stream::iter(conv_args.into_iter().enumerate()) .map(Ok) - .try_for_each_concurrent(num_cpus::get(), |(i, args)| { + .try_for_each_concurrent(concurrent_jobs, |(i, args)| { let config = &config; let ui_queue = &ui_queue; let log_path = &log_path;