use std::{io, process::ExitCode, time::Duration};
use tokio::select;
use tokio::time::sleep;
use tracing::info;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{EnvFilter, Layer, fmt, layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() -> miette::Result<ExitCode> {
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(|root| async move {
let child1 = root.spawn("child 1", child1);
let child2 = root.spawn("child 2", child2);
let child3 = root.spawn("child 3", child3);
select! {
_ = child3.join() => {
info!("Child 3 done");
},
_ = root.shutdown_requested() => (),
}
select! {
_ = child2.join() => {
info!("Child 2 done");
},
_ = root.shutdown_requested() => (),
}
select! {
_ = child1.join() => {
info!("Child 1 done");
},
_ = root.shutdown_requested() => (),
}
Ok::<(), miette::Report>(())
})
.await
}
async fn child1(subsys: tosub::Subsystem) -> miette::Result<()> {
info!("Hello from {}", subsys.name());
select! {
_ = sleep(Duration::from_secs(1)) => info!("Child 1 completed work"),
_ = subsys.shutdown_requested() => info!("Child 1 received shutdown request"),
}
Ok(())
}
async fn child2(subsys: tosub::Subsystem) -> miette::Result<()> {
info!("Hello from {}", subsys.name());
select! {
_ = sleep(Duration::from_secs(2)) => info!("Child 2 completed work"),
_ = subsys.shutdown_requested() => info!("Child 2 received shutdown request"),
}
Err(miette::miette!("Child 2 encountered an error"))
}
async fn child3(subsys: tosub::Subsystem) -> miette::Result<()> {
info!("Hello from {}", subsys.name());
select! {
_ = sleep(Duration::from_secs(3)) => info!("Child 3 completed work"),
_ = subsys.shutdown_requested() => info!("Child 3 received shutdown request"),
}
Ok(())
}