light_prover_client/
prover.rs

1use std::{
2    process::Command,
3    sync::atomic::{AtomicBool, Ordering},
4    thread::sleep,
5    time::Duration,
6};
7
8use tracing::info;
9
10use crate::{
11    constants::{HEALTH_CHECK, SERVER_ADDRESS},
12    helpers::get_project_root,
13};
14
15static IS_LOADING: AtomicBool = AtomicBool::new(false);
16
17pub async fn spawn_prover() {
18    if let Some(_project_root) = get_project_root() {
19        let prover_path: &str = {
20            #[cfg(feature = "devenv")]
21            {
22                &format!("{}/{}", _project_root.trim(), "cli/test_bin/run")
23            }
24            #[cfg(not(feature = "devenv"))]
25            {
26                println!("Running in production mode, using prover binary");
27                "light"
28            }
29        };
30
31        if !health_check(10, 1).await && !IS_LOADING.load(Ordering::Relaxed) {
32            IS_LOADING.store(true, Ordering::Relaxed);
33
34            let command = Command::new(prover_path)
35                .arg("start-prover")
36                .spawn()
37                .expect("Failed to start prover process");
38
39            let _ = command.wait_with_output();
40
41            let health_result = health_check(120, 1).await;
42            if health_result {
43                info!("Prover started successfully");
44            } else {
45                panic!("Failed to start prover, health check failed.");
46            }
47        }
48    } else {
49        panic!("Failed to find project root.");
50    };
51}
52
53pub async fn health_check(retries: usize, timeout: usize) -> bool {
54    let client = reqwest::Client::new();
55    let mut result = false;
56    for _ in 0..retries {
57        match client
58            .get(format!("{}{}", SERVER_ADDRESS, HEALTH_CHECK))
59            .send()
60            .await
61        {
62            Ok(_) => {
63                result = true;
64                break;
65            }
66            Err(_) => {
67                sleep(Duration::from_secs(timeout as u64));
68            }
69        }
70    }
71    result
72}