Skip to main content

forest/cli/subcommands/
wait_api_cmd.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use crate::rpc::{self, prelude::*};
5use std::time::{Duration, Instant};
6
7#[derive(Debug, clap::Args)]
8pub struct WaitApiCommand {
9    /// duration to wait till fail, e.g. `5s`, `5seconds`, `1m`, `1min`, etc.
10    #[arg(long)]
11    timeout: Option<humantime::Duration>,
12}
13
14impl WaitApiCommand {
15    pub async fn run(self, client: rpc::Client) -> anyhow::Result<()> {
16        let request = Version::request(())?.with_timeout(Duration::from_secs(1));
17        let timeout = self.timeout.map(Duration::from);
18        let start = Instant::now();
19        let mut success = false;
20        loop {
21            match timeout {
22                Some(timeout) if start.elapsed() > timeout => break,
23                _ => {}
24            }
25            if let Ok(_r) = client.call(request.clone()).await {
26                success = true;
27                break;
28            }
29            println!("Not online yet...");
30            tokio::time::sleep(Duration::from_secs(1)).await;
31        }
32
33        if success {
34            println!("Forest API is online!");
35        } else {
36            println!("Timed out waiting for the API to come online");
37        }
38
39        Ok(())
40    }
41}