Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serve): Opt-in parallelism for deno serve #24920

Merged
merged 21 commits into from
Aug 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use same arg as test
  • Loading branch information
nathanwhit committed Aug 12, 2024
commit c0a4e1722bd0f7b6570e37bcf33c403289328429
99 changes: 52 additions & 47 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,11 +2645,7 @@ fn serve_subcommand() -> Command {
.value_parser(serve_host_validator),
)
.arg(
Arg::new("parallel").long("parallel").num_args(0..=1)
.help("Run multiple server workers. Pass 0 to use the number of cores. Defaults to 0.")
.require_equals(true)
.default_missing_value("0")
.value_parser(value_parser!(usize))
parallel_arg("multiple server workers", false)
)
.arg(check_arg(false))
.arg(watch_arg(true))
Expand Down Expand Up @@ -2812,11 +2808,7 @@ Directory arguments are expanded to all contained files matching the glob
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("parallel")
.long("parallel")
.help("Run test modules in parallel. Parallelism defaults to the number of available CPUs or the value in the DENO_JOBS environment variable.")
.conflicts_with("jobs")
.action(ArgAction::SetTrue)
parallel_arg("test modules", true)
)
.arg(
Arg::new("jobs")
Expand Down Expand Up @@ -2859,6 +2851,18 @@ Directory arguments are expanded to all contained files matching the glob
)
}

fn parallel_arg(descr: &str, jobs_fallback: bool) -> Arg {
let arg = Arg::new("parallel")
.long("parallel")
.help(format!("Run {descr} in parallel. Parallelism defaults to the number of available CPUs or the value in the DENO_JOBS environment variable."))
.action(ArgAction::SetTrue);
if jobs_fallback {
arg.conflicts_with("jobs")
} else {
arg
}
}

fn types_subcommand() -> Command {
Command::new("types").about(
"Print runtime TypeScript declarations.
Expand Down Expand Up @@ -4357,13 +4361,7 @@ fn serve_parse(
.remove_one::<String>("host")
.unwrap_or_else(|| "0.0.0.0".to_owned());

let worker_count = matches.remove_one::<usize>("parallel").map(|n| {
if n == 0 {
std::thread::available_parallelism().unwrap().get()
} else {
n
}
});
let worker_count = parallel_arg_parse(matches, false).map(|v| v.get());

runtime_args_parse(flags, matches, true, true);
// If the user didn't pass --allow-net, add this port to the network
Expand Down Expand Up @@ -4436,6 +4434,42 @@ fn task_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.subcommand = DenoSubcommand::Task(task_flags);
}

fn parallel_arg_parse(
matches: &mut ArgMatches,
fallback_to_jobs: bool,
) -> Option<NonZeroUsize> {
if matches.get_flag("parallel") {
if let Ok(value) = env::var("DENO_JOBS") {
value.parse::<NonZeroUsize>().ok()
} else {
std::thread::available_parallelism().ok()
}
} else if fallback_to_jobs && matches.contains_id("jobs") {
// We can't change this to use the log crate because its not configured
// yet at this point since the flags haven't been parsed. This flag is
// deprecated though so it's not worth changing the code to use the log
// crate here and this is only done for testing anyway.
#[allow(clippy::print_stderr)]
{
eprintln!(
"⚠️ {}",
crate::colors::yellow(concat!(
"The `--jobs` flag is deprecated and will be removed in Deno 2.0.\n",
"Use the `--parallel` flag with possibly the `DENO_JOBS` environment variable instead.\n",
"Learn more at: https://docs.deno.com/runtime/manual/basics/env_variables"
)),
);
}
if let Some(value) = matches.remove_one::<NonZeroUsize>("jobs") {
Some(value)
} else {
std::thread::available_parallelism().ok()
}
} else {
None
}
}

fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.type_check_mode = TypeCheckMode::Local;
runtime_args_parse(flags, matches, true, true);
Expand Down Expand Up @@ -4502,36 +4536,7 @@ fn test_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.argv.extend(script_arg);
}

let concurrent_jobs = if matches.get_flag("parallel") {
if let Ok(value) = env::var("DENO_JOBS") {
value.parse::<NonZeroUsize>().ok()
} else {
std::thread::available_parallelism().ok()
}
} else if matches.contains_id("jobs") {
// We can't change this to use the log crate because its not configured
// yet at this point since the flags haven't been parsed. This flag is
// deprecated though so it's not worth changing the code to use the log
// crate here and this is only done for testing anyway.
#[allow(clippy::print_stderr)]
{
eprintln!(
"⚠️ {}",
crate::colors::yellow(concat!(
"The `--jobs` flag is deprecated and will be removed in Deno 2.0.\n",
"Use the `--parallel` flag with possibly the `DENO_JOBS` environment variable instead.\n",
"Learn more at: https://docs.deno.com/runtime/manual/basics/env_variables"
)),
);
}
if let Some(value) = matches.remove_one::<NonZeroUsize>("jobs") {
Some(value)
} else {
std::thread::available_parallelism().ok()
}
} else {
None
};
let concurrent_jobs = parallel_arg_parse(matches, true);

let include = if let Some(files) = matches.remove_many::<String>("files") {
files.collect()
Expand Down