2023-06-19 18:56:14 +00:00
|
|
|
use std::{error, fs, io, path};
|
|
|
|
|
|
|
|
use tokio_util::sync::CancellationToken;
|
2023-06-14 18:22:10 +00:00
|
|
|
|
|
|
|
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
|
|
|
|
match fs::File::open(path) {
|
|
|
|
Ok(file) => Ok(Some(file)),
|
|
|
|
Err(err) => match err.kind() {
|
|
|
|
io::ErrorKind::NotFound => Ok(None),
|
|
|
|
_ => Err(err),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 18:56:14 +00:00
|
|
|
|
2023-06-21 12:29:47 +00:00
|
|
|
struct Task<E>
|
2023-06-19 18:56:14 +00:00
|
|
|
where
|
|
|
|
E: error::Error + Send + 'static,
|
|
|
|
{
|
|
|
|
canceller: CancellationToken,
|
2023-06-21 12:29:47 +00:00
|
|
|
handle: tokio::task::JoinHandle<Result<(), E>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TaskStack<E>
|
|
|
|
where
|
|
|
|
E: error::Error + Send + 'static,
|
|
|
|
{
|
|
|
|
wait_group: Vec<Task<E>>,
|
2023-06-19 18:56:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:15:42 +00:00
|
|
|
impl<E> TaskStack<E>
|
2023-06-19 18:56:14 +00:00
|
|
|
where
|
|
|
|
E: error::Error + Send + 'static,
|
|
|
|
{
|
2023-06-21 11:15:42 +00:00
|
|
|
pub fn new() -> TaskStack<E> {
|
|
|
|
TaskStack {
|
|
|
|
wait_group: Vec::new(),
|
2023-06-19 18:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:29:47 +00:00
|
|
|
pub fn push_spawn<F, Fut>(&mut self, mut f: F)
|
2023-06-19 18:56:14 +00:00
|
|
|
where
|
|
|
|
Fut: futures::Future<Output = Result<(), E>> + Send + 'static,
|
|
|
|
F: FnMut(CancellationToken) -> Fut,
|
|
|
|
{
|
2023-06-21 12:29:47 +00:00
|
|
|
let canceller = CancellationToken::new();
|
|
|
|
let handle = tokio::spawn(f(canceller.clone()));
|
|
|
|
self.wait_group.push(Task { canceller, handle });
|
2023-06-19 18:56:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:15:42 +00:00
|
|
|
pub async fn stop(mut self) -> Result<(), E> {
|
|
|
|
// reverse wait_group in place, so we stop the most recently added first. Since this method
|
|
|
|
// consumes self this is fine.
|
|
|
|
self.wait_group.reverse();
|
|
|
|
|
2023-06-21 12:29:47 +00:00
|
|
|
for t in self.wait_group {
|
|
|
|
t.canceller.cancel();
|
|
|
|
if let Err(err) = t.handle.await.expect("task failed") {
|
2023-06-21 11:15:42 +00:00
|
|
|
return Err(err);
|
2023-06-19 18:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-21 11:15:42 +00:00
|
|
|
Ok(())
|
2023-06-19 18:56:14 +00:00
|
|
|
}
|
|
|
|
}
|