|
| 1 | +//! Cronjob to import tracker torrent data and updating seeders and leechers |
| 2 | +//! info. |
| 3 | +//! |
| 4 | +//! It has two services: |
| 5 | +//! |
| 6 | +//! - The importer which is the cronjob executed at regular intervals. |
| 7 | +//! - The importer API. |
| 8 | +//! |
| 9 | +//! The cronjob sends a heartbeat signal to the API each time it is executed. |
| 10 | +//! The last heartbeat signal time is used to determine whether the cronjob was |
| 11 | +//! executed successfully or not. The API has a `health_check` endpoint which is |
| 12 | +//! used when the application is running in containers. |
| 13 | +use std::sync::{Arc, Mutex}; |
| 14 | + |
| 15 | +use axum::extract::State; |
| 16 | +use axum::routing::{get, post}; |
| 17 | +use axum::{Json, Router}; |
| 18 | +use chrono::{DateTime, Utc}; |
| 19 | +use log::{error, info}; |
| 20 | +use serde_json::{json, Value}; |
| 21 | +use tokio::task::JoinHandle; |
| 22 | + |
| 23 | +use crate::tracker::statistics_importer::StatisticsImporter; |
| 24 | + |
| 25 | +const IMPORTER_API_IP: &str = "127.0.0.1"; |
| 26 | + |
| 27 | +#[derive(Clone)] |
| 28 | +struct ImporterState { |
| 29 | + /// Shared variable to store the timestamp of the last heartbeat sent |
| 30 | + /// by the cronjob. |
| 31 | + pub last_heartbeat: Arc<Mutex<DateTime<Utc>>>, |
| 32 | + /// Interval between importation executions |
| 33 | + pub torrent_info_update_interval: u64, |
| 34 | +} |
| 35 | + |
| 36 | +pub fn start( |
| 37 | + importer_port: u16, |
| 38 | + torrent_info_update_interval: u64, |
| 39 | + tracker_statistics_importer: &Arc<StatisticsImporter>, |
| 40 | +) -> JoinHandle<()> { |
| 41 | + let weak_tracker_statistics_importer = Arc::downgrade(tracker_statistics_importer); |
| 42 | + |
| 43 | + tokio::spawn(async move { |
| 44 | + info!("Tracker statistics importer launcher started"); |
| 45 | + |
| 46 | + // Start the Importer API |
| 47 | + |
| 48 | + let _importer_api_handle = tokio::spawn(async move { |
| 49 | + let import_state = Arc::new(ImporterState { |
| 50 | + last_heartbeat: Arc::new(Mutex::new(Utc::now())), |
| 51 | + torrent_info_update_interval, |
| 52 | + }); |
| 53 | + |
| 54 | + let app = Router::new() |
| 55 | + .route("/", get(|| async { Json(json!({})) })) |
| 56 | + .route("/health_check", get(health_check_handler)) |
| 57 | + .with_state(import_state.clone()) |
| 58 | + .route("/heartbeat", post(heartbeat_handler)) |
| 59 | + .with_state(import_state); |
| 60 | + |
| 61 | + let addr = format!("{IMPORTER_API_IP}:{importer_port}"); |
| 62 | + |
| 63 | + info!("Tracker statistics importer API server listening on http://{}", addr); |
| 64 | + |
| 65 | + axum::Server::bind(&addr.parse().unwrap()) |
| 66 | + .serve(app.into_make_service()) |
| 67 | + .await |
| 68 | + .unwrap(); |
| 69 | + }); |
| 70 | + |
| 71 | + // Start the Importer cronjob |
| 72 | + |
| 73 | + info!("Tracker statistics importer cronjob starting ..."); |
| 74 | + |
| 75 | + let interval = std::time::Duration::from_secs(torrent_info_update_interval); |
| 76 | + let mut interval = tokio::time::interval(interval); |
| 77 | + |
| 78 | + interval.tick().await; // first tick is immediate... |
| 79 | + |
| 80 | + loop { |
| 81 | + interval.tick().await; |
| 82 | + |
| 83 | + info!("Running tracker statistics importer ..."); |
| 84 | + |
| 85 | + if let Err(e) = send_heartbeat(importer_port).await { |
| 86 | + error!("Failed to send heartbeat from importer cronjob: {}", e); |
| 87 | + } |
| 88 | + |
| 89 | + if let Some(tracker) = weak_tracker_statistics_importer.upgrade() { |
| 90 | + drop(tracker.import_all_torrents_statistics().await); |
| 91 | + } else { |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +/// Endpoint for container health check. |
| 99 | +async fn health_check_handler(State(state): State<Arc<ImporterState>>) -> Json<Value> { |
| 100 | + let margin_in_seconds = 10; |
| 101 | + let now = Utc::now(); |
| 102 | + let last_heartbeat = state.last_heartbeat.lock().unwrap(); |
| 103 | + |
| 104 | + if now.signed_duration_since(*last_heartbeat).num_seconds() |
| 105 | + <= (state.torrent_info_update_interval + margin_in_seconds).try_into().unwrap() |
| 106 | + { |
| 107 | + Json(json!({ "status": "Ok" })) |
| 108 | + } else { |
| 109 | + Json(json!({ "status": "Error" })) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// The tracker statistics importer cronjob sends a heartbeat on each execution |
| 114 | +/// to inform that it's alive. This endpoint handles receiving that signal. |
| 115 | +async fn heartbeat_handler(State(state): State<Arc<ImporterState>>) -> Json<Value> { |
| 116 | + let now = Utc::now(); |
| 117 | + let mut last_heartbeat = state.last_heartbeat.lock().unwrap(); |
| 118 | + *last_heartbeat = now; |
| 119 | + Json(json!({ "status": "Heartbeat received" })) |
| 120 | +} |
| 121 | + |
| 122 | +/// Send a heartbeat from the importer cronjob to the importer API. |
| 123 | +async fn send_heartbeat(importer_port: u16) -> Result<(), reqwest::Error> { |
| 124 | + let client = reqwest::Client::new(); |
| 125 | + let url = format!("http://{IMPORTER_API_IP}:{importer_port}/heartbeat"); |
| 126 | + |
| 127 | + client.post(url).send().await?; |
| 128 | + |
| 129 | + Ok(()) |
| 130 | +} |
0 commit comments