Sequential match statements
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
notplants 2022-07-15 11:35:28 +02:00
parent fc50bb5ee5
commit bc0c0fca7f
1 changed files with 30 additions and 22 deletions

View File

@ -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<String, PeachConfigError> {
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(),
})
}
}