👕 refactor: move the webhook task into run_relay

main
(on company time) 2022-12-16 09:57:38 -06:00
parent b53748b2c4
commit 9ab3b42e32
3 changed files with 42 additions and 28 deletions

View File

@ -144,6 +144,7 @@ pub mod file {
pub news_url: Option <String>,
pub hide_audit_log: Option <bool>,
pub webhook_url: Option <String>,
}
}
@ -158,6 +159,7 @@ pub struct Config {
pub scraper_keys: HashMap <String, ScraperKey>,
pub news_url: Option <String>,
pub hide_audit_log: bool,
pub webhook_url: Option <String>,
}
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 <file::Config> for Config {
scraper_keys,
news_url: f.news_url,
hide_audit_log: f.hide_audit_log.unwrap_or (false),
webhook_url: f.webhook_url,
})
}
}

View File

@ -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 (())
}

View File

@ -48,33 +48,8 @@ async fn main () -> Result <(), Box <dyn Error>> {
}
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 ();