Skip to content

Commit

Permalink
hbs: call retry on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
zo-el committed Apr 17, 2024
1 parent 6eb3029 commit 83fc551
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
44 changes: 32 additions & 12 deletions src/hbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use anyhow::Result;
use base64::prelude::*;
use holochain_types::prelude::{holochain_serial, SerializedBytes, Signature, Timestamp};
use hpos_hc_connect::{hpos_agent::get_hpos_config, CoreAppAgent};
use reqwest::Response;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
Expand All @@ -27,22 +28,48 @@ impl HbsClient {
let client = reqwest::Client::builder().build()?;
Ok(Self { client })
}
pub async fn get_hosting_criteria(&self) -> HostingCriteria {
pub async fn get_hosting_criteria(&self) -> Option<HostingCriteria> {
match self.get_access_token().await {
Ok(v) => v,
Err(e) => {
tracing::warn!("Unable to get kyc & jurisdiction: {:?}", e);
tracing::warn!("returning default kyc level 1");
tracing::warn!("returning default jurisdiction of None");
HostingCriteria {
Some(HostingCriteria {
id: None,
jurisdiction: None,
kyc: KycLevel::Level1,
}
})
}
}
}
async fn get_access_token(&self) -> Result<HostingCriteria> {

async fn get_access_token(&self) -> Result<Option<HostingCriteria>> {
let mut retry_counter = 0;
let mut retry = true;
let mut body = String::new();
// retry atleast twice
while retry && (retry_counter < 2) {
let response = self.inner_get_access_token().await?;
tracing::debug!("response received");
body = response.text().await?;
// 504 Gateway Timeout
// here we either need to retry or end the script
if body.contains("error code: 504") {
tracing::warn!("Gateway Timeout. Retrying...");
retry_counter += 1;
} else {
retry = false;
}
}
tracing::debug!("Result: {}", body);
let result: serde_json::Value = serde_json::from_str(&body)?;
let h: HostingCriteria = serde_json::from_value(result)?;
tracing::debug!("HostingCriteria: {:?}", h);
Ok(Some(h))
}

async fn inner_get_access_token(&self) -> Result<Response> {
let config: hpos_config_core::Config = get_hpos_config()?;

let email = match config {
Expand Down Expand Up @@ -94,14 +121,7 @@ impl HbsClient {
.headers(headers)
.json(&json);

let response = request.send().await?;
tracing::debug!("response received");
let body = response.text().await?;
tracing::debug!("Result: {}", body);
let result: serde_json::Value = serde_json::from_str(&body)?;
let h: HostingCriteria = serde_json::from_value(result)?;
tracing::debug!("HostingCriteria: {:?}", h);
Ok(h)
Ok(request.send().await?)
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use host_zome_calls::get_all_published_hosted_happs;
mod install_app;
use install_app::install_holo_hosted_happs;
mod uninstall_apps;
use tracing::{debug, info};
use tracing::{debug, error, info};
use uninstall_apps::uninstall_ineligible_happs;
mod suspend_happs;
use suspend_happs::suspend_unpaid_happs;
Expand All @@ -26,7 +26,13 @@ use crate::host_zome_calls::{get_pending_transactions, CoreAppClient};
pub async fn run(core_happ: &config::Happ, config: &config::Config) -> Result<()> {
info!("Activating holo hosted apps");
let hbs_connect = HbsClient::connect()?;
let hosting_criteria = hbs_connect.get_hosting_criteria().await;
let hosting_criteria = match hbs_connect.get_hosting_criteria().await {
Some(v) => v,
None => {
error!("Unable to get hosting criteria from HBS. Exiting...");
return Err(anyhow::anyhow!("Unable to get hosting criteria"));
}
};
let kyc_level = hosting_criteria.kyc;
debug!("Got kyc level {:?}", &kyc_level);
let jurisdiction = hosting_criteria.jurisdiction;
Expand Down

0 comments on commit 83fc551

Please sign in to comment.