-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Errors during partial shutdown no longer cause global shutdown
If an error during a partial shutdown happens, that error should be delivered to the task that issued the partial shutdown. Instead, there was a bug where the error would cause a global shutdown. This change fixes that bug.
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! This example demonstrates how to perform a partial shutdown of the system. | ||
//! | ||
//! Subsys1 will perform a partial shutdown after 5 seconds, which will in turn | ||
//! shut down Subsys2 and Subsys3, leaving Subsys1 running. | ||
|
||
use anyhow::Result; | ||
use env_logger::{Builder, Env}; | ||
use tokio::time::{sleep, Duration}; | ||
use tokio_graceful_shutdown::{SubsystemHandle, Toplevel}; | ||
|
||
async fn subsys3(subsys: SubsystemHandle) -> Result<()> { | ||
log::info!("Subsys3 started."); | ||
subsys.on_shutdown_requested().await; | ||
panic!("Subsystem3 threw an error!") | ||
} | ||
|
||
async fn subsys2(mut subsys: SubsystemHandle) -> Result<()> { | ||
log::info!("Subsys2 started."); | ||
subsys.start("Subsys3", subsys3); | ||
subsys.on_shutdown_requested().await; | ||
log::info!("Subsys2 stopped."); | ||
Ok(()) | ||
} | ||
|
||
async fn subsys1(mut subsys: SubsystemHandle) -> Result<()> { | ||
// This subsystem shuts down the nested subsystem after 5 seconds. | ||
log::info!("Subsys1 started."); | ||
|
||
log::info!("Starting nested subsystem ..."); | ||
let nested_subsys = subsys.start("Subsys2", subsys2); | ||
log::info!("Nested subsystem started."); | ||
|
||
tokio::select! { | ||
_ = subsys.on_shutdown_requested() => (), | ||
_ = sleep(Duration::from_secs(1)) => { | ||
log::info!("Shutting down nested subsystem ..."); | ||
if let Err(err) = subsys.perform_partial_shutdown(nested_subsys).await{ | ||
log::warn!("Partial shutdown failed: {}", err); | ||
}; | ||
log::info!("Nested subsystem shut down."); | ||
subsys.on_shutdown_requested().await; | ||
} | ||
}; | ||
|
||
log::info!("Subsys1 stopped."); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
// Init logging | ||
Builder::from_env(Env::default().default_filter_or("debug")).init(); | ||
|
||
// Create toplevel | ||
Toplevel::new() | ||
.start("Subsys1", subsys1) | ||
.catch_signals() | ||
.handle_shutdown_requests(Duration::from_millis(1000)) | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters