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