|
| 1 | +//! Job that runs a task on intervals to update peers' activity metrics. |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use chrono::Utc; |
| 5 | +use tokio::task::JoinHandle; |
| 6 | +use torrust_tracker_clock::clock::Time; |
| 7 | +use torrust_tracker_metrics::label::LabelSet; |
| 8 | +use torrust_tracker_metrics::metric_name; |
| 9 | +use torrust_tracker_primitives::DurationSinceUnixEpoch; |
| 10 | +use tracing::instrument; |
| 11 | + |
| 12 | +use super::repository::Repository; |
| 13 | +use crate::statistics::{TORRENT_REPOSITORY_PEERS_INACTIVE_TOTAL, TORRENT_REPOSITORY_TORRENTS_INACTIVE_TOTAL}; |
| 14 | +use crate::{CurrentClock, Swarms}; |
| 15 | + |
| 16 | +#[must_use] |
| 17 | +#[instrument(skip(swarms, stats_repository))] |
| 18 | +pub fn start_job( |
| 19 | + swarms: &Arc<Swarms>, |
| 20 | + stats_repository: &Arc<Repository>, |
| 21 | + inactivity_cutoff: DurationSinceUnixEpoch, |
| 22 | +) -> JoinHandle<()> { |
| 23 | + let weak_swarms = std::sync::Arc::downgrade(swarms); |
| 24 | + let weak_stats_repository = std::sync::Arc::downgrade(stats_repository); |
| 25 | + |
| 26 | + let interval_in_secs = 15; // todo: make this configurable |
| 27 | + |
| 28 | + tokio::spawn(async move { |
| 29 | + let interval = std::time::Duration::from_secs(interval_in_secs); |
| 30 | + let mut interval = tokio::time::interval(interval); |
| 31 | + interval.tick().await; |
| 32 | + |
| 33 | + loop { |
| 34 | + tokio::select! { |
| 35 | + _ = tokio::signal::ctrl_c() => { |
| 36 | + tracing::info!("Stopping peers activity metrics update job (ctrl-c signal received) ..."); |
| 37 | + break; |
| 38 | + } |
| 39 | + _ = interval.tick() => { |
| 40 | + if let (Some(swarms), Some(stats_repository)) = (weak_swarms.upgrade(), weak_stats_repository.upgrade()) { |
| 41 | + update_activity_metrics(interval_in_secs, &swarms, &stats_repository, inactivity_cutoff).await; |
| 42 | + } else { |
| 43 | + tracing::info!("Stopping peers activity metrics update job (can't upgrade weak pointers) ..."); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +async fn update_activity_metrics( |
| 53 | + interval_in_secs: u64, |
| 54 | + swarms: &Arc<Swarms>, |
| 55 | + stats_repository: &Arc<Repository>, |
| 56 | + inactivity_cutoff: DurationSinceUnixEpoch, |
| 57 | +) { |
| 58 | + let start_time = Utc::now().time(); |
| 59 | + |
| 60 | + tracing::debug!( |
| 61 | + "Updating peers and torrents activity metrics (executed every {} secs) ...", |
| 62 | + interval_in_secs |
| 63 | + ); |
| 64 | + |
| 65 | + let activity_metadata = swarms.get_activity_metadata(inactivity_cutoff).await; |
| 66 | + |
| 67 | + activity_metadata.log(); |
| 68 | + |
| 69 | + update_inactive_peers_total(stats_repository, activity_metadata.inactive_peers_total).await; |
| 70 | + update_inactive_torrents_total(stats_repository, activity_metadata.inactive_torrents_total).await; |
| 71 | + |
| 72 | + tracing::debug!( |
| 73 | + "Peers and torrents activity metrics updated in {} ms", |
| 74 | + (Utc::now().time() - start_time).num_milliseconds() |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +async fn update_inactive_peers_total(stats_repository: &Arc<Repository>, inactive_peers_total: usize) { |
| 79 | + #[allow(clippy::cast_precision_loss)] |
| 80 | + let inactive_peers_total = inactive_peers_total as f64; |
| 81 | + |
| 82 | + let _unused = stats_repository |
| 83 | + .set_gauge( |
| 84 | + &metric_name!(TORRENT_REPOSITORY_PEERS_INACTIVE_TOTAL), |
| 85 | + &LabelSet::default(), |
| 86 | + inactive_peers_total, |
| 87 | + CurrentClock::now(), |
| 88 | + ) |
| 89 | + .await; |
| 90 | +} |
| 91 | + |
| 92 | +async fn update_inactive_torrents_total(stats_repository: &Arc<Repository>, inactive_torrents_total: usize) { |
| 93 | + #[allow(clippy::cast_precision_loss)] |
| 94 | + let inactive_torrents_total = inactive_torrents_total as f64; |
| 95 | + |
| 96 | + let _unused = stats_repository |
| 97 | + .set_gauge( |
| 98 | + &metric_name!(TORRENT_REPOSITORY_TORRENTS_INACTIVE_TOTAL), |
| 99 | + &LabelSet::default(), |
| 100 | + inactive_torrents_total, |
| 101 | + CurrentClock::now(), |
| 102 | + ) |
| 103 | + .await; |
| 104 | +} |
0 commit comments