Wait for sbot
This commit is contained in:
parent
01138eef35
commit
cb09d6c3e9
|
@ -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]]
|
||||
|
|
|
@ -42,6 +42,8 @@ pub enum PeachConfigError {
|
|||
Golgi { source: GolgiError },
|
||||
#[snafu(display("{}", message))]
|
||||
CmdInputError { message: String },
|
||||
#[snafu(display("{}", message))]
|
||||
WaitForSbotError { message: String },
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for PeachConfigError {
|
||||
|
|
|
@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, PeachConfigError> {
|
||||
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()})
|
||||
}
|
Loading…
Reference in New Issue