use std::future::Future;
use tokio::task::JoinHandle;
use tracing::error;
#[cfg(test)]
#[path = "tasks_test.rs"]
mod tasks_test;
pub fn spawn_with_exit_on_panic<F, T>(future: F) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
inner_spawn_with_exit_on_panic(future, exit_process)
}
pub(crate) fn inner_spawn_with_exit_on_panic<F, E, T>(future: F, on_exit_f: E) -> JoinHandle<T>
where
F: Future<Output = T> + Send + 'static,
E: FnOnce() + Send + 'static,
T: Send + 'static,
{
let monitored_task = tokio::spawn(future);
tokio::spawn(async move {
match monitored_task.await {
Ok(res) => res,
Err(err) => {
error!("Monitored task failed: {:?}", err);
on_exit_f();
unreachable!()
}
}
})
}
pub(crate) fn exit_process() {
std::process::exit(1);
}