-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tokio taskname registration for use in tokio-console
This uses the `tokio-unstable` feature that has to be enabled and also the runtime variable `RUSTFLAGS="--cfg tokio_unstable"` must be set to enable tokio taskname registration. See example program `tokio-console.rs` for usage.
- Loading branch information
1 parent
689208e
commit a43abad
Showing
3 changed files
with
94 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
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,60 @@ | ||
//! This example demonstrates how to use the tokio-console application for tracing tokio tasks's | ||
//! runtime behaviour. Subsystems will appear under their registration names. | ||
//! | ||
//! To make this work, | ||
//! | ||
//! * Compile `tokio-graceful-shutdown` with the `tokio-unstable` feature to register subsystem | ||
//! task names. | ||
//! | ||
//! * Run this example with the environment variable: | ||
//! | ||
//! ``` | ||
//! RUSTFLAGS=="--cfg tokio_unstable" | ||
//! ``` | ||
//! | ||
//! * Run the `tokio-console` CLI application and watch your snappy low-latency tasks | ||
use miette::Result; | ||
use tokio::time::{sleep, Duration}; | ||
use tokio_graceful_shutdown::{SubsystemBuilder, SubsystemHandle, Toplevel}; | ||
use tracing_subscriber::prelude::*; | ||
|
||
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<()> { | ||
let console_layer = console_subscriber::spawn(); | ||
// Init logging | ||
tracing_subscriber::registry() | ||
.with(console_layer) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
|
||
// Setup and execute subsystem tree | ||
Toplevel::new(|s| async move { | ||
s.start(SubsystemBuilder::new("Subsys1", subsys1)); | ||
s.start(SubsystemBuilder::new("Subsys2", subsys2)); | ||
s.start(SubsystemBuilder::new("Subsys3", subsys1)); | ||
}) | ||
.catch_signals() | ||
.handle_shutdown_requests(Duration::from_millis(1000)) | ||
.await | ||
.map_err(Into::into) | ||
} |
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