poll rustfmt child processes
This commit is contained in:
parent
5620afc4e0
commit
ab7d769651
1 changed files with 19 additions and 4 deletions
|
@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
use std::sync::mpsc::SyncSender;
|
use std::sync::mpsc::SyncSender;
|
||||||
|
|
||||||
fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl FnMut() {
|
fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl FnMut(bool) -> bool {
|
||||||
let mut cmd = Command::new(&rustfmt);
|
let mut cmd = Command::new(&rustfmt);
|
||||||
// avoid the submodule config paths from coming into play,
|
// avoid the submodule config paths from coming into play,
|
||||||
// we only allow a single global config for the workspace for now
|
// we only allow a single global config for the workspace for now
|
||||||
|
@ -23,7 +23,13 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
|
||||||
let cmd_debug = format!("{:?}", cmd);
|
let cmd_debug = format!("{:?}", cmd);
|
||||||
let mut cmd = cmd.spawn().expect("running rustfmt");
|
let mut cmd = cmd.spawn().expect("running rustfmt");
|
||||||
// poor man's async: return a closure that'll wait for rustfmt's completion
|
// poor man's async: return a closure that'll wait for rustfmt's completion
|
||||||
move || {
|
move |block: bool| -> bool {
|
||||||
|
if !block {
|
||||||
|
match cmd.try_wait() {
|
||||||
|
Ok(Some(_)) => {}
|
||||||
|
_ => return false,
|
||||||
|
}
|
||||||
|
}
|
||||||
let status = cmd.wait().unwrap();
|
let status = cmd.wait().unwrap();
|
||||||
if !status.success() {
|
if !status.success() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
@ -34,6 +40,7 @@ fn rustfmt(src: &Path, rustfmt: &Path, paths: &[PathBuf], check: bool) -> impl F
|
||||||
);
|
);
|
||||||
crate::detail_exit(1);
|
crate::detail_exit(1);
|
||||||
}
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,15 +153,23 @@ pub fn format(build: &Builder<'_>, check: bool, paths: &[PathBuf]) {
|
||||||
let child = rustfmt(&src, &rustfmt_path, paths.as_slice(), check);
|
let child = rustfmt(&src, &rustfmt_path, paths.as_slice(), check);
|
||||||
children.push_back(child);
|
children.push_back(child);
|
||||||
|
|
||||||
|
// poll completion before waiting
|
||||||
|
for i in (0..children.len()).rev() {
|
||||||
|
if children[i](false) {
|
||||||
|
children.swap_remove_back(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if children.len() >= max_processes {
|
if children.len() >= max_processes {
|
||||||
// await oldest child
|
// await oldest child
|
||||||
children.pop_front().unwrap()();
|
children.pop_front().unwrap()(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// await remaining children
|
// await remaining children
|
||||||
for mut child in children {
|
for mut child in children {
|
||||||
child();
|
child(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue