Skip to content

Commit f18bad5

Browse files
committed
fix: [#384] use free por for importer API for isolated test envs
1 parent a392ea1 commit f18bad5

File tree

10 files changed

+28
-8
lines changed

10 files changed

+28
-8
lines changed

share/default/config/index.container.mysql.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ max_torrent_page_size = 30
4848

4949
[tracker_statistics_importer]
5050
torrent_info_update_interval = 3600
51+
port = 3002

share/default/config/index.container.sqlite3.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ max_torrent_page_size = 30
4848

4949
[tracker_statistics_importer]
5050
torrent_info_update_interval = 3600
51+
port = 3002

share/default/config/index.development.sqlite3.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ max_torrent_page_size = 30
4444

4545
[tracker_statistics_importer]
4646
torrent_info_update_interval = 3600
47+
port = 3002

src/app.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
4646

4747
let settings = configuration.settings.read().await;
4848

49+
// From [database] config
4950
let database_connect_url = settings.database.connect_url.clone();
50-
let torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval;
51+
// From [importer] config
52+
let importer_torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval;
53+
let importer_port = settings.tracker_statistics_importer.port;
54+
// From [net] config
5155
let net_ip = "0.0.0.0".to_string();
5256
let net_port = settings.net.port;
5357

@@ -156,7 +160,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
156160
// Start cronjob to import tracker torrent data and updating
157161
// seeders and leechers info.
158162
let tracker_statistics_importer_handle =
159-
console::tracker_statistics_importer::start(torrent_info_update_interval, &tracker_statistics_importer);
163+
console::tracker_statistics_importer::start(importer_port, importer_torrent_info_update_interval, &tracker_statistics_importer);
160164

161165
// Start API server
162166
let running_api = web::api::start(app_data, &net_ip, net_port, api_version).await;

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,15 @@ impl Default for Api {
334334
pub struct TrackerStatisticsImporter {
335335
/// The interval in seconds to get statistics from the tracker.
336336
pub torrent_info_update_interval: u64,
337+
/// The port to listen on. Default to `3002`.
338+
pub port: u16,
337339
}
338340

339341
impl Default for TrackerStatisticsImporter {
340342
fn default() -> Self {
341343
Self {
342344
torrent_info_update_interval: 3600,
345+
port: 3002,
343346
}
344347
}
345348
}

src/console/tracker_statistics_importer.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use tokio::task::JoinHandle;
2323
use crate::tracker::statistics_importer::StatisticsImporter;
2424

2525
const IMPORTER_API_IP: &str = "127.0.0.1";
26-
const IMPORTER_API_PORT: u16 = 3002; // todo: use configuration option
2726

2827
#[derive(Clone)]
2928
struct ImporterState {
@@ -34,7 +33,11 @@ struct ImporterState {
3433
pub torrent_info_update_interval: u64,
3534
}
3635

37-
pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Arc<StatisticsImporter>) -> JoinHandle<()> {
36+
pub fn start(
37+
importer_port: u16,
38+
torrent_info_update_interval: u64,
39+
tracker_statistics_importer: &Arc<StatisticsImporter>,
40+
) -> JoinHandle<()> {
3841
let weak_tracker_statistics_importer = Arc::downgrade(tracker_statistics_importer);
3942

4043
tokio::spawn(async move {
@@ -55,7 +58,7 @@ pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Ar
5558
.route("/heartbeat", post(heartbeat_handler))
5659
.with_state(import_state);
5760

58-
let addr = format!("{IMPORTER_API_IP}:{IMPORTER_API_PORT}");
61+
let addr = format!("{IMPORTER_API_IP}:{importer_port}");
5962

6063
info!("Tracker statistics importer API server listening on http://{}", addr);
6164

@@ -79,7 +82,7 @@ pub fn start(torrent_info_update_interval: u64, tracker_statistics_importer: &Ar
7982

8083
info!("Running tracker statistics importer ...");
8184

82-
if let Err(e) = send_heartbeat().await {
85+
if let Err(e) = send_heartbeat(importer_port).await {
8386
error!("Failed to send heartbeat from importer cronjob: {}", e);
8487
}
8588

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

119122
/// Send a heartbeat from the importer cronjob to the importer API.
120-
async fn send_heartbeat() -> Result<(), reqwest::Error> {
123+
async fn send_heartbeat(importer_port: u16) -> Result<(), reqwest::Error> {
121124
let client = reqwest::Client::new();
122-
let url = format!("http://{IMPORTER_API_IP}:{IMPORTER_API_PORT}/heartbeat");
125+
let url = format!("http://{IMPORTER_API_IP}:{importer_port}/heartbeat");
123126

124127
client.post(url).send().await?;
125128

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
//!
201201
//! [tracker_statistics_importer]
202202
//! torrent_info_update_interval = 3600
203+
//! port = 3002
203204
//! ```
204205
//!
205206
//! For more information about configuration you can visit the documentation for the [`config`]) module.

src/web/api/v1/contexts/settings/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
//! },
7676
//! "tracker_statistics_importer": {
7777
//! "torrent_info_update_interval": 3600
78+
//! "port": 3002
7879
//! }
7980
//! }
8081
//! }

tests/common/contexts/settings/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub struct Api {
8282
#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
8383
pub struct TrackerStatisticsImporter {
8484
pub torrent_info_update_interval: u64,
85+
port: u16,
8586
}
8687

8788
impl From<DomainSettings> for Settings {
@@ -185,6 +186,7 @@ impl From<DomainTrackerStatisticsImporter> for TrackerStatisticsImporter {
185186
fn from(tracker_statistics_importer: DomainTrackerStatisticsImporter) -> Self {
186187
Self {
187188
torrent_info_update_interval: tracker_statistics_importer.torrent_info_update_interval,
189+
port: tracker_statistics_importer.port,
188190
}
189191
}
190192
}

tests/environments/isolated.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ fn ephemeral(temp_dir: &TempDir) -> config::TorrustIndex {
8282
// Ephemeral API port
8383
configuration.net.port = FREE_PORT;
8484

85+
// Ephemeral Importer API port
86+
configuration.tracker_statistics_importer.port = FREE_PORT;
87+
8588
// Ephemeral SQLite database
8689
configuration.database.connect_url = format!("sqlite://{}?mode=rwc", random_database_file_path_in(temp_dir));
8790

0 commit comments

Comments
 (0)