use crate::prelude::*;
use crate::task;
use crate::thread;
use signal_hook::consts::TERM_SIGNALS;
use signal_hook::iterator::Signals;
use std::process::exit;
pub fn run<T, E>(future: impl Future<Output = Result<T, E>> + Send + 'static) -> !
where
T: Send + 'static,
E: Display + Send + 'static,
{
let task = task::start(future);
match thread::block_on(task.join()) {
Ok(Err(err)) => {
error!("The main task failed. {}", err);
thread::sleep(Duration::hz(60));
exit(1)
}
Err(err) => {
if let Some(value) = err.value_str() {
error!("The main task panicked with `{}`.", value);
} else {
error!("The main task panicked.")
}
thread::sleep(Duration::hz(60));
exit(-1)
}
_ => exit(0),
}
}
pub fn run_with<T, E, F>(func: impl FnOnce(task::CancelSignal) -> F + Send + 'static) -> !
where
T: Send + 'static,
E: Display + Send + 'static,
F: Future<Output = Result<T, E>> + Send + 'static,
{
let canceler = task::Canceler::new();
let cancel = canceler.signal();
let mut signals = Signals::new(TERM_SIGNALS).expect("Failed to register signal handler");
thread::start("af_core::run canceler", move || {
let mut iter = signals.into_iter();
iter.next();
warn!("The process received a termination signal. Canceling the main task…");
canceler.cancel();
});
run(async { func(cancel).await })
}