chore(rust): tidier libpk::main macro impl

This commit is contained in:
alyssa 2025-05-17 16:19:30 +00:00
parent 7737850afb
commit f9a6c2cefe
18 changed files with 90 additions and 64 deletions

View file

@ -8,6 +8,7 @@ anyhow = { workspace = true }
fred = { workspace = true }
lazy_static = { workspace = true }
metrics = { workspace = true }
pk_macros = { path = "../macros" }
sentry = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

View file

@ -14,7 +14,9 @@ pub mod state;
pub mod _config;
pub use crate::_config::CONFIG as config;
// functions in this file are only used by the main function below
// functions in this file are only used by the main function in macros/entrypoint.rs
pub use pk_macros::main;
pub fn init_logging(component: &str) {
let sentry_layer =
@ -68,28 +70,3 @@ pub fn init_sentry() -> sentry::ClientInitGuard {
..Default::default()
})
}
#[macro_export]
macro_rules! main {
($component:expr) => {
fn main() -> anyhow::Result<()> {
let _sentry_guard = libpk::init_sentry();
// we might also be able to use env!("CARGO_CRATE_NAME") here
libpk::init_logging($component);
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
if let Err(error) = libpk::init_metrics() {
tracing::error!(?error, "failed to init metrics collector");
};
tracing::info!("hello world");
if let Err(error) = real_main().await {
tracing::error!(?error, "failed to run service");
};
});
Ok(())
}
};
}