From a1dc28f25e653618ff8de98675b584b71709a1bd Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 6 Jul 2020 13:22:02 -0400 Subject: [PATCH] infinitely retry connections --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 37 +++++++++++++++++++++++-------------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 09a2778..5c215b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1511,7 +1511,7 @@ checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" [[package]] name = "tron" -version = "0.2.5" +version = "0.2.6" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index f393b12..0491af0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tron" -version = "0.2.5" +version = "0.2.6" authors = ["Christine Dodrill "] edition = "2018" repository = "https://tulpa.dev/cadey/tron" diff --git a/src/main.rs b/src/main.rs index 314ea93..e6ef753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,14 +50,14 @@ impl fmt::Display for Hit { } // TODO(Xe): make this an actual struct for the love of god. -struct Rules(Vec, Config, furbooru::Client); +struct Rules<'a>(&'a Vec, Config, furbooru::Client); -impl Rules { +impl<'a> Rules<'a> { fn check(&self, text: &String) -> Option> { let mut result: Vec = vec![]; let mut found = false; - for rule in &self.0 { + for rule in self.0 { if rule.regex.is_match(text) { log::debug!("{:?} matches {}", text, rule.raw); found = true; @@ -83,7 +83,7 @@ impl Rules { } #[async_trait] -impl FirehoseAdaptor for Rules { +impl<'a> FirehoseAdaptor for Rules<'a> { async fn image_created(&self, img: Image) -> Result<()> { let url = format!("https://furbooru.org/{}", img.id); log::debug!("got image {}", url); @@ -242,15 +242,24 @@ async fn main() -> Result<()> { cfg.furbooru_api_key.clone(), )?; log::info!("listening on the firehose"); - cli.firehose(Rules( - compiled_rules, - cfg.clone(), - Client::new( - user_agent(cfg.bot_owner_furbooru_account.clone()), - cfg.furbooru_api_key.clone(), - )?, - )) - .await?; - Ok(()) + loop { + if let Err(why) = cli + .firehose(Rules( + &compiled_rules, + cfg.clone(), + Client::new( + user_agent(cfg.bot_owner_furbooru_account.clone()), + cfg.furbooru_api_key.clone(), + )?, + )) + .await + { + log::error!("firehose error: {:#?}", why); + discord_webhook::execute( + cfg.discord_webhook_url.clone(), + Body::new(format!("firehose error:\n```\n{:#?}\n```", why)) + ).await?; + } + } }