Skip to content

Commit 17f4e0c

Browse files
committed
feat: [#1358] add the and run the event listener when the tracker starts
This creates independent services that are not used yet in the tracker-core, meaning the `Swarms` object created in the `TorrentRepositoryContainer` will not store any torrent yet. The tracker core is still creating its own fresh instance.
1 parent 1508bfb commit 17f4e0c

File tree

8 files changed

+86
-0
lines changed

8 files changed

+86
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ torrust-rest-tracker-api-core = { version = "3.0.0-develop", path = "packages/re
5555
torrust-server-lib = { version = "3.0.0-develop", path = "packages/server-lib" }
5656
torrust-tracker-clock = { version = "3.0.0-develop", path = "packages/clock" }
5757
torrust-tracker-configuration = { version = "3.0.0-develop", path = "packages/configuration" }
58+
torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "packages/torrent-repository" }
5859
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "packages/udp-tracker-server" }
5960
tracing = "0"
6061
tracing-subscriber = { version = "0", features = ["json"] }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::sync::Arc;
2+
3+
use crate::event::bus::EventBus;
4+
use crate::event::sender::Broadcaster;
5+
use crate::event::{self};
6+
use crate::statistics::repository::Repository;
7+
use crate::{statistics, Swarms};
8+
9+
pub struct TorrentRepositoryContainer {
10+
pub swarms: Arc<Swarms>,
11+
pub event_bus: Arc<event::bus::EventBus>,
12+
pub stats_event_sender: event::sender::Sender,
13+
pub stats_repository: Arc<statistics::repository::Repository>,
14+
}
15+
16+
impl TorrentRepositoryContainer {
17+
#[must_use]
18+
pub fn initialize() -> Self {
19+
let swarms = Arc::new(Swarms::default());
20+
21+
// Torrent repository stats
22+
let broadcaster = Broadcaster::default();
23+
let stats_repository = Arc::new(Repository::new());
24+
25+
// todo: add a config option to enable/disable stats for this package
26+
let event_bus = Arc::new(EventBus::new(true, broadcaster.clone()));
27+
28+
let stats_event_sender = event_bus.sender();
29+
30+
Self {
31+
swarms,
32+
event_bus,
33+
stats_event_sender,
34+
stats_repository,
35+
}
36+
}
37+
}

packages/torrent-repository/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod container;
12
pub mod event;
23
pub mod statistics;
34
pub mod swarm;

src/app.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ async fn load_data_from_database(config: &Configuration, app_container: &Arc<App
7272
async fn start_jobs(config: &Configuration, app_container: &Arc<AppContainer>) -> JobManager {
7373
let mut job_manager = JobManager::new();
7474

75+
start_torrent_repository_event_listener(config, app_container, &mut job_manager);
7576
start_http_core_event_listener(config, app_container, &mut job_manager);
7677
start_udp_core_event_listener(config, app_container, &mut job_manager);
7778
start_udp_server_event_listener(config, app_container, &mut job_manager);
79+
7880
start_the_udp_instances(config, app_container, &mut job_manager).await;
7981
start_the_http_instances(config, app_container, &mut job_manager).await;
8082
start_the_http_api(config, app_container, &mut job_manager).await;
@@ -126,6 +128,18 @@ fn load_torrents_from_database(config: &Configuration, app_container: &Arc<AppCo
126128
}
127129
}
128130

131+
fn start_torrent_repository_event_listener(
132+
config: &Configuration,
133+
app_container: &Arc<AppContainer>,
134+
job_manager: &mut JobManager,
135+
) {
136+
let opt_handle = jobs::torrent_repository::start_event_listener(config, app_container);
137+
138+
if let Some(handle) = opt_handle {
139+
job_manager.push("torrent_repository_event_listener", handle);
140+
}
141+
}
142+
129143
fn start_http_core_event_listener(config: &Configuration, app_container: &Arc<AppContainer>, job_manager: &mut JobManager) {
130144
let opt_handle = jobs::http_tracker_core::start_event_listener(config, app_container);
131145

src/bootstrap/jobs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod http_tracker;
1111
pub mod http_tracker_core;
1212
pub mod manager;
1313
pub mod torrent_cleanup;
14+
pub mod torrent_repository;
1415
pub mod tracker_apis;
1516
pub mod udp_tracker;
1617
pub mod udp_tracker_core;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::sync::Arc;
2+
3+
use tokio::task::JoinHandle;
4+
use torrust_tracker_configuration::Configuration;
5+
6+
use crate::container::AppContainer;
7+
8+
pub fn start_event_listener(config: &Configuration, app_container: &Arc<AppContainer>) -> Option<JoinHandle<()>> {
9+
if config.core.tracker_usage_statistics {
10+
let job = torrust_tracker_torrent_repository::statistics::event::listener::run_event_listener(
11+
app_container.torrent_repository_container.event_bus.receiver(),
12+
&app_container.torrent_repository_container.stats_repository,
13+
);
14+
15+
Some(job)
16+
} else {
17+
tracing::info!("HTTP tracker core event listener job is disabled.");
18+
None
19+
}
20+
}

src/container.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use bittorrent_udp_tracker_core::{self};
99
use torrust_rest_tracker_api_core::container::TrackerHttpApiCoreContainer;
1010
use torrust_server_lib::registar::Registar;
1111
use torrust_tracker_configuration::{Configuration, HttpApi};
12+
use torrust_tracker_torrent_repository::container::TorrentRepositoryContainer;
1213
use torrust_udp_tracker_server::container::UdpTrackerServerContainer;
1314
use tracing::instrument;
1415

@@ -28,6 +29,9 @@ pub struct AppContainer {
2829
// Registar
2930
pub registar: Arc<Registar>,
3031

32+
// Torrent Repository
33+
pub torrent_repository_container: Arc<TorrentRepositoryContainer>,
34+
3135
// Core
3236
pub tracker_core_container: Arc<TrackerCoreContainer>,
3337

@@ -54,6 +58,10 @@ impl AppContainer {
5458

5559
let registar = Arc::new(Registar::default());
5660

61+
// Torrent Repository
62+
63+
let torrent_repository_container = Arc::new(TorrentRepositoryContainer::initialize());
64+
5765
// Core
5866

5967
let tracker_core_container = Arc::new(TrackerCoreContainer::initialize(&core_config));
@@ -84,6 +92,9 @@ impl AppContainer {
8492
// Registar
8593
registar,
8694

95+
// Torrent Repository
96+
torrent_repository_container,
97+
8798
// Core
8899
tracker_core_container,
89100

0 commit comments

Comments
 (0)