-
Notifications
You must be signed in to change notification settings - Fork 11
/
06_nested_subsystems.rs
42 lines (37 loc) · 1.27 KB
/
06_nested_subsystems.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! This example demonstrates how one subsystem can launch another
//! nested subsystem.
use miette::Result;
use tokio::time::{sleep, Duration};
use tokio_graceful_shutdown::{SubsystemBuilder, SubsystemHandle, Toplevel};
async fn subsys1(subsys: SubsystemHandle) -> Result<()> {
subsys.start(SubsystemBuilder::new("Subsys2", subsys2));
tracing::info!("Subsystem1 started.");
subsys.on_shutdown_requested().await;
tracing::info!("Shutting down Subsystem1 ...");
sleep(Duration::from_millis(500)).await;
tracing::info!("Subsystem1 stopped.");
Ok(())
}
async fn subsys2(subsys: SubsystemHandle) -> Result<()> {
tracing::info!("Subsystem2 started.");
subsys.on_shutdown_requested().await;
tracing::info!("Shutting down Subsystem2 ...");
sleep(Duration::from_millis(500)).await;
tracing::info!("Subsystem2 stopped.");
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
// Init logging
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
// Setup and execute subsystem tree
Toplevel::new(|s| async move {
s.start(SubsystemBuilder::new("Subsys1", subsys1));
})
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(1000))
.await
.map_err(Into::into)
}