use std::{io, time::Duration};
use tokio::{select, time::sleep};
use tosub::Subsystem;
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::{EnvFilter, Layer, fmt, layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() -> miette::Result<()> {
tracing_subscriber::registry()
.with(
fmt::Layer::new().with_writer(io::stderr).with_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
),
)
.init();
tosub::build_root("hello_world")
.catch_signals()
.with_timeout(Duration::from_secs(5))
.start(run)
.await?;
Ok(())
}
async fn run(subsys: Subsystem) -> miette::Result<()> {
println!("Hello from {}", subsys.name());
select! {
_ = async {
info!("Doing some work, this should take about two seconds. Press Ctrl+C to stop...");
sleep(Duration::from_secs(2)).await;
} => (),
_ = subsys.shutdown_requested() => (),
}
info!(
"Stopped. Cleaning up, this should take about one second. Press Ctrl+C to exit immediately..."
);
sleep(Duration::from_secs(1)).await;
Ok(())
}