From 9ab3b42e32eb8b6b338d37fbdfc31efa7bd2e8aa Mon Sep 17 00:00:00 2001 From: "(on company time)" <_@_> Date: Fri, 16 Dec 2022 09:57:38 -0600 Subject: [PATCH] :shirt: refactor: move the webhook task into run_relay --- crates/ptth_relay/src/config.rs | 4 ++++ crates/ptth_relay/src/lib.rs | 37 ++++++++++++++++++++++++++++++++- crates/ptth_relay/src/main.rs | 29 ++------------------------ 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/crates/ptth_relay/src/config.rs b/crates/ptth_relay/src/config.rs index 0865ec4..68c2c5b 100644 --- a/crates/ptth_relay/src/config.rs +++ b/crates/ptth_relay/src/config.rs @@ -144,6 +144,7 @@ pub mod file { pub news_url: Option , pub hide_audit_log: Option , + pub webhook_url: Option , } } @@ -158,6 +159,7 @@ pub struct Config { pub scraper_keys: HashMap , pub news_url: Option , pub hide_audit_log: bool, + pub webhook_url: Option , } impl Default for Config { @@ -170,6 +172,7 @@ impl Default for Config { scraper_keys: Default::default (), news_url: None, hide_audit_log: false, + webhook_url: None, } } } @@ -199,6 +202,7 @@ impl TryFrom for Config { scraper_keys, news_url: f.news_url, hide_audit_log: f.hide_audit_log.unwrap_or (false), + webhook_url: f.webhook_url, }) } } diff --git a/crates/ptth_relay/src/lib.rs b/crates/ptth_relay/src/lib.rs index 80512c1..fad5833 100644 --- a/crates/ptth_relay/src/lib.rs +++ b/crates/ptth_relay/src/lib.rs @@ -818,7 +818,7 @@ pub async fn run_relay ( let mut request_rendezvous = state_2.request_rendezvous.lock ().await; request_rendezvous.iter_mut () - .for_each (|(k, v)| { + .for_each (|(_k, v)| { match v { RequestRendezvous::ParkedServer (_) => (), RequestRendezvous::ParkedClients (requests) => requests.retain (|req| req.id.as_str () >= timeout_ulid.as_str ()), @@ -878,6 +878,39 @@ pub async fn run_relay ( state.audit_log.push (AuditEvent::new (AuditData::RelayStart)).await; + let webhook_task = { + let state = state.clone (); + + tokio::spawn (async move { + let client = reqwest::Client::default (); + + let mut interval = tokio::time::interval (std::time::Duration::from_secs (7200)); + interval.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip); + + let mut tick_seq = 0; + + loop { + interval.tick ().await; + + let webhook_url = { + let config = state.config.read ().await; + config.webhook_url.clone () + }; + + if let Some (webhook_url) = webhook_url.as_ref () { + let now = chrono::Utc::now (); + + let j = serde_json::json! ({ + "text": format! ("PTTH relay sent test webhook message {} at {:?}", tick_seq, now), + }).to_string (); + + client.post (webhook_url).body (j).send ().await.ok (); + tick_seq += 1; + } + } + }) + }; + trace! ("Serving relay on {:?}", addr); server.with_graceful_shutdown (async { @@ -910,6 +943,8 @@ pub async fn run_relay ( debug! ("Performed all cleanup"); }).await?; + webhook_task.abort (); + Ok (()) } diff --git a/crates/ptth_relay/src/main.rs b/crates/ptth_relay/src/main.rs index 593446a..2bdd35a 100644 --- a/crates/ptth_relay/src/main.rs +++ b/crates/ptth_relay/src/main.rs @@ -48,33 +48,8 @@ async fn main () -> Result <(), Box > { } let config_path = PathBuf::from ("config/ptth_relay.toml"); - let config = Config::from_file (&config_path).await?; - - tokio::spawn (async { - let webhook_url = std::env::var ("WEBHOOK_URL"); - - let client = reqwest::Client::default (); - - let mut interval = tokio::time::interval (std::time::Duration::from_secs (7200)); - interval.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip); - - let mut tick_seq = 0; - - loop { - interval.tick ().await; - - if let Ok (webhook_url) = webhook_url.as_ref () { - let now = chrono::Utc::now (); - - let j = serde_json::json! ({ - "text": format! ("PTTH relay sent test webhook message {} at {:?}", tick_seq, now), - }).to_string (); - - client.post (webhook_url).body (j).send ().await.ok (); - tick_seq += 1; - } - } - }); + let mut config = Config::from_file (&config_path).await?; + config.webhook_url = std::env::var ("WEBHOOK_URL").ok ().or (config.webhook_url); let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force ();