1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::time::Duration;

use tokio::time::timeout;
use tracing::trace;

use crate::network::node::NetworkNode;

pub(crate) async fn verify_nodes(nodes: &[&NetworkNode]) -> Result<(), anyhow::Error> {
    timeout(Duration::from_secs(90), check_nodes(nodes))
        .await
        .map_err(|_| anyhow::anyhow!("one or more nodes are not ready!"))
}

// TODO: we should inject in someway the logic to make the request
// in order to allow us to `mock` and easily test this.
// maybe moved to the provider with a NodeStatus, and some helpers like wait_running, wait_ready, etc... ? to be discussed
async fn check_nodes(nodes: &[&NetworkNode]) {
    loop {
        let tasks: Vec<_> = nodes
            .iter()
            .map(|node| {
                trace!("🔎 checking node: {} ", node.name);
                reqwest::get(node.prometheus_uri.clone())
            })
            .collect();

        let all_ready = futures::future::try_join_all(tasks).await;
        if all_ready.is_ok() {
            return;
        }

        tokio::time::sleep(Duration::from_millis(1000)).await;
    }
}