lavina/src/util/mod.rs

35 lines
876 B
Rust
Raw Normal View History

2023-02-07 15:21:00 +00:00
use crate::prelude::*;
pub mod http;
2023-02-04 01:01:49 +00:00
pub mod table;
pub mod telemetry;
2023-02-12 12:15:31 +00:00
#[cfg(test)]
pub mod testkit;
2023-02-07 15:21:00 +00:00
pub struct Terminator {
2023-02-14 22:49:56 +00:00
signal: Promise<()>,
2023-02-07 15:21:00 +00:00
completion: JoinHandle<Result<()>>,
}
impl Terminator {
pub async fn terminate(self) -> Result<()> {
match self.signal.send(()) {
Ok(()) => {}
Err(_) => log::warn!("Termination channel is dropped"),
2023-02-07 15:21:00 +00:00
}
self.completion.await??;
Ok(())
}
2023-02-22 15:05:28 +00:00
/// Used to spawn managed tasks with support for graceful shutdown
pub fn spawn<Fun, Fut>(launcher: Fun) -> Terminator
where
Fun: FnOnce(Deferred<()>) -> Fut,
Fut: Future<Output = Result<()>> + Send + 'static,
{
let (signal, rx) = oneshot();
let future = launcher(rx);
let completion = tokio::task::spawn(future);
Terminator { signal, completion }
}
2023-02-07 15:21:00 +00:00
}