Skip to content

Commit f975bf5

Browse files
committed
fix: [#420] improve error loggin for statistics importer
Now you can see the exact error when the tracker API fails. For example, the following error when the torrent is not found: ``` 2024-01-03T17:25:49.817570509+00:00 [statistics_importer][ERROR] Error updating torrent tracker stats for torrent with id 3; info-hash c0bae61394917c2cc3aa3af9c2291bfe80b5bbf4. Error: TorrentNotFound ```
1 parent 70ac664 commit f975bf5

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/tracker/statistics_importer.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::sync::Arc;
22

33
use log::{error, info};
44

5-
use super::service::{Service, TorrentInfo};
5+
use super::service::{Service, TorrentInfo, TrackerAPIError};
66
use crate::config::Configuration;
77
use crate::databases::database::{self, Database};
8-
use crate::errors::ServiceError;
98

109
pub struct StatisticsImporter {
1110
database: Arc<Box<dyn Database>>,
@@ -39,22 +38,12 @@ impl StatisticsImporter {
3938

4039
let ret = self.import_torrent_statistics(torrent.torrent_id, &torrent.info_hash).await;
4140

42-
// code-review: should we treat differently for each case?. The
43-
// tracker API could be temporarily offline, or there could be a
44-
// tracker misconfiguration.
45-
//
46-
// This is the log when the torrent is not found in the tracker:
47-
//
48-
// ```
49-
// 2023-05-09T13:31:24.497465723+00:00 [torrust_index::tracker::statistics_importer][ERROR] Error updating torrent tracker stats for torrent with id 140: TorrentNotFound
50-
// ```
51-
5241
if let Some(err) = ret.err() {
5342
let message = format!(
5443
"Error updating torrent tracker stats for torrent with id {}: {:?}",
5544
torrent.torrent_id, err
5645
);
57-
error!("{}", message);
46+
error!(target: "statistics_importer", "{}", message);
5847
}
5948
}
6049

@@ -67,17 +56,20 @@ impl StatisticsImporter {
6756
///
6857
/// Will return an error if the HTTP request failed or the torrent is not
6958
/// found.
70-
pub async fn import_torrent_statistics(&self, torrent_id: i64, info_hash: &str) -> Result<TorrentInfo, ServiceError> {
71-
if let Ok(torrent_info) = self.tracker_service.get_torrent_info(info_hash).await {
72-
drop(
73-
self.database
74-
.update_tracker_info(torrent_id, &self.tracker_url, torrent_info.seeders, torrent_info.leechers)
75-
.await,
76-
);
77-
Ok(torrent_info)
78-
} else {
79-
drop(self.database.update_tracker_info(torrent_id, &self.tracker_url, 0, 0).await);
80-
Err(ServiceError::TorrentNotFound)
59+
pub async fn import_torrent_statistics(&self, torrent_id: i64, info_hash: &str) -> Result<TorrentInfo, TrackerAPIError> {
60+
match self.tracker_service.get_torrent_info(info_hash).await {
61+
Ok(torrent_info) => {
62+
drop(
63+
self.database
64+
.update_tracker_info(torrent_id, &self.tracker_url, torrent_info.seeders, torrent_info.leechers)
65+
.await,
66+
);
67+
Ok(torrent_info)
68+
}
69+
Err(err) => {
70+
drop(self.database.update_tracker_info(torrent_id, &self.tracker_url, 0, 0).await);
71+
Err(err)
72+
}
8173
}
8274
}
8375
}

0 commit comments

Comments
 (0)