2022-07-12 09:51:49 +00:00
|
|
|
use std::{thread, time};
|
|
|
|
|
|
|
|
use crate::error::PeachConfigError;
|
|
|
|
use peach_lib::sbot::init_sbot;
|
|
|
|
|
2022-07-15 09:35:28 +00:00
|
|
|
static MAX_NUM_ATTEMPTS: u8 = 10;
|
|
|
|
|
2022-07-12 09:51:49 +00:00
|
|
|
/// Utility function to wait for a successful whoami call with sbot
|
|
|
|
/// After each attempt to call whoami it waits 2 seconds,
|
2022-07-15 09:35:28 +00:00
|
|
|
/// and if after MAX_NUM_ATTEMPTS (10) there is no successful whoami call
|
2022-07-12 09:51:49 +00:00
|
|
|
/// 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 mut whoami = None;
|
2022-07-15 09:35:28 +00:00
|
|
|
|
|
|
|
while num_attempts < MAX_NUM_ATTEMPTS {
|
|
|
|
let mut sbot = None;
|
|
|
|
|
2022-07-12 09:51:49 +00:00
|
|
|
let sbot_res = init_sbot().await;
|
|
|
|
match sbot_res {
|
2022-07-15 09:35:28 +00:00
|
|
|
Ok(sbot_instance) => {
|
|
|
|
sbot = Some(sbot_instance);
|
2022-07-12 10:29:47 +00:00
|
|
|
}
|
2022-07-12 09:51:49 +00:00
|
|
|
Err(err) => {
|
|
|
|
eprintln!("failed to connect to sbot: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
2022-07-15 09:35:28 +00:00
|
|
|
|
|
|
|
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);
|
2022-07-12 10:29:47 +00:00
|
|
|
}
|
2022-07-15 09:35:28 +00:00
|
|
|
|
2022-07-12 10:29:47 +00:00
|
|
|
whoami.ok_or(PeachConfigError::WaitForSbotError {
|
|
|
|
message: "Failed to find sbot_id after 10 attempts".to_string(),
|
|
|
|
})
|
2022-07-15 09:37:05 +00:00
|
|
|
}
|