Skip to content

Commit

Permalink
Add second subsystem to basic usage example, as it was unclear in the…
Browse files Browse the repository at this point in the history
… example that the start function could be called multiple times
  • Loading branch information
Finomnis committed Dec 18, 2021
1 parent 7babc57 commit 36327c9
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions examples/01_normal_shutdown.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//! This example demonstrates the basic usage pattern of this crate.
//!
//! It shows that a subsystem gets started, and when the program
//! gets shut down (by pressing Ctrl-C), the subsystem gets shut down
//! It shows that subsystems get started, and when the program
//! gets shut down (by pressing Ctrl-C), the subsystems get shut down
//! gracefully.
//!
//! In this case, the subsystem is an async function.
//! This crate supports async functions and async coroutines.
//!
//! If custom arguments for the subsystem coroutine are required,
//! If custom arguments for the subsystem coroutines are required,
//! a struct has to be used instead, as seen in other examples.
use anyhow::Result;
Expand All @@ -19,11 +16,20 @@ async fn subsys1(subsys: SubsystemHandle) -> Result<()> {
log::info!("Subsystem1 started.");
subsys.on_shutdown_requested().await;
log::info!("Shutting down Subsystem1 ...");
sleep(Duration::from_millis(500)).await;
sleep(Duration::from_millis(400)).await;
log::info!("Subsystem1 stopped.");
Ok(())
}

async fn subsys2(subsys: SubsystemHandle) -> Result<()> {
log::info!("Subsystem2 started.");
subsys.on_shutdown_requested().await;
log::info!("Shutting down Subsystem2 ...");
sleep(Duration::from_millis(500)).await;
log::info!("Subsystem2 stopped.");
Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
// Init logging
Expand All @@ -32,6 +38,7 @@ async fn main() -> Result<()> {
// Create toplevel
Toplevel::new()
.start("Subsys1", subsys1)
.start("Subsys2", subsys2)
.catch_signals()
.handle_shutdown_requests(Duration::from_millis(1000))
.await
Expand Down

0 comments on commit 36327c9

Please sign in to comment.