Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions crates/shared/cli-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ base-cli-utils = { git = "https://github.com/base/node-reth" }
```

```rust,ignore
use base_cli_utils::{GlobalArgs, Version};
use base_cli_utils::runtime::{build_runtime, run_until_ctrl_c};
use base_cli_utils::{GlobalArgs, RuntimeManager, Version};
use clap::Parser;

#[derive(Parser)]
Expand All @@ -32,10 +31,12 @@ struct MyCli {

fn main() -> eyre::Result<()> {
Version::init();
let cli = MyCli::parse();
let _cli = MyCli::parse();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the commented code sample underscored 😅 is the linter running on these code blocks too somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea the linter runs on rust code docs. Can ignore by writing ```rust,ignore


let runtime = build_runtime()?;
runtime.block_on(run_until_ctrl_c(async { /* ... */ }))
RuntimeManager::run_until_ctrl_c(async {
// ... your async code ...
Ok(())
})
}
```

Expand Down
50 changes: 17 additions & 33 deletions crates/shared/cli-utils/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,27 @@ use std::future::Future;
pub struct RuntimeManager;

impl RuntimeManager {
/// Builds a multi-threaded Tokio runtime with all features enabled.
pub fn build_runtime() -> eyre::Result<tokio::runtime::Runtime> {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| eyre::eyre!("Failed to build tokio runtime: {}", e))
/// Creates a new default tokio multi-thread [Runtime](tokio::runtime::Runtime) with all
/// features enabled.
pub fn tokio_runtime() -> Result<tokio::runtime::Runtime, std::io::Error> {
tokio::runtime::Builder::new_multi_thread().enable_all().build()
}

/// Runs a future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = ()>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};

tokio::select! {
biased;
() = ctrl_c => Ok(()),
() = fut => Ok(()),
}
}

/// Runs a fallible future to completion, returning early on Ctrl+C.
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
/// Run a fallible future until ctrl-c is pressed.
pub fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
where
F: Future<Output = eyre::Result<()>>,
{
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
};

tokio::select! {
biased;
() = ctrl_c => Ok(()),
result = fut => result,
}
let rt = Self::tokio_runtime().map_err(|e| eyre::eyre!(e))?;
rt.block_on(async move {
tokio::select! {
biased;
_ = tokio::signal::ctrl_c() => {
tracing::info!(target: "cli", "Received Ctrl-C, shutting down...");
Ok(())
}
res = fut => res,
}
})
}
}
Loading