Skip to content

Commit 4febda4

Browse files
committed
fic: [#1539] persisten metrics should be enabled by config
1 parent 2c9311b commit 4febda4

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

packages/tracker-core/src/statistics/event/handler.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub async fn handle_event(
1313
event: Event,
1414
stats_repository: &Arc<Repository>,
1515
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
16+
persistent_torrent_completed_stat: bool,
1617
now: DurationSinceUnixEpoch,
1718
) {
1819
match event {
@@ -50,23 +51,25 @@ pub async fn handle_event(
5051
)
5152
.await;
5253

53-
// Increment the number of downloads for the torrent in the database
54-
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
55-
Ok(()) => {
56-
tracing::debug!(info_hash = ?info_hash, "Number of torrent downloads increased");
54+
if persistent_torrent_completed_stat {
55+
// Increment the number of downloads for the torrent in the database
56+
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
57+
Ok(()) => {
58+
tracing::debug!(info_hash = ?info_hash, "Number of torrent downloads increased");
59+
}
60+
Err(err) => {
61+
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads for the torrent");
62+
}
5763
}
58-
Err(err) => {
59-
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads for the torrent");
60-
}
61-
}
6264

63-
// Increment the global number of downloads (for all torrents) in the database
64-
match db_torrent_repository.increase_global_number_of_downloads() {
65-
Ok(()) => {
66-
tracing::debug!("Global number of downloads increased");
67-
}
68-
Err(err) => {
69-
tracing::error!(error = ?err, "Failed to increase global number of downloads");
65+
// Increment the global number of downloads (for all torrents) in the database
66+
match db_torrent_repository.increase_global_number_of_downloads() {
67+
Ok(()) => {
68+
tracing::debug!("Global number of downloads increased");
69+
}
70+
Err(err) => {
71+
tracing::error!(error = ?err, "Failed to increase global number of downloads");
72+
}
7073
}
7174
}
7275
}

packages/tracker-core/src/statistics/event/listener.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ pub fn run_event_listener(
1515
receiver: Receiver,
1616
repository: &Arc<Repository>,
1717
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
18+
persistent_torrent_completed_stat: bool,
1819
) -> JoinHandle<()> {
1920
let stats_repository = repository.clone();
2021
let db_torrent_repository: Arc<DatabasePersistentTorrentRepository> = db_torrent_repository.clone();
2122

2223
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Starting torrent repository event listener");
2324

2425
tokio::spawn(async move {
25-
dispatch_events(receiver, stats_repository, db_torrent_repository).await;
26+
dispatch_events(
27+
receiver,
28+
stats_repository,
29+
db_torrent_repository,
30+
persistent_torrent_completed_stat,
31+
)
32+
.await;
2633

2734
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Torrent repository listener finished");
2835
})
@@ -32,6 +39,7 @@ async fn dispatch_events(
3239
mut receiver: Receiver,
3340
stats_repository: Arc<Repository>,
3441
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
42+
persistent_torrent_completed_stat: bool,
3543
) {
3644
let shutdown_signal = tokio::signal::ctrl_c();
3745

@@ -48,7 +56,12 @@ async fn dispatch_events(
4856

4957
result = receiver.recv() => {
5058
match result {
51-
Ok(event) => handle_event(event, &stats_repository, &db_torrent_repository, CurrentClock::now()).await,
59+
Ok(event) => handle_event(
60+
event,
61+
&stats_repository,
62+
&db_torrent_repository,
63+
persistent_torrent_completed_stat,
64+
CurrentClock::now()).await,
5265
Err(e) => {
5366
match e {
5467
RecvError::Closed => {

packages/tracker-core/tests/common/test_env.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl TestEnv {
7878
self.torrent_repository_container.event_bus.receiver(),
7979
&self.tracker_core_container.stats_repository,
8080
&self.tracker_core_container.db_torrent_repository,
81+
self.tracker_core_container
82+
.core_config
83+
.tracker_policy
84+
.persistent_torrent_completed_stat,
8185
);
8286

8387
jobs.push(job);

src/bootstrap/jobs/tracker_core.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ pub fn start_event_listener(config: &Configuration, app_container: &Arc<AppConta
1111
app_container.torrent_repository_container.event_bus.receiver(),
1212
&app_container.tracker_core_container.stats_repository,
1313
&app_container.tracker_core_container.db_torrent_repository,
14+
app_container
15+
.tracker_core_container
16+
.core_config
17+
.tracker_policy
18+
.persistent_torrent_completed_stat,
1419
);
1520

1621
Some(job)

0 commit comments

Comments
 (0)