Skip to content

Commit

Permalink
fix: [torrust#384] use free por for importer API for isolated test envs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 17, 2023
1 parent a392ea1 commit 2a2d625
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions share/default/config/index.container.mysql.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ max_torrent_page_size = 30

[tracker_statistics_importer]
torrent_info_update_interval = 3600
port = 3002
1 change: 1 addition & 0 deletions share/default/config/index.container.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ max_torrent_page_size = 30

[tracker_statistics_importer]
torrent_info_update_interval = 3600
port = 3002
1 change: 1 addition & 0 deletions share/default/config/index.development.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ max_torrent_page_size = 30

[tracker_statistics_importer]
torrent_info_update_interval = 3600
port = 3002
13 changes: 10 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions src/console/tracker_statistics_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,7 +33,11 @@ struct ImporterState {
pub torrent_info_update_interval: u64,
}

pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Arc<StatisticsImporter>) -> JoinHandle<()> {
pub fn start(
importer_port: u16,
torrent_info_update_interval: u64,
tracker_statistics_importer: &Arc<StatisticsImporter>,
) -> JoinHandle<()> {
let weak_tracker_statistics_importer = Arc::downgrade(tracker_statistics_importer);

tokio::spawn(async move {
Expand All @@ -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);

Expand All @@ -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);
}

Expand Down Expand Up @@ -117,9 +120,9 @@ async fn heartbeat_handler(State(state): State<Arc<ImporterState>>) -> Json<Valu
}

/// Send a heartbeat from the importer cronjob to the importer API.
async fn send_heartbeat() -> 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?;

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/web/api/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
//! },
//! "tracker_statistics_importer": {
//! "torrent_info_update_interval": 3600
//! "port": 3002
//! }
//! }
//! }
Expand Down
2 changes: 2 additions & 0 deletions tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DomainSettings> for Settings {
Expand Down Expand Up @@ -185,6 +186,7 @@ impl From<DomainTrackerStatisticsImporter> 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,
}
}
}
3 changes: 3 additions & 0 deletions tests/environments/isolated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 2a2d625

Please sign in to comment.