Skip to main content

max / makenotwork

2.2 KB · 70 lines History Blame Raw
1 //! Background WHOIS/domain-expiry check task.
2
3 use tokio::task::JoinHandle;
4 use tracing::info;
5
6 use pom::alerts::Alerter;
7 use pom::checks::whois;
8 use pom::config::Config;
9 use pom::db;
10
11 use super::{CheckInterval, configured_targets};
12
13 pub(crate) fn spawn_whois_tasks(
14 config: &Config,
15 pool: &sqlx::SqlitePool,
16 cancel: &tokio_util::sync::CancellationToken,
17 alerter: Option<&Alerter>,
18 ) -> Vec<JoinHandle<()>> {
19 let whois_interval_secs = config.serve.whois_check_interval_secs;
20 let mut handles = Vec::new();
21
22 for (name, target_config) in configured_targets(config) {
23 let Some(whois_config) = target_config.whois else {
24 continue;
25 };
26 let label = target_config.label.clone();
27 let pool = pool.clone();
28 let alerter = alerter.cloned();
29 let cancel = cancel.clone();
30 let warn_days = whois_config.warn_days;
31
32 info!(
33 "{name}: WHOIS check every {whois_interval_secs}s (domain={})",
34 whois_config.domain
35 );
36
37 handles.push(tokio::spawn(async move {
38 let mut ticks = CheckInterval::new(whois_interval_secs, cancel);
39
40 while ticks.next().await {
41 let result = whois::check_whois(&name, &whois_config).await;
42 info!(
43 "{}: WHOIS {}, {:?} days remaining",
44 name, whois_config.domain, result.days_remaining
45 );
46
47 if let Err(e) = db::insert_whois_check(&pool, &result).await {
48 tracing::error!("{name}: failed to store WHOIS check: {e}");
49 }
50
51 if let Some(ref alerter) = alerter {
52 if let Some(ref error) = result.error {
53 alerter
54 .send_whois_error_alert(&name, &label, &whois_config.domain, error)
55 .await;
56 } else if let Some(days) = result.days_remaining
57 && days <= warn_days as i64
58 {
59 alerter
60 .send_whois_expiry_alert(&name, &label, &whois_config.domain, days)
61 .await;
62 }
63 }
64 }
65 }));
66 }
67
68 handles
69 }
70