use std::future::Future;
use std::time::Duration;
use rustvello_core::error::RustvelloResult;
use rustvello_core::runner::Runner;
use super::PersistentTokioRunner;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ShutdownOutcome {
Drained,
DeadlineElapsed,
}
impl PersistentTokioRunner {
pub async fn with_bounded_shutdown<F>(
self,
signal: F,
budget: Duration,
) -> RustvelloResult<ShutdownOutcome>
where
F: Future<Output = ()> + Send,
{
tokio::pin!(signal);
let run = self.run();
tokio::pin!(run);
tokio::select! {
biased;
_ = &mut signal => self.shutdown().await?,
result = &mut run => return result.map(|()| ShutdownOutcome::Drained),
}
match tokio::time::timeout(budget, &mut run).await {
Ok(result) => result.map(|()| ShutdownOutcome::Drained),
Err(_) => Ok(ShutdownOutcome::DeadlineElapsed),
}
}
}