From b53748b2c4a702a6d74dedcc96b3df1e0de292d6 Mon Sep 17 00:00:00 2001 From: "(on company time)" <_@_> Date: Fri, 16 Dec 2022 09:30:38 -0600 Subject: [PATCH] :heavy_plus_sign: try sending webhooks from the relay --- Cargo.lock | 1 + crates/ptth_relay/Cargo.toml | 5 +++++ crates/ptth_relay/src/main.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e3e6038..99920b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1396,6 +1396,7 @@ dependencies = [ "itertools", "ptth_core", "rand", + "reqwest", "rmp-serde", "rusty_ulid 1.0.0", "serde", diff --git a/crates/ptth_relay/Cargo.toml b/crates/ptth_relay/Cargo.toml index ae7f4c0..c39c4cc 100644 --- a/crates/ptth_relay/Cargo.toml +++ b/crates/ptth_relay/Cargo.toml @@ -37,3 +37,8 @@ tracing-futures = "0.2.4" tracing-subscriber = "0.2.15" ptth_core = { path = "../ptth_core", version = "2.0.0" } + +[dependencies.reqwest] +version = "0.11.13" +default-features = false +features = ["stream", "rustls-tls", "hyper-rustls"] diff --git a/crates/ptth_relay/src/main.rs b/crates/ptth_relay/src/main.rs index 7dc7f4a..593446a 100644 --- a/crates/ptth_relay/src/main.rs +++ b/crates/ptth_relay/src/main.rs @@ -50,6 +50,32 @@ 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 (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force (); forced_shutdown.wrap_server (