use std::future::Future;
use std::time::Duration;
use kerniq_core::ServerError;
pub fn in_runtime() -> bool {
tokio::runtime::Handle::try_current().is_ok()
}
pub fn block_on<F: Future>(
workers: Option<usize>,
shutdown_timeout: Duration,
future: F,
) -> Result<F::Output, ServerError> {
if in_runtime() {
return Err(ServerError::NestedRuntime);
}
let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.enable_all().thread_name("kerniq-worker");
if let Some(workers) = workers {
builder.worker_threads(workers);
}
let runtime = builder.build().map_err(ServerError::Runtime)?;
let output = runtime.block_on(future);
runtime.shutdown_timeout(shutdown_timeout);
Ok(output)
}