forest/cli/subcommands/
wait_api_cmd.rs1use crate::rpc::{self, prelude::*};
5use std::time::{Duration, Instant};
6
7#[derive(Debug, clap::Args)]
8pub struct WaitApiCommand {
9 #[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}