use std::sync::{Once, OnceLock};
use encore_sdk_core::Runtime as CoreRuntime;
static RUNTIME: OnceLock<CoreRuntime> = OnceLock::new();
pub(crate) fn core() -> &'static CoreRuntime {
static WATCH: Once = Once::new();
let core = RUNTIME.get_or_init(|| {
std::thread::scope(|scope| scope.spawn(build).join())
.unwrap_or_else(|panic| std::panic::resume_unwind(panic))
});
WATCH.call_once(|| crate::shutdown::watch(core));
core
}
fn build() -> CoreRuntime {
CoreRuntime::builder()
.with_test_mode_from_env()
.with_exec_mode_from_env()
.with_runtime_config_from_env()
.build()
.unwrap_or_else(|e| {
use std::io::Write;
let _ = writeln!(
std::io::stderr(),
"encore: failed to initialize the SDK runtime: {e:#}"
);
std::process::exit(1);
})
}
pub fn initialize() {
core();
crate::pubsub::start_registered();
}
pub fn tokio_handle() -> tokio::runtime::Handle {
core().tokio_handle().clone()
}
pub fn run_blocking() {
crate::pubsub::start_registered();
let handle = tokio_handle();
handle.block_on(crate::shutdown::signal());
let result = handle.block_on(crate::shutdown::drained());
std::process::exit(result.code());
}