From 2a2d625d62b8c67c30b444c23579acaa609b51d9 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 17 Nov 2023 15:30:43 +0000 Subject: [PATCH] fix: [#384] use free por for importer API for isolated test envs --- share/default/config/index.container.mysql.toml | 1 + share/default/config/index.container.sqlite3.toml | 1 + .../default/config/index.development.sqlite3.toml | 1 + src/app.rs | 13 ++++++++++--- src/config.rs | 3 +++ src/console/tracker_statistics_importer.rs | 15 +++++++++------ src/lib.rs | 1 + src/web/api/v1/contexts/settings/mod.rs | 1 + tests/common/contexts/settings/mod.rs | 2 ++ tests/environments/isolated.rs | 3 +++ 10 files changed, 32 insertions(+), 9 deletions(-) diff --git a/share/default/config/index.container.mysql.toml b/share/default/config/index.container.mysql.toml index 1999c4a1..c7b4c33c 100644 --- a/share/default/config/index.container.mysql.toml +++ b/share/default/config/index.container.mysql.toml @@ -48,3 +48,4 @@ max_torrent_page_size = 30 [tracker_statistics_importer] torrent_info_update_interval = 3600 +port = 3002 \ No newline at end of file diff --git a/share/default/config/index.container.sqlite3.toml b/share/default/config/index.container.sqlite3.toml index c0cb6002..ac1dd71d 100644 --- a/share/default/config/index.container.sqlite3.toml +++ b/share/default/config/index.container.sqlite3.toml @@ -48,3 +48,4 @@ max_torrent_page_size = 30 [tracker_statistics_importer] torrent_info_update_interval = 3600 +port = 3002 \ No newline at end of file diff --git a/share/default/config/index.development.sqlite3.toml b/share/default/config/index.development.sqlite3.toml index 06f89a3c..669979af 100644 --- a/share/default/config/index.development.sqlite3.toml +++ b/share/default/config/index.development.sqlite3.toml @@ -44,3 +44,4 @@ max_torrent_page_size = 30 [tracker_statistics_importer] torrent_info_update_interval = 3600 +port = 3002 \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index fa476060..22ffd4f7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -46,8 +46,12 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running let settings = configuration.settings.read().await; + // From [database] config let database_connect_url = settings.database.connect_url.clone(); - let torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval; + // From [importer] config + let importer_torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval; + let importer_port = settings.tracker_statistics_importer.port; + // From [net] config let net_ip = "0.0.0.0".to_string(); let net_port = settings.net.port; @@ -155,8 +159,11 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running // Start cronjob to import tracker torrent data and updating // seeders and leechers info. - let tracker_statistics_importer_handle = - console::tracker_statistics_importer::start(torrent_info_update_interval, &tracker_statistics_importer); + let tracker_statistics_importer_handle = console::tracker_statistics_importer::start( + importer_port, + importer_torrent_info_update_interval, + &tracker_statistics_importer, + ); // Start API server let running_api = web::api::start(app_data, &net_ip, net_port, api_version).await; diff --git a/src/config.rs b/src/config.rs index 941c3921..b660236c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -334,12 +334,15 @@ impl Default for Api { pub struct TrackerStatisticsImporter { /// The interval in seconds to get statistics from the tracker. pub torrent_info_update_interval: u64, + /// The port to listen on. Default to `3002`. + pub port: u16, } impl Default for TrackerStatisticsImporter { fn default() -> Self { Self { torrent_info_update_interval: 3600, + port: 3002, } } } diff --git a/src/console/tracker_statistics_importer.rs b/src/console/tracker_statistics_importer.rs index 12ae7c1b..c0787ad1 100644 --- a/src/console/tracker_statistics_importer.rs +++ b/src/console/tracker_statistics_importer.rs @@ -23,7 +23,6 @@ use tokio::task::JoinHandle; use crate::tracker::statistics_importer::StatisticsImporter; const IMPORTER_API_IP: &str = "127.0.0.1"; -const IMPORTER_API_PORT: u16 = 3002; // todo: use configuration option #[derive(Clone)] struct ImporterState { @@ -34,7 +33,11 @@ struct ImporterState { pub torrent_info_update_interval: u64, } -pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Arc) -> JoinHandle<()> { +pub fn start( + importer_port: u16, + torrent_info_update_interval: u64, + tracker_statistics_importer: &Arc, +) -> JoinHandle<()> { let weak_tracker_statistics_importer = Arc::downgrade(tracker_statistics_importer); tokio::spawn(async move { @@ -55,7 +58,7 @@ pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Ar .route("/heartbeat", post(heartbeat_handler)) .with_state(import_state); - let addr = format!("{IMPORTER_API_IP}:{IMPORTER_API_PORT}"); + let addr = format!("{IMPORTER_API_IP}:{importer_port}"); info!("Tracker statistics importer API server listening on http://{}", addr); @@ -79,7 +82,7 @@ pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Ar info!("Running tracker statistics importer ..."); - if let Err(e) = send_heartbeat().await { + if let Err(e) = send_heartbeat(importer_port).await { error!("Failed to send heartbeat from importer cronjob: {}", e); } @@ -117,9 +120,9 @@ async fn heartbeat_handler(State(state): State>) -> Json Result<(), reqwest::Error> { +async fn send_heartbeat(importer_port: u16) -> Result<(), reqwest::Error> { let client = reqwest::Client::new(); - let url = format!("http://{IMPORTER_API_IP}:{IMPORTER_API_PORT}/heartbeat"); + let url = format!("http://{IMPORTER_API_IP}:{importer_port}/heartbeat"); client.post(url).send().await?; diff --git a/src/lib.rs b/src/lib.rs index 039bdfeb..ba89003c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,6 +200,7 @@ //! //! [tracker_statistics_importer] //! torrent_info_update_interval = 3600 +//! port = 3002 //! ``` //! //! For more information about configuration you can visit the documentation for the [`config`]) module. diff --git a/src/web/api/v1/contexts/settings/mod.rs b/src/web/api/v1/contexts/settings/mod.rs index 5bb35151..bd8cd54c 100644 --- a/src/web/api/v1/contexts/settings/mod.rs +++ b/src/web/api/v1/contexts/settings/mod.rs @@ -75,6 +75,7 @@ //! }, //! "tracker_statistics_importer": { //! "torrent_info_update_interval": 3600 +//! "port": 3002 //! } //! } //! } diff --git a/tests/common/contexts/settings/mod.rs b/tests/common/contexts/settings/mod.rs index 5ba7a0db..ece13b6d 100644 --- a/tests/common/contexts/settings/mod.rs +++ b/tests/common/contexts/settings/mod.rs @@ -82,6 +82,7 @@ pub struct Api { #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)] pub struct TrackerStatisticsImporter { pub torrent_info_update_interval: u64, + port: u16, } impl From for Settings { @@ -185,6 +186,7 @@ impl From for TrackerStatisticsImporter { fn from(tracker_statistics_importer: DomainTrackerStatisticsImporter) -> Self { Self { torrent_info_update_interval: tracker_statistics_importer.torrent_info_update_interval, + port: tracker_statistics_importer.port, } } } diff --git a/tests/environments/isolated.rs b/tests/environments/isolated.rs index e30c8907..175976f2 100644 --- a/tests/environments/isolated.rs +++ b/tests/environments/isolated.rs @@ -82,6 +82,9 @@ fn ephemeral(temp_dir: &TempDir) -> config::TorrustIndex { // Ephemeral API port configuration.net.port = FREE_PORT; + // Ephemeral Importer API port + configuration.tracker_statistics_importer.port = FREE_PORT; + // Ephemeral SQLite database configuration.database.connect_url = format!("sqlite://{}?mode=rwc", random_database_file_path_in(temp_dir));