use crate::graceful::ShutdownGuard;
use tracing::instrument::Instrument;
#[derive(Default, Debug, Clone)]
pub struct Executor {
guard: Option<ShutdownGuard>,
}
impl Executor {
pub const fn new() -> Self {
Self { guard: None }
}
pub fn graceful(guard: ShutdownGuard) -> Self {
Self { guard: Some(guard) }
}
pub fn spawn_task<F>(&self, future: F) -> tokio::task::JoinHandle<F::Output>
where
F: Future<Output: Send + 'static> + Send + 'static,
{
match &self.guard {
Some(guard) => guard.spawn_task(future.in_current_span()),
None => tokio::spawn(future.in_current_span()),
}
}
pub fn guard(&self) -> Option<&ShutdownGuard> {
self.guard.as_ref()
}
}