Skip to content

Commit 23ed22b

Browse files
authored
refactor(cli-utils): RuntimeManager API Updates (#418)
* refactor(cli-utils): simplify RuntimeManager API Consolidate runtime utilities by replacing separate build_runtime(), run_until_ctrl_c(), and run_until_ctrl_c_fallible() methods with a simpler tokio_runtime() and unified run_until_ctrl_c() that handles runtime creation internally. Ported from #395. * fix(cli-utils): bias ctrl+c
1 parent fe8d4cd commit 23ed22b

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

crates/shared/cli-utils/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ base-cli-utils = { git = "https://github.com/base/node-reth" }
2020
```
2121

2222
```rust,ignore
23-
use base_cli_utils::{GlobalArgs, Version};
24-
use base_cli_utils::runtime::{build_runtime, run_until_ctrl_c};
23+
use base_cli_utils::{GlobalArgs, RuntimeManager, Version};
2524
use clap::Parser;
2625
2726
#[derive(Parser)]
@@ -32,10 +31,12 @@ struct MyCli {
3231
3332
fn main() -> eyre::Result<()> {
3433
Version::init();
35-
let cli = MyCli::parse();
34+
let _cli = MyCli::parse();
3635
37-
let runtime = build_runtime()?;
38-
runtime.block_on(run_until_ctrl_c(async { /* ... */ }))
36+
RuntimeManager::run_until_ctrl_c(async {
37+
// ... your async code ...
38+
Ok(())
39+
})
3940
}
4041
```
4142

crates/shared/cli-utils/src/runtime.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,27 @@ use std::future::Future;
77
pub struct RuntimeManager;
88

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

18-
/// Runs a future to completion, returning early on Ctrl+C.
19-
pub async fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
20-
where
21-
F: Future<Output = ()>,
22-
{
23-
let ctrl_c = async {
24-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
25-
};
26-
27-
tokio::select! {
28-
biased;
29-
() = ctrl_c => Ok(()),
30-
() = fut => Ok(()),
31-
}
32-
}
33-
34-
/// Runs a fallible future to completion, returning early on Ctrl+C.
35-
pub async fn run_until_ctrl_c_fallible<F>(fut: F) -> eyre::Result<()>
16+
/// Run a fallible future until ctrl-c is pressed.
17+
pub fn run_until_ctrl_c<F>(fut: F) -> eyre::Result<()>
3618
where
3719
F: Future<Output = eyre::Result<()>>,
3820
{
39-
let ctrl_c = async {
40-
tokio::signal::ctrl_c().await.expect("Failed to install Ctrl+C handler");
41-
};
42-
43-
tokio::select! {
44-
biased;
45-
() = ctrl_c => Ok(()),
46-
result = fut => result,
47-
}
21+
let rt = Self::tokio_runtime().map_err(|e| eyre::eyre!(e))?;
22+
rt.block_on(async move {
23+
tokio::select! {
24+
biased;
25+
_ = tokio::signal::ctrl_c() => {
26+
tracing::info!(target: "cli", "Received Ctrl-C, shutting down...");
27+
Ok(())
28+
}
29+
res = fut => res,
30+
}
31+
})
4832
}
4933
}

0 commit comments

Comments
 (0)