|
|
|
@ -105,11 +105,15 @@ impl Future for CommandWaiter
|
|
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>
|
|
|
|
|
{
|
|
|
|
|
let future = async {
|
|
|
|
|
self.0.take().unwrap().await.map_err(|_| Error::WorkerDropped)
|
|
|
|
|
};
|
|
|
|
|
tokio::pin!(future);
|
|
|
|
|
future.poll(cx)
|
|
|
|
|
if let Some(value) = self.0.take() {
|
|
|
|
|
let future = async move {
|
|
|
|
|
value.await.map_err(|_| Error::WorkerDropped)
|
|
|
|
|
};
|
|
|
|
|
tokio::pin!(future);
|
|
|
|
|
future.poll(cx)
|
|
|
|
|
} else {
|
|
|
|
|
Poll::Ready(Err(Error::WorkerDropped))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -128,15 +132,12 @@ impl Future for TaskWaiter
|
|
|
|
|
|
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>
|
|
|
|
|
{
|
|
|
|
|
let future = async {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
idx = self.0.take().unwrap() => {
|
|
|
|
|
return idx.map_err(|_| Error::WorkerDropped);
|
|
|
|
|
}
|
|
|
|
|
_ = &mut self.1 => {
|
|
|
|
|
return Err(Error::WorkerDropped);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let value = self.0.take().unwrap();
|
|
|
|
|
let one = &mut self.1;
|
|
|
|
|
let future = async {
|
|
|
|
|
let val = value.await.map_err(|_| Error::WorkerDropped)?;
|
|
|
|
|
one.await.map_err(|_| Error::WorkerDropped)?;
|
|
|
|
|
Ok(val)
|
|
|
|
|
};
|
|
|
|
|
tokio::pin!(future);
|
|
|
|
|
future.poll(cx)
|
|
|
|
@ -161,7 +162,7 @@ impl<'a> CommandBuilder<'a>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Add another Builder to this one
|
|
|
|
|
#[inline] pub fn chain<I: IntoIterator<Item = CommandKind>>(mut self, other: I) -> Self
|
|
|
|
|
#[inline] pub fn chain<I: IntoIterator<Item = CommandKind>>(&mut self, other: I) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.extend(other);
|
|
|
|
|
self
|
|
|
|
@ -169,42 +170,42 @@ impl<'a> CommandBuilder<'a>
|
|
|
|
|
|
|
|
|
|
// Commands
|
|
|
|
|
/// Print line on the worker's progress bar
|
|
|
|
|
pub fn println(mut self, line: impl Into<String>) -> Self
|
|
|
|
|
pub fn println(&mut self, line: impl Into<String>) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::PrintLine(line.into()));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Print error line on the worker's progress bar
|
|
|
|
|
pub fn eprintln(mut self, line: impl Into<String>) -> Self
|
|
|
|
|
pub fn eprintln(&mut self, line: impl Into<String>) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::PrintLineErr(line.into()));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Increase the worker's max number
|
|
|
|
|
pub fn bump_max(mut self, by: usize) -> Self
|
|
|
|
|
pub fn bump_max(&mut self, by: usize) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::BumpHigh(by));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Increase the worker's min number
|
|
|
|
|
pub fn bump_min(mut self, by: usize) -> Self
|
|
|
|
|
pub fn bump_min(&mut self, by: usize) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::BumpLow(by));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Remove a task by ID.
|
|
|
|
|
pub fn remove_task(mut self, task_idx: usize) -> Self
|
|
|
|
|
pub fn remove_task(&mut self, task_idx: usize) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::RemoveTask(task_idx));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Signal a shutdown to the worker
|
|
|
|
|
pub fn shutdown(mut self) -> Self
|
|
|
|
|
pub fn shutdown(&mut self) -> &mut Self
|
|
|
|
|
{
|
|
|
|
|
self.send_command(CommandKind::Complete);
|
|
|
|
|
self
|
|
|
|
@ -363,7 +364,7 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
|
|
|
|
|
I: IntoIterator<Item=String>>(high: usize, tasks: I) -> (ProgressSender, JoinHandle<Result<(), Error>>)
|
|
|
|
|
{
|
|
|
|
|
let mut list = task_list::TaskList::from_iter(tasks);
|
|
|
|
|
let mut progress = P::with_title(50, &list);
|
|
|
|
|
let mut progress = P::with_title(50, format!("(0/0) {}",list.as_str()));
|
|
|
|
|
|
|
|
|
|
let (shutdown_tx, shutdown_rx) = watch::channel(None);
|
|
|
|
|
let (tx, mut rx) = mpsc::channel::<Command>(16);
|
|
|
|
@ -387,11 +388,11 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
|
|
|
|
|
let (command, _defer) = {
|
|
|
|
|
let Command{comm, comp} = command;
|
|
|
|
|
let d = defer::Defer::new(|| {
|
|
|
|
|
let _ =comp.send(());
|
|
|
|
|
let _ = comp.send(());
|
|
|
|
|
});
|
|
|
|
|
(comm, d)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum MaybeSingle<T>
|
|
|
|
|
where T: IntoIterator<Item = CommandKind>
|
|
|
|
|
{
|
|
|
|
@ -432,39 +433,51 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
|
|
|
|
|
};
|
|
|
|
|
stage::Stage::from_iter(command)
|
|
|
|
|
};
|
|
|
|
|
let mut has_blanked = false;
|
|
|
|
|
while let Some(command) = commands.next().await {
|
|
|
|
|
match command {
|
|
|
|
|
CommandKind::BumpHigh(high) => {
|
|
|
|
|
let stat = stat.to_mut();
|
|
|
|
|
stat.high+=high;
|
|
|
|
|
progress.set_progress(stat.get_pct());
|
|
|
|
|
|
|
|
|
|
progress.set_title(format!("({}/{}) {}",stat.low, stat.high, list.as_ref()).as_str());
|
|
|
|
|
},
|
|
|
|
|
CommandKind::BumpLow(low) => {
|
|
|
|
|
let stat = stat.to_mut();
|
|
|
|
|
stat.low+=low;
|
|
|
|
|
progress.set_progress(stat.get_pct());
|
|
|
|
|
|
|
|
|
|
progress.set_title(format!("({}/{}) {}",stat.low, stat.high, list.as_ref()).as_str());
|
|
|
|
|
},
|
|
|
|
|
CommandKind::PrintLine(line) => {
|
|
|
|
|
progress.println(&line[..]);
|
|
|
|
|
progress.blank();
|
|
|
|
|
has_blanked = true;
|
|
|
|
|
println!("{}", &line[..]);
|
|
|
|
|
},
|
|
|
|
|
CommandKind::PrintLineErr(line) => {
|
|
|
|
|
progress.eprintln(&line[..]);
|
|
|
|
|
progress.blank();
|
|
|
|
|
has_blanked = true;
|
|
|
|
|
eprintln!("{}", &line[..]);
|
|
|
|
|
},
|
|
|
|
|
CommandKind::AddTask(task) => {
|
|
|
|
|
let idx = list.push(task.name);
|
|
|
|
|
if let Err(_) = task.idx.send(idx) {
|
|
|
|
|
list.pop(idx);
|
|
|
|
|
} else {
|
|
|
|
|
progress.set_title(list.as_ref());
|
|
|
|
|
progress.set_title(format!("({}/{}) {}",stat.low, stat.high, list.as_ref()).as_str());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
CommandKind::RemoveTask(task) => {
|
|
|
|
|
if list.pop(task) {
|
|
|
|
|
progress.set_title(list.as_ref());
|
|
|
|
|
progress.set_title(format!("({}/{}) {}", stat.low, stat.high, list.as_ref()).as_str());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
CommandKind::Complete => {
|
|
|
|
|
break;
|
|
|
|
|
if has_blanked {
|
|
|
|
|
progress.refresh();
|
|
|
|
|
}
|
|
|
|
|
rx.close();
|
|
|
|
|
},
|
|
|
|
|
CommandKind::Many(many) => {
|
|
|
|
|
let _ = commands.sender().send_many(many).await;
|
|
|
|
@ -472,6 +485,10 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if has_blanked {
|
|
|
|
|
progress.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Cow::Owned(stat) = std::mem::replace(&mut stat, Cow::Borrowed(&last_stat /* wtf? how is this legal? idk*/)) {
|
|
|
|
|
//It's been written to
|
|
|
|
|
let _ = stat_tx.broadcast(stat.clone());
|
|
|
|
@ -479,6 +496,7 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
progress.complete();
|
|
|
|
|
}).await.map_err(|_| Error::WorkerPanic);
|
|
|
|
|
shutdown_tx.broadcast(Some(res.clone())).expect("Failed to communicate worker shutdown with waiters");
|
|
|
|
|
res
|
|
|
|
|