Skip to content

Commit

Permalink
Fix: retry init app connection (#197)
Browse files Browse the repository at this point in the history
This adds a catch case for whenever the identified app connection is
down or unable to be established. Whenever that happens this attempts to
create a new app interface connection a single time, and if that fails,
then it throws the ws error as before.
  • Loading branch information
JettTech authored and Matthew Geddes committed Aug 19, 2024
1 parent 256a8c4 commit f1f0685
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/hpos_connect_hc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ sodoken = { workspace = true }
url2 = "0.0.6"
holofuel_types = { workspace = true }
chrono = "0.4.19"
const_env = "0.1"
const_env = "0.1"
log = "0.4.17"
34 changes: 26 additions & 8 deletions crates/hpos_connect_hc/src/app_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,32 @@ impl AppConnection {
keystore: MetaLairClient,
app_id: String,
) -> Result<Self> {
let attached_app_interfaces = admin_ws
.list_app_interfaces()
.await
.context("failed to start fetch app interfaces during app connection setup")?;
match Self::inner_connect(admin_ws, keystore.clone(), app_id.clone(), false).await {
Ok(c) => Ok(c),
Err(e) => {
log::warn!("Failed to connnect to existing app websocket for core happ id: {:?}... creating and connecting to a new one. Error: {:#?}", app_id, e);
Self::inner_connect(admin_ws, keystore, app_id, true).await
}
}
}
async fn inner_connect(
admin_ws: &mut AdminWebsocket,
keystore: MetaLairClient,
app_id: String,
force_new_interface: bool,
) -> Result<Self> {
let app_interface = if !force_new_interface {
let attached_app_interfaces = admin_ws
.list_app_interfaces()
.await
.context("failed to start fetch app interfaces during app connection setup")?;

let app_interface = attached_app_interfaces.into_iter().find(|a| {
a.installed_app_id.is_some() && a.installed_app_id.to_owned().unwrap() == app_id
});
attached_app_interfaces.into_iter().find(|a| {
a.installed_app_id.is_some() && a.installed_app_id.to_owned().unwrap() == app_id
})
} else {
None
};

let app_port = match app_interface {
Some(a) => a.port,
Expand Down Expand Up @@ -135,7 +153,7 @@ impl AppConnection {
.get(&role_name)
.ok_or(anyhow!("unable to find cells for RoleName {}", &role_name))?;
let cells = app_cells
.into_iter()
.iter()
.filter_map(|cell_info| match cell_info {
CellInfo::Cloned(cloned_cell) => Some(cloned_cell.clone()),
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/hpos_connect_hc/src/hha_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl CoreAppAgent {

let app = AppConnection::connect(&mut admin_ws, keystore, core_app.id())
.await
.context("failed to connect to holochain's app interface")?;
.context("Failed to connect to holochain's app interface")?;

Ok(Self { app })
}
Expand Down

0 comments on commit f1f0685

Please sign in to comment.