Implement cancel_all

This commit is contained in:
Alex Mikhalev 2020-08-17 20:06:09 -06:00
parent 43f1e59516
commit ccfd130022

View File

@ -7,7 +7,7 @@ use std::{
atomic::{AtomicI32, Ordering}, atomic::{AtomicI32, Ordering},
Arc, Arc,
}, },
time::Duration, time::Duration, mem::swap,
}; };
use thiserror::Error; use thiserror::Error;
use tokio::{ use tokio::{
@ -38,6 +38,7 @@ enum RunnerMsg {
Quit, Quit,
QueueRun(RunHandle, SectionRef, Duration), QueueRun(RunHandle, SectionRef, Duration),
CancelRun(RunHandle), CancelRun(RunHandle),
CancelAll,
} }
#[derive(Debug)] #[derive(Debug)]
@ -170,6 +171,14 @@ async fn runner_task(
trace!(handle = handle.0, "cancelling run by handle"); trace!(handle = handle.0, "cancelling run by handle");
run.cancel(&*interface); run.cancel(&*interface);
} }
},
CancelAll => {
let mut old_runs = LinkedList::new();
swap(&mut old_runs, &mut run_queue);
trace!(count = old_runs.len(), "cancelling all runs");
for mut run in old_runs {
run.cancel(&*interface);
}
} }
} }
}; };
@ -235,7 +244,8 @@ impl SectionRunner {
} }
pub async fn cancel_all(&mut self) -> Result<()> { pub async fn cancel_all(&mut self) -> Result<()> {
todo!() self.msg_send.send(RunnerMsg::CancelAll).await?;
Ok(())
} }
pub async fn pause(&mut self) -> Result<()> { pub async fn pause(&mut self) -> Result<()> {