peach-workspace/peach-config/src/wait_for_sbot.rs

43 lines
1.5 KiB
Rust

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 (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) {
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 {
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 after 10 attempts".to_string()})
}