From cb09d6c3e9f32820bbd5883fd4a7de8e89863f17 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 12 Jul 2022 11:51:49 +0200 Subject: [PATCH 1/5] Wait for sbot --- Cargo.lock | 2 +- peach-config/src/error.rs | 2 ++ peach-config/src/main.rs | 16 ++++++++++++ peach-config/src/wait_for_sbot.rs | 41 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 peach-config/src/wait_for_sbot.rs diff --git a/Cargo.lock b/Cargo.lock index 9297288..ffaa736 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2407,7 +2407,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f77e66f6d6d898cbbd4a09c48fd3507cfc210b7c83055de02a38b5f7a1e6d216" dependencies = [ "libc", - "time 0.3.11", + "time 0.1.44", ] [[package]] diff --git a/peach-config/src/error.rs b/peach-config/src/error.rs index 862a866..9b47e6a 100644 --- a/peach-config/src/error.rs +++ b/peach-config/src/error.rs @@ -42,6 +42,8 @@ pub enum PeachConfigError { Golgi { source: GolgiError }, #[snafu(display("{}", message))] CmdInputError { message: String }, + #[snafu(display("{}", message))] + WaitForSbotError { message: String }, } impl From for PeachConfigError { diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index 2fd7624..96493a6 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -10,6 +10,7 @@ mod setup_peach_deb; mod status; mod update; mod utils; +mod wait_for_sbot; use clap::arg_enum; use log::error; @@ -61,6 +62,10 @@ enum PeachConfig { /// It takes an address argument of the form host:port #[structopt(name = "publish-address")] PublishAddress(PublishAddressOpts), + + /// Wait for a successful connection to sbot + #[structopt(name = "wait-for-sbot")] + WaitForSbot, } #[derive(StructOpt, Debug)] @@ -192,6 +197,17 @@ async fn run() { ) } } + }, + PeachConfig::WaitForSbot => { + match wait_for_sbot::wait_for_sbot().await { + Ok(_) => {} + Err(err) => { + error!( + "peach-config did not successfully connect to sbot: {}", + err + ) + } + } } } } diff --git a/peach-config/src/wait_for_sbot.rs b/peach-config/src/wait_for_sbot.rs new file mode 100644 index 0000000..d291e95 --- /dev/null +++ b/peach-config/src/wait_for_sbot.rs @@ -0,0 +1,41 @@ +use std::{thread, time}; + +use crate::error::PeachConfigError; +use peach_lib::sbot::init_sbot; + +/// Utility function to wait for a successful whoami call with sbot +/// After each attempt to call whoami it waits 2 seconds, +/// and if after max_num_attempts (8) there is no successful whoami call +/// it returns an Error. Otherwise it returns Ok(sbot_id). +pub async fn wait_for_sbot() -> Result { + let mut num_attempts = 0; + let max_num_attempts = 8; + let mut sbot_is_running = 0; + let mut whoami = None; + while (sbot_is_running == 0) & (num_attempts < max_num_attempts) { + let sbot_res = init_sbot().await; + match sbot_res { + Ok(mut sbot) => { + let sbot_id_res = sbot.whoami().await; + match sbot_id_res { + Ok(sbot_id) => { + sbot_is_running = 1; + whoami = Some(sbot_id) + }, + Err(err) => { + eprintln!("whoami failed: {:?}", err); + } + } + }, + Err(err) => { + eprintln!("failed to connect to sbot: {:?}", err); + } + }; + if sbot_is_running == 0 { + num_attempts += 1; + let two_seconds = time::Duration::from_secs(2); + thread::sleep(two_seconds); + } + }; + whoami.ok_or(PeachConfigError::WaitForSbotError{ message: "Failed to find sbot_id".to_string()}) +} From 29f5ad0e8484b9f4c75fe0380f73b826b942a4f9 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 12 Jul 2022 12:18:39 +0200 Subject: [PATCH 2/5] Wait for sbot is working --- Cargo.lock | 2 +- peach-config/Cargo.toml | 2 +- peach-config/src/main.rs | 4 +++- peach-config/src/wait_for_sbot.rs | 7 ++++--- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ffaa736..539ecef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2200,7 +2200,7 @@ dependencies = [ [[package]] name = "peach-config" -version = "0.1.26" +version = "0.1.27" dependencies = [ "async-std", "clap", diff --git a/peach-config/Cargo.toml b/peach-config/Cargo.toml index a81b605..f81ed62 100644 --- a/peach-config/Cargo.toml +++ b/peach-config/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-config" -version = "0.1.26" +version = "0.1.27" authors = ["Andrew Reid ", "Max Fowler "] edition = "2018" description = "Command line tool for installing, updating and configuring PeachCloud" diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index 96493a6..47e8b43 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -200,7 +200,9 @@ async fn run() { }, PeachConfig::WaitForSbot => { match wait_for_sbot::wait_for_sbot().await { - Ok(_) => {} + Ok(sbot_id) => { + println!("connected with sbot and found sbot_id: {:?}", sbot_id) + } Err(err) => { error!( "peach-config did not successfully connect to sbot: {}", diff --git a/peach-config/src/wait_for_sbot.rs b/peach-config/src/wait_for_sbot.rs index d291e95..239e81a 100644 --- a/peach-config/src/wait_for_sbot.rs +++ b/peach-config/src/wait_for_sbot.rs @@ -5,11 +5,11 @@ use peach_lib::sbot::init_sbot; /// Utility function to wait for a successful whoami call with sbot /// After each attempt to call whoami it waits 2 seconds, -/// and if after max_num_attempts (8) there is no successful whoami call +/// and if after max_num_attempts (10) there is no successful whoami call /// it returns an Error. Otherwise it returns Ok(sbot_id). pub async fn wait_for_sbot() -> Result { let mut num_attempts = 0; - let max_num_attempts = 8; + let max_num_attempts = 10; let mut sbot_is_running = 0; let mut whoami = None; while (sbot_is_running == 0) & (num_attempts < max_num_attempts) { @@ -32,10 +32,11 @@ pub async fn wait_for_sbot() -> Result { } }; if sbot_is_running == 0 { + println!("trying again {:?}", num_attempts); num_attempts += 1; let two_seconds = time::Duration::from_secs(2); thread::sleep(two_seconds); } }; - whoami.ok_or(PeachConfigError::WaitForSbotError{ message: "Failed to find sbot_id".to_string()}) + whoami.ok_or(PeachConfigError::WaitForSbotError{ message: "Failed to find sbot_id after 10 attempts".to_string()}) } From fc50bb5ee5e0363094bd34a5f7c90529ecca8af1 Mon Sep 17 00:00:00 2001 From: notplants Date: Tue, 12 Jul 2022 12:29:47 +0200 Subject: [PATCH 3/5] Cargo fmt --- peach-config/src/main.rs | 21 ++++++++------------- peach-config/src/wait_for_sbot.rs | 10 ++++++---- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/peach-config/src/main.rs b/peach-config/src/main.rs index 47e8b43..754083e 100644 --- a/peach-config/src/main.rs +++ b/peach-config/src/main.rs @@ -197,20 +197,15 @@ async fn run() { ) } } - }, - PeachConfig::WaitForSbot => { - match wait_for_sbot::wait_for_sbot().await { - Ok(sbot_id) => { - println!("connected with sbot and found sbot_id: {:?}", sbot_id) - } - Err(err) => { - error!( - "peach-config did not successfully connect to sbot: {}", - err - ) - } - } } + PeachConfig::WaitForSbot => match wait_for_sbot::wait_for_sbot().await { + Ok(sbot_id) => { + println!("connected with sbot and found sbot_id: {:?}", sbot_id) + } + Err(err) => { + error!("peach-config did not successfully connect to sbot: {}", err) + } + }, } } } diff --git a/peach-config/src/wait_for_sbot.rs b/peach-config/src/wait_for_sbot.rs index 239e81a..602329e 100644 --- a/peach-config/src/wait_for_sbot.rs +++ b/peach-config/src/wait_for_sbot.rs @@ -21,12 +21,12 @@ pub async fn wait_for_sbot() -> Result { Ok(sbot_id) => { sbot_is_running = 1; whoami = Some(sbot_id) - }, + } Err(err) => { eprintln!("whoami failed: {:?}", err); } } - }, + } Err(err) => { eprintln!("failed to connect to sbot: {:?}", err); } @@ -37,6 +37,8 @@ pub async fn wait_for_sbot() -> Result { let two_seconds = time::Duration::from_secs(2); thread::sleep(two_seconds); } - }; - whoami.ok_or(PeachConfigError::WaitForSbotError{ message: "Failed to find sbot_id after 10 attempts".to_string()}) + } + whoami.ok_or(PeachConfigError::WaitForSbotError { + message: "Failed to find sbot_id after 10 attempts".to_string(), + }) } From bc0c0fca7fcb1873f39fb9d7bb7ba044d2cbcf62 Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 15 Jul 2022 11:35:28 +0200 Subject: [PATCH 4/5] Sequential match statements --- peach-config/src/wait_for_sbot.rs | 52 ++++++++++++++++++------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/peach-config/src/wait_for_sbot.rs b/peach-config/src/wait_for_sbot.rs index 602329e..6a1d2c1 100644 --- a/peach-config/src/wait_for_sbot.rs +++ b/peach-config/src/wait_for_sbot.rs @@ -3,42 +3,50 @@ use std::{thread, time}; use crate::error::PeachConfigError; use peach_lib::sbot::init_sbot; +static MAX_NUM_ATTEMPTS: u8 = 10; + /// Utility function to wait for a successful whoami call with sbot /// After each attempt to call whoami it waits 2 seconds, -/// and if after max_num_attempts (10) there is no successful whoami call +/// and if after MAX_NUM_ATTEMPTS (10) there is no successful whoami call /// it returns an Error. Otherwise it returns Ok(sbot_id). pub async fn wait_for_sbot() -> Result { let mut num_attempts = 0; - let max_num_attempts = 10; - let mut sbot_is_running = 0; let mut whoami = None; - while (sbot_is_running == 0) & (num_attempts < max_num_attempts) { + + while num_attempts < MAX_NUM_ATTEMPTS { + let mut sbot = None; + let sbot_res = init_sbot().await; match sbot_res { - Ok(mut sbot) => { - let sbot_id_res = sbot.whoami().await; - match sbot_id_res { - Ok(sbot_id) => { - sbot_is_running = 1; - whoami = Some(sbot_id) - } - Err(err) => { - eprintln!("whoami failed: {:?}", err); - } - } + Ok(sbot_instance) => { + sbot = Some(sbot_instance); } Err(err) => { eprintln!("failed to connect to sbot: {:?}", err); } - }; - if sbot_is_running == 0 { - println!("trying again {:?}", num_attempts); - num_attempts += 1; - let two_seconds = time::Duration::from_secs(2); - thread::sleep(two_seconds); } + + if sbot.is_some() { + let sbot_id_res = sbot.unwrap().whoami().await; + match sbot_id_res { + Ok(sbot_id) => { + whoami = Some(sbot_id); + break; + } + Err(err) => { + eprintln!("whoami failed: {:?}", err); + } + } + } + + println!("trying to connect to sbot again {:?}", num_attempts); + num_attempts += 1; + + let sleep_duration = time::Duration::from_secs(2); + thread::sleep(sleep_duration); } + whoami.ok_or(PeachConfigError::WaitForSbotError { message: "Failed to find sbot_id after 10 attempts".to_string(), }) -} +} \ No newline at end of file From 03ac8907930c4e3568c6971d3a68fbc10a67061e Mon Sep 17 00:00:00 2001 From: notplants Date: Fri, 15 Jul 2022 11:37:05 +0200 Subject: [PATCH 5/5] Cargo fmt --- peach-config/src/wait_for_sbot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peach-config/src/wait_for_sbot.rs b/peach-config/src/wait_for_sbot.rs index 6a1d2c1..9564261 100644 --- a/peach-config/src/wait_for_sbot.rs +++ b/peach-config/src/wait_for_sbot.rs @@ -49,4 +49,4 @@ pub async fn wait_for_sbot() -> Result { whoami.ok_or(PeachConfigError::WaitForSbotError { message: "Failed to find sbot_id after 10 attempts".to_string(), }) -} \ No newline at end of file +}