use std::{io, time::Duration};
use tokio::time::sleep;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{EnvFilter, Layer, fmt, layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
fmt::Layer::new().with_writer(io::stderr).with_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.with_env_var("WORTERBUCH_LOG")
.from_env_lossy(),
),
)
.init();
tosub::Subsystem::build_root("hello_world")
.catch_signals()
.with_timeout(Duration::from_secs(5))
.start(|root| async move {
let child1 = root.spawn("child 1", |subsystem| async move {
println!("Hello from {}", subsystem.name());
sleep(Duration::from_secs(1)).await;
panic!("Oopsie whoopsie!");
#[allow(unreachable_code)]
Ok::<(), miette::ErrReport>(())
});
let child2 = root.spawn("child 2", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} needs two seconds to shut down ...", subsystem.name());
sleep(Duration::from_secs(2)).await;
Ok::<(), miette::ErrReport>(())
});
root.spawn("child 3", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} needs three seconds to shut down ...", subsystem.name());
sleep(Duration::from_secs(3)).await;
Ok::<(), miette::ErrReport>(())
});
child1.spawn("grandchild 1", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} shuts down immedaiately.", subsystem.name());
Ok::<(), miette::ErrReport>(())
});
let grandchild2 = child1.spawn("grandchild 2", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} needs a second to shut down ...", subsystem.name());
sleep(Duration::from_secs(1)).await;
Ok::<(), miette::ErrReport>(())
});
child2.spawn("grandchild 3", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} needs two second to shut down ...", subsystem.name());
sleep(Duration::from_secs(2)).await;
Ok::<(), miette::ErrReport>(())
});
grandchild2.spawn("great grandchild 1", |subsystem| async move {
println!("Hello from {}", subsystem.name());
subsystem.shutdown_requested().await;
println!("{} needs a second to shut down ...", subsystem.name());
sleep(Duration::from_secs(1)).await;
Ok::<(), miette::ErrReport>(())
});
root.shutdown_requested().await;
Ok::<(), miette::ErrReport>(())
})
.join()
.await;
}