feat: add "jobs" cli argument

This commit is contained in:
2021-04-22 21:05:34 +02:00
parent 5cf98b3c17
commit 1cf7cec8bd
3 changed files with 34 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
# Changelog # Changelog
* "copy" encoding format added * "copy" encoding format added
* "jobs" cli argument added, that lets you set the number of concurrent transcodes
## v1.1.0 ## v1.1.0

View File

@@ -12,6 +12,7 @@ pub struct Config {
pub from: PathBuf, pub from: PathBuf,
pub to: PathBuf, pub to: PathBuf,
pub matches: Vec<TranscodeMatch>, pub matches: Vec<TranscodeMatch>,
pub jobs: Option<usize>,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -127,7 +128,7 @@ pub fn config() -> Result<Config> {
.long("config") .long("config")
.required(false) .required(false)
.takes_value(true) .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(
Arg::with_name("from") Arg::with_name("from")
@@ -135,7 +136,7 @@ pub fn config() -> Result<Config> {
.long("from") .long("from")
.required(false) .required(false)
.takes_value(true) .takes_value(true)
.help("from directory path"), .help("\"from\" directory path"),
) )
.arg( .arg(
Arg::with_name("to") Arg::with_name("to")
@@ -143,7 +144,15 @@ pub fn config() -> Result<Config> {
.long("to") .long("to")
.required(false) .required(false)
.takes_value(true) .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")) .subcommand(SubCommand::with_name("init").about("writes an example config"))
.get_matches(); .get_matches();
@@ -278,6 +287,24 @@ pub fn config() -> Result<Config> {
.canonicalize() .canonicalize()
.context("Could not canonicalize \"to\" path")?, .context("Could not canonicalize \"to\" path")?,
matches: transcode_matches, 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()?,
}) })
} }

View File

@@ -192,9 +192,11 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
log_path: log_path.clone(), log_path: log_path.clone(),
}); });
let concurrent_jobs = config.jobs.unwrap_or_else(|| num_cpus::get());
stream::iter(conv_args.into_iter().enumerate()) stream::iter(conv_args.into_iter().enumerate())
.map(Ok) .map(Ok)
.try_for_each_concurrent(num_cpus::get(), |(i, args)| { .try_for_each_concurrent(concurrent_jobs, |(i, args)| {
let config = &config; let config = &config;
let ui_queue = &ui_queue; let ui_queue = &ui_queue;
let log_path = &log_path; let log_path = &log_path;