use tracing::Instrument;
use crate::context::context;
pub async fn spawn_bg<F>(future: F) -> tokio::task::JoinHandle<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
let permit = context()
.bg_semaphore
.acquire()
.await
.expect("mytheclipse: bg semaphore closed unexpectedly");
let span = tracing::info_span!("mytheclipse_bg_task");
tokio::spawn(
async move {
let _permit = permit;
future.await
}
.instrument(span),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn spawn_bg_roundtrips_a_value() {
let handle = spawn_bg(async { 9u32 }).await;
assert_eq!(handle.await.unwrap(), 9);
}
}