Skip to content

Commit e595190

Browse files
committed
refactor: [torrust#1477] create an explicit job creation module for event listeners
The "jobs" folder should contain all the jogs run by the main app.
1 parent a29aaae commit e595190

File tree

5 files changed

+92
-52
lines changed

5 files changed

+92
-52
lines changed

src/app.rs

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use tokio::task::JoinHandle;
2727
use torrust_tracker_configuration::{Configuration, HttpTracker, UdpTracker};
2828
use tracing::instrument;
2929

30-
use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
30+
use crate::bootstrap::jobs::{self, health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
3131
use crate::bootstrap::{self};
3232
use crate::container::AppContainer;
3333

@@ -110,63 +110,42 @@ async fn load_whitelisted_torrents(config: &Configuration, app_container: &Arc<A
110110
}
111111

112112
fn start_http_core_event_listener(config: &Configuration, app_container: &Arc<AppContainer>) {
113-
if config.core.tracker_usage_statistics {
114-
let _job = bittorrent_http_tracker_core::statistics::event::listener::run_event_listener(
115-
app_container.http_tracker_core_services.event_bus.receiver(),
116-
&app_container.http_tracker_core_services.stats_repository,
117-
);
118-
119-
// todo: this cannot be enabled otherwise the application never ends
120-
// because the event listener never stops. You see this console message
121-
// forever:
122-
//
123-
// !! shuting down in 90 seconds !!
124-
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
125-
//
126-
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
127-
128-
//jobs.push(job);
129-
}
113+
let _job = jobs::http_tracker_core::start_event_listener(config, app_container);
114+
115+
// todo: this cannot be enabled otherwise the application never ends
116+
// because the event listener never stops. You see this console message
117+
// forever:
118+
//
119+
// !! shuting down in 90 seconds !!
120+
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
121+
//
122+
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
130123
}
131124

132125
fn start_udp_core_event_listener(config: &Configuration, app_container: &Arc<AppContainer>) {
133-
if config.core.tracker_usage_statistics {
134-
let _job = bittorrent_udp_tracker_core::statistics::event::listener::run_event_listener(
135-
app_container.udp_tracker_core_services.event_bus.receiver(),
136-
&app_container.udp_tracker_core_services.stats_repository,
137-
);
138-
139-
// todo: this cannot be enabled otherwise the application never ends
140-
// because the event listener never stops. You see this console message
141-
// forever:
142-
//
143-
// !! shuting down in 90 seconds !!
144-
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
145-
//
146-
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
147-
148-
//jobs.push(job);
149-
}
126+
let _job = jobs::udp_tracker_core::start_event_listener(config, app_container);
127+
128+
// todo: the job cannot be added in the jobs vector otherwise the application never ends
129+
// because the event listener never stops. You see this console message
130+
// forever:
131+
//
132+
// !! shuting down in 90 seconds !!
133+
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
134+
//
135+
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
150136
}
151137

152138
fn start_udp_server_event_listener(config: &Configuration, app_container: &Arc<AppContainer>) {
153-
if config.core.tracker_usage_statistics {
154-
let _job = torrust_udp_tracker_server::statistics::event::listener::run_event_listener(
155-
app_container.udp_tracker_server_container.event_bus.receiver(),
156-
&app_container.udp_tracker_server_container.stats_repository,
157-
);
158-
159-
// todo: this cannot be enabled otherwise the application never ends
160-
// because the event listener never stops. You see this console message
161-
// forever:
162-
//
163-
// !! shuting down in 90 seconds !!
164-
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
165-
//
166-
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
167-
168-
//jobs.push(job);
169-
}
139+
let _job = jobs::udp_tracker_server::start_event_listener(config, app_container);
140+
141+
// todo: the job cannot be added in the jobs vector otherwise the application never ends
142+
// because the event listener never stops. You see this console message
143+
// forever:
144+
//
145+
// !! shuting down in 90 seconds !!
146+
// 2025-04-24T15:27:45.454101Z INFO graceful_shutdown: torrust_axum_server::signals: remaining alive connections: 0
147+
//
148+
// Depends on: https://github.com/torrust/torrust-tracker/issues/1405
170149
}
171150

172151
async fn start_the_udp_instances(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
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 = bittorrent_http_tracker_core::statistics::event::listener::run_event_listener(
11+
app_container.http_tracker_core_services.event_bus.receiver(),
12+
&app_container.http_tracker_core_services.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/bootstrap/jobs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//!
88
//! This modules contains all the functions needed to start those jobs.
99
pub mod health_check_api;
10+
pub mod http_tracker_core;
1011
pub mod http_tracker;
1112
pub mod torrent_cleanup;
1213
pub mod tracker_apis;
14+
pub mod udp_tracker_core;
15+
pub mod udp_tracker_server;
1316
pub mod udp_tracker;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 = bittorrent_udp_tracker_core::statistics::event::listener::run_event_listener(
11+
app_container.udp_tracker_core_services.event_bus.receiver(),
12+
&app_container.udp_tracker_core_services.stats_repository,
13+
);
14+
Some(job)
15+
} else {
16+
tracing::info!("UDP tracker core event listener job is disabled.");
17+
None
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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_udp_tracker_server::statistics::event::listener::run_event_listener(
11+
app_container.udp_tracker_server_container.event_bus.receiver(),
12+
&app_container.udp_tracker_server_container.stats_repository,
13+
);
14+
Some(job)
15+
} else {
16+
tracing::info!("UDP tracker server event listener job is disabled.");
17+
None
18+
}
19+
}

0 commit comments

Comments
 (0)