use std::future::Future;
use wind_tunnel_core::prelude::{ShutdownHandle, ShutdownSignalError};
#[derive(Debug)]
pub struct Executor {
runtime: tokio::runtime::Runtime,
shutdown_handle: ShutdownHandle,
}
impl Executor {
pub(crate) fn new(runtime: tokio::runtime::Runtime, shutdown_handle: ShutdownHandle) -> Self {
Self {
runtime,
shutdown_handle,
}
}
pub fn execute_in_place<T>(
&self,
fut: impl Future<Output = anyhow::Result<T>>,
) -> anyhow::Result<T> {
let mut shutdown_listener = self.shutdown_handle.new_listener();
self.runtime.block_on(async move {
tokio::select! {
result = fut => result,
_ = shutdown_listener.wait_for_shutdown() => {
Err(anyhow::anyhow!(ShutdownSignalError::default()))
},
}
})
}
pub fn spawn(&self, fut: impl Future<Output = ()> + Send + 'static) {
self.runtime.spawn(fut);
}
}