pub mod error;
pub mod grpc;
pub mod http;
use futures::future::join_all;
use rsketch_error::Result;
use tokio::{sync::oneshot::Receiver, task::JoinHandle};
use tokio_util::sync::CancellationToken;
pub struct ServiceHandler {
join_handle: JoinHandle<()>,
cancellation_token: CancellationToken,
started_rx: Option<Receiver<()>>,
reporter_handles: Vec<JoinHandle<()>>,
}
impl ServiceHandler {
pub async fn wait_for_start(&mut self) -> Result<()> {
self.started_rx
.take()
.expect("Server start signal already consumed")
.await
.expect("Failed to receive server start signal");
Ok(())
}
pub async fn wait_for_stop(self) -> Result<()> {
let handles = self
.reporter_handles
.into_iter()
.chain(std::iter::once(self.join_handle));
join_all(handles).await;
Ok(())
}
pub fn shutdown(&self) { self.cancellation_token.cancel(); }
#[must_use]
pub fn is_finished(&self) -> bool { self.join_handle.is_finished() }
}