Extract retry logging into helper method and fix formatting
- Add LogRetryAttempt() helper method for consistent logging - Fix formatting in ShouldRetryRequest() for better readability - Use helper method in both GetRecentTracksRequestFinished and GetTopTracksRequestFinished - Eliminates duplicate logging code Co-authored-by: jonaski <10343810+jonaski@users.noreply.github.com>
This commit is contained in:
@@ -259,7 +259,7 @@ void LastFMImport::GetRecentTracksRequestFinished(QNetworkReply *reply, GetRecen
|
||||
if (!json_object_result.success()) {
|
||||
if (ShouldRetryRequest(json_object_result) && request.retry_count < kMaxRetries) {
|
||||
const int delay_ms = CalculateBackoffDelay(request.retry_count);
|
||||
qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")";
|
||||
LogRetryAttempt(json_object_result.http_status_code, request.retry_count, delay_ms);
|
||||
QTimer::singleShot(delay_ms, this, [this, request]() {
|
||||
GetRecentTracksRequest retry_request(request.page, request.retry_count + 1);
|
||||
SendGetRecentTracksRequest(retry_request);
|
||||
@@ -426,7 +426,7 @@ void LastFMImport::GetTopTracksRequestFinished(QNetworkReply *reply, GetTopTrack
|
||||
if (!json_object_result.success()) {
|
||||
if (ShouldRetryRequest(json_object_result) && request.retry_count < kMaxRetries) {
|
||||
const int delay_ms = CalculateBackoffDelay(request.retry_count);
|
||||
qLog(Warning) << "Last.fm request failed with status" << json_object_result.http_status_code << ", retrying in" << delay_ms << "ms (attempt" << (request.retry_count + 1) << "of" << kMaxRetries << ")";
|
||||
LogRetryAttempt(json_object_result.http_status_code, request.retry_count, delay_ms);
|
||||
QTimer::singleShot(delay_ms, this, [this, request]() {
|
||||
GetTopTracksRequest retry_request(request.page, request.retry_count + 1);
|
||||
SendGetTopTracksRequest(retry_request);
|
||||
@@ -554,6 +554,12 @@ bool LastFMImport::ShouldRetryRequest(const JsonObjectResult &result) const {
|
||||
result.network_error == QNetworkReply::TemporaryNetworkFailureError;
|
||||
}
|
||||
|
||||
void LastFMImport::LogRetryAttempt(const int http_status_code, const int retry_count, const int delay_ms) const {
|
||||
qLog(Warning) << "Last.fm request failed with status" << http_status_code
|
||||
<< ", retrying in" << delay_ms << "ms (attempt"
|
||||
<< (retry_count + 1) << "of" << kMaxRetries << ")";
|
||||
}
|
||||
|
||||
int LastFMImport::CalculateBackoffDelay(const int retry_count) const {
|
||||
const int safe_shift = std::min(retry_count, kMaxBackoffShift);
|
||||
return kInitialBackoffMs * (1 << safe_shift);
|
||||
|
||||
@@ -82,6 +82,7 @@ class LastFMImport : public JsonBaseRequest {
|
||||
|
||||
bool ShouldRetryRequest(const JsonObjectResult &result) const;
|
||||
int CalculateBackoffDelay(const int retry_count) const;
|
||||
void LogRetryAttempt(const int http_status_code, const int retry_count, const int delay_ms) const;
|
||||
|
||||
void Error(const QString &error, const QVariant &debug = QVariant()) override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user