-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make listener not fail on decode errors and db errors
- Loading branch information
1 parent
602adca
commit d164d7a
Showing
6 changed files
with
136 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,5 +72,5 @@ message Envelope { | |
bytes cipher = 14; | ||
} | ||
|
||
optional string relays = 17; | ||
repeated string relays = 17; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,109 @@ | ||
mod events; | ||
use std::{collections::LinkedList, time::Duration}; | ||
|
||
pub use events::Listener; | ||
use crate::{cache::Cache, tfchain::tfchain, twin::Twin}; | ||
use anyhow::Result; | ||
use futures::StreamExt; | ||
use log; | ||
use subxt::{OnlineClient, PolkadotConfig}; | ||
|
||
#[derive(Clone)] | ||
pub struct Listener<C> | ||
where | ||
C: Cache<Twin>, | ||
{ | ||
cache: C, | ||
api: OnlineClient<PolkadotConfig>, | ||
substrate_urls: LinkedList<String>, | ||
} | ||
|
||
impl<C> Listener<C> | ||
where | ||
C: Cache<Twin> + Clone, | ||
{ | ||
pub async fn new(substrate_urls: Vec<String>, cache: C) -> Result<Self> { | ||
let mut urls = LinkedList::from_iter(substrate_urls); | ||
|
||
let api = Self::connect(&mut urls).await?; | ||
|
||
cache.flush().await?; | ||
Ok(Listener { | ||
api, | ||
cache, | ||
substrate_urls: urls, | ||
}) | ||
} | ||
|
||
async fn connect(urls: &mut LinkedList<String>) -> Result<OnlineClient<PolkadotConfig>> { | ||
let trials = urls.len() * 2; | ||
for _ in 0..trials { | ||
let url = match urls.front() { | ||
Some(url) => url, | ||
None => anyhow::bail!("substrate urls list is empty"), | ||
}; | ||
|
||
match OnlineClient::<PolkadotConfig>::from_url(url).await { | ||
Ok(client) => return Ok(client), | ||
Err(err) => { | ||
log::error!( | ||
"failed to create substrate client with url \"{}\": {}", | ||
url, | ||
err | ||
); | ||
} | ||
} | ||
|
||
if let Some(front) = urls.pop_front() { | ||
urls.push_back(front); | ||
} | ||
} | ||
|
||
anyhow::bail!("failed to connect to substrate using the provided urls") | ||
} | ||
|
||
pub async fn listen(&mut self) -> Result<()> { | ||
loop { | ||
// always flush in case some blocks were finalized before reconnecting | ||
if let Err(err) = self.cache.flush().await { | ||
log::error!("failed to flush redis cache {}", err); | ||
tokio::time::sleep(Duration::from_millis(500)).await; | ||
continue; | ||
} | ||
match self.handle_events().await { | ||
Err(err) => { | ||
if let Some(subxt::Error::Rpc(_)) = err.downcast_ref::<subxt::Error>() { | ||
self.api = Self::connect(&mut self.substrate_urls).await?; | ||
} else { | ||
log::error!("error listening to events {}", err); | ||
} | ||
} | ||
Ok(_) => {} | ||
} | ||
} | ||
} | ||
|
||
async fn handle_events(&self) -> Result<()> { | ||
log::info!("started chain events listener"); | ||
let mut blocks_sub = self.api.blocks().subscribe_finalized().await?; | ||
while let Some(block) = blocks_sub.next().await { | ||
let events = block?.events().await?; | ||
for evt in events.iter() { | ||
let evt = match evt { | ||
Err(err) => { | ||
log::error!("failed to decode event {}", err); | ||
continue; | ||
} | ||
Ok(e) => e, | ||
}; | ||
if let Ok(Some(twin)) = evt.as_event::<tfchain::tfgrid_module::events::TwinStored>() | ||
{ | ||
self.cache.set(twin.0.id, twin.0.into()).await?; | ||
} else if let Ok(Some(twin)) = | ||
evt.as_event::<tfchain::tfgrid_module::events::TwinUpdated>() | ||
{ | ||
self.cache.set(twin.0.id, twin.0.into()).await?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters