reth_cli_runner/
lib.rs

1//! A tokio based CLI runner.
2
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10
11//! Entrypoint for running commands.
12
13use reth_tasks::{TaskExecutor, TaskManager};
14use std::{future::Future, pin::pin, sync::mpsc, time::Duration};
15use tracing::{debug, error, trace};
16
17/// Executes CLI commands.
18///
19/// Provides utilities for running a cli command to completion.
20#[derive(Debug)]
21#[non_exhaustive]
22pub struct CliRunner {
23    tokio_runtime: tokio::runtime::Runtime,
24}
25
26impl CliRunner {
27    /// Attempts to create a new [`CliRunner`] using the default tokio
28    /// [`Runtime`](tokio::runtime::Runtime).
29    ///
30    /// The default tokio runtime is multi-threaded, with both I/O and time drivers enabled.
31    pub fn try_default_runtime() -> Result<Self, std::io::Error> {
32        Ok(Self { tokio_runtime: tokio_runtime()? })
33    }
34
35    /// Create a new [`CliRunner`] from a provided tokio [`Runtime`](tokio::runtime::Runtime).
36    pub fn from_runtime(tokio_runtime: tokio::runtime::Runtime) -> Self {
37        Self { tokio_runtime }
38    }
39}
40
41// === impl CliRunner ===
42
43impl CliRunner {
44    /// Executes the given _async_ command on the tokio runtime until the command future resolves or
45    /// until the process receives a `SIGINT` or `SIGTERM` signal.
46    ///
47    /// Tasks spawned by the command via the [`TaskExecutor`] are shut down and an attempt is made
48    /// to drive their shutdown to completion after the command has finished.
49    pub fn run_command_until_exit<F, E>(
50        self,
51        command: impl FnOnce(CliContext) -> F,
52    ) -> Result<(), E>
53    where
54        F: Future<Output = Result<(), E>>,
55        E: Send + Sync + From<std::io::Error> + From<reth_tasks::PanickedTaskError> + 'static,
56    {
57        let AsyncCliRunner { context, mut task_manager, tokio_runtime } =
58            AsyncCliRunner::new(self.tokio_runtime);
59
60        // Executes the command until it finished or ctrl-c was fired
61        let command_res = tokio_runtime.block_on(run_to_completion_or_panic(
62            &mut task_manager,
63            run_until_ctrl_c(command(context)),
64        ));
65
66        if command_res.is_err() {
67            error!(target: "reth::cli", "shutting down due to error");
68        } else {
69            debug!(target: "reth::cli", "shutting down gracefully");
70            // after the command has finished or exit signal was received we shutdown the task
71            // manager which fires the shutdown signal to all tasks spawned via the task
72            // executor and awaiting on tasks spawned with graceful shutdown
73            task_manager.graceful_shutdown_with_timeout(Duration::from_secs(5));
74        }
75
76        // `drop(tokio_runtime)` would block the current thread until its pools
77        // (including blocking pool) are shutdown. Since we want to exit as soon as possible, drop
78        // it on a separate thread and wait for up to 5 seconds for this operation to
79        // complete.
80        let (tx, rx) = mpsc::channel();
81        std::thread::Builder::new()
82            .name("tokio-runtime-shutdown".to_string())
83            .spawn(move || {
84                drop(tokio_runtime);
85                let _ = tx.send(());
86            })
87            .unwrap();
88
89        let _ = rx.recv_timeout(Duration::from_secs(5)).inspect_err(|err| {
90            debug!(target: "reth::cli", %err, "tokio runtime shutdown timed out");
91        });
92
93        command_res
94    }
95
96    /// Executes a regular future until completion or until external signal received.
97    pub fn run_until_ctrl_c<F, E>(self, fut: F) -> Result<(), E>
98    where
99        F: Future<Output = Result<(), E>>,
100        E: Send + Sync + From<std::io::Error> + 'static,
101    {
102        let tokio_runtime = tokio_runtime()?;
103        tokio_runtime.block_on(run_until_ctrl_c(fut))?;
104        Ok(())
105    }
106
107    /// Executes a regular future as a spawned blocking task until completion or until external
108    /// signal received.
109    ///
110    /// See [`Runtime::spawn_blocking`](tokio::runtime::Runtime::spawn_blocking) .
111    pub fn run_blocking_until_ctrl_c<F, E>(self, fut: F) -> Result<(), E>
112    where
113        F: Future<Output = Result<(), E>> + Send + 'static,
114        E: Send + Sync + From<std::io::Error> + 'static,
115    {
116        let tokio_runtime = tokio_runtime()?;
117        let handle = tokio_runtime.handle().clone();
118        let fut = tokio_runtime.handle().spawn_blocking(move || handle.block_on(fut));
119        tokio_runtime
120            .block_on(run_until_ctrl_c(async move { fut.await.expect("Failed to join task") }))?;
121
122        // drop the tokio runtime on a separate thread because drop blocks until its pools
123        // (including blocking pool) are shutdown. In other words `drop(tokio_runtime)` would block
124        // the current thread but we want to exit right away.
125        std::thread::Builder::new()
126            .name("tokio-runtime-shutdown".to_string())
127            .spawn(move || drop(tokio_runtime))
128            .unwrap();
129
130        Ok(())
131    }
132}
133
134/// [`CliRunner`] configuration when executing commands asynchronously
135struct AsyncCliRunner {
136    context: CliContext,
137    task_manager: TaskManager,
138    tokio_runtime: tokio::runtime::Runtime,
139}
140
141// === impl AsyncCliRunner ===
142
143impl AsyncCliRunner {
144    /// Given a tokio [`Runtime`](tokio::runtime::Runtime), creates additional context required to
145    /// execute commands asynchronously.
146    fn new(tokio_runtime: tokio::runtime::Runtime) -> Self {
147        let task_manager = TaskManager::new(tokio_runtime.handle().clone());
148        let task_executor = task_manager.executor();
149        Self { context: CliContext { task_executor }, task_manager, tokio_runtime }
150    }
151}
152
153/// Additional context provided by the [`CliRunner`] when executing commands
154#[derive(Debug)]
155pub struct CliContext {
156    /// Used to execute/spawn tasks
157    pub task_executor: TaskExecutor,
158}
159
160/// Creates a new default tokio multi-thread [Runtime](tokio::runtime::Runtime) with all features
161/// enabled
162pub fn tokio_runtime() -> Result<tokio::runtime::Runtime, std::io::Error> {
163    tokio::runtime::Builder::new_multi_thread().enable_all().build()
164}
165
166/// Runs the given future to completion or until a critical task panicked.
167///
168/// Returns the error if a task panicked, or the given future returned an error.
169async fn run_to_completion_or_panic<F, E>(tasks: &mut TaskManager, fut: F) -> Result<(), E>
170where
171    F: Future<Output = Result<(), E>>,
172    E: Send + Sync + From<reth_tasks::PanickedTaskError> + 'static,
173{
174    {
175        let fut = pin!(fut);
176        tokio::select! {
177            err = tasks => {
178                return Err(err.into())
179            },
180            res = fut => res?,
181        }
182    }
183    Ok(())
184}
185
186/// Runs the future to completion or until:
187/// - `ctrl-c` is received.
188/// - `SIGTERM` is received (unix only).
189async fn run_until_ctrl_c<F, E>(fut: F) -> Result<(), E>
190where
191    F: Future<Output = Result<(), E>>,
192    E: Send + Sync + 'static + From<std::io::Error>,
193{
194    let ctrl_c = tokio::signal::ctrl_c();
195
196    #[cfg(unix)]
197    {
198        let mut stream = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
199        let sigterm = stream.recv();
200        let sigterm = pin!(sigterm);
201        let ctrl_c = pin!(ctrl_c);
202        let fut = pin!(fut);
203
204        tokio::select! {
205            _ = ctrl_c => {
206                trace!(target: "reth::cli", "Received ctrl-c");
207            },
208            _ = sigterm => {
209                trace!(target: "reth::cli", "Received SIGTERM");
210            },
211            res = fut => res?,
212        }
213    }
214
215    #[cfg(not(unix))]
216    {
217        let ctrl_c = pin!(ctrl_c);
218        let fut = pin!(fut);
219
220        tokio::select! {
221            _ = ctrl_c => {
222                trace!(target: "reth::cli", "Received ctrl-c");
223            },
224            res = fut => res?,
225        }
226    }
227
228    Ok(())
229}