From 36327c92f72d6a5eebada1e89b05c005ec1732ca Mon Sep 17 00:00:00 2001 From: Finomnis Date: Sat, 18 Dec 2021 15:52:13 +0100 Subject: [PATCH] Add second subsystem to basic usage example, as it was unclear in the example that the start function could be called multiple times --- examples/01_normal_shutdown.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/examples/01_normal_shutdown.rs b/examples/01_normal_shutdown.rs index caec1f6..ebe294a 100644 --- a/examples/01_normal_shutdown.rs +++ b/examples/01_normal_shutdown.rs @@ -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; @@ -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 @@ -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