Skip to main content

max / pom

2.3 KB · 63 lines History Blame Raw
1 use tokio::task::JoinHandle;
2 use tracing::info;
3
4 use pom::alerts::Alerter;
5 use pom::checks::whois;
6 use pom::config::Config;
7 use pom::db;
8
9 pub(crate) fn spawn_whois_tasks(
10 config: &Config,
11 pool: &sqlx::SqlitePool,
12 cancel: &tokio_util::sync::CancellationToken,
13 alerter: &Option<Alerter>,
14 ) -> Vec<JoinHandle<()>> {
15 let whois_interval_secs = config.serve.tls_check_interval_secs;
16 let mut handles = Vec::new();
17
18 for name in config.target_names() {
19 let target_config = config.get_target(&name).unwrap().clone();
20 let Some(whois_config) = target_config.whois else { continue };
21 let label = target_config.label.clone();
22 let pool = pool.clone();
23 let alerter = alerter.clone();
24 let cancel = cancel.clone();
25 let warn_days = whois_config.warn_days;
26
27 info!("{name}: WHOIS check every {whois_interval_secs}s (domain={})", whois_config.domain);
28
29 handles.push(tokio::spawn(async move {
30 let mut interval = tokio::time::interval(
31 std::time::Duration::from_secs(whois_interval_secs),
32 );
33 interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
34
35 interval.tick().await; // consume immediate first tick
36 loop {
37 tokio::select! {
38 _ = cancel.cancelled() => break,
39 _ = interval.tick() => {}
40 }
41 let result = whois::check_whois(&name, &whois_config).await;
42 info!("{}: WHOIS {} — {:?} days remaining", name, whois_config.domain, result.days_remaining);
43
44 if let Err(e) = db::insert_whois_check(&pool, &result).await {
45 tracing::error!("{name}: failed to store WHOIS check: {e}");
46 }
47
48 if let Some(ref alerter) = alerter {
49 if let Some(ref error) = result.error {
50 alerter.send_whois_error_alert(&name, &label, &whois_config.domain, error).await;
51 } else if let Some(days) = result.days_remaining
52 && days <= warn_days as i64
53 {
54 alerter.send_whois_expiry_alert(&name, &label, &whois_config.domain, days).await;
55 }
56 }
57 }
58 }));
59 }
60
61 handles
62 }
63