From 5e73665ac10b55def42adbcbd777c8948f4437dc Mon Sep 17 00:00:00 2001 From: pgherveou Date: Mon, 23 Oct 2023 10:25:25 +0200 Subject: [PATCH] nits --- crates/examples/Cargo.toml | 1 + crates/examples/examples/simple_network_example.rs | 4 +++- crates/orchestrator/src/spawner.rs | 12 ++++++------ 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/examples/Cargo.toml b/crates/examples/Cargo.toml index dce89cfbc..341a4f7d3 100644 --- a/crates/examples/Cargo.toml +++ b/crates/examples/Cargo.toml @@ -13,3 +13,4 @@ provider = { path = "../provider" } # to review the exports for neeeded types support = { path = "../support" } tokio = { workspace = true } +futures = { workspace = true } diff --git a/crates/examples/examples/simple_network_example.rs b/crates/examples/examples/simple_network_example.rs index 2c4881465..3367e0e1e 100644 --- a/crates/examples/examples/simple_network_example.rs +++ b/crates/examples/examples/simple_network_example.rs @@ -1,6 +1,7 @@ // use std::time::Duration; use configuration::NetworkConfig; +use futures::stream::StreamExt; use orchestrator::Orchestrator; use provider::NativeProvider; use support::{fs::local::LocalFileSystem, process::os::OsProcessManager}; @@ -18,7 +19,8 @@ async fn main() -> Result<(), Box> { println!("🚀🚀🚀🚀 network deployed"); let client = network.get_node("alice")?.client(); - let mut blocks = client.blocks().subscribe_finalized().await?; + let mut blocks = client.blocks().subscribe_finalized().await?.take(3); + while let Some(block) = blocks.next().await { println!("Block #{}", block?.header().number); } diff --git a/crates/orchestrator/src/spawner.rs b/crates/orchestrator/src/spawner.rs index 9dc7dc37a..5ab9295ca 100644 --- a/crates/orchestrator/src/spawner.rs +++ b/crates/orchestrator/src/spawner.rs @@ -163,7 +163,7 @@ where println!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name); println!("\n"); - let client = retry(|| async { OnlineClient::from_url(&ws_uri).await }) + let client = retry(5, || async { OnlineClient::from_url(&ws_uri).await }) .await .context(format!("Failed to connect to node rpc at {ws_uri}"))?; @@ -182,18 +182,18 @@ where )) } -async fn retry(connect: F) -> Result +async fn retry(retry_count: u32, connect: F) -> Result where T: Future>, F: Fn() -> T, { - let mut retries = 5; + let mut failed_count = 0; loop { match connect().await { - Err(_) if retries >= 0 => { - println!("Error connecting, retrying ..."); + Err(_) if retry_count > failed_count => { + failed_count += 1; + println!("Error connecting, retrying {failed_count}/{retry_count} ..."); tokio::time::sleep(std::time::Duration::from_secs(1)).await; - retries -= 1; }, res => break res, }