From f27e006e576866ad19fe350f5f1907585356eaac Mon Sep 17 00:00:00 2001 From: Brent <25038394+elric667@users.noreply.github.com> Date: Sat, 14 Nov 2026 00:25:25 +0000 Subject: [PATCH 1/3] Back off pending author import retries after the first hour Pending author imports that wait on the metadata server retried every ~5 minutes forever, and every attempt runs a full author refresh. A request that never resolves cost 288 author refreshes a day (#359, #264, #119). Keep the existing cadence for roughly the first hour (three 1-minute retries, then 5-minute retries through attempt 15), then double the delay up to a 6-hour ceiling. Imports still never fail on a transient wait. The shared AuthorRetrySchedule.NextAttempt used by metadata and format sync is unchanged. Also log the "waiting until" line at Warn only on the attempts ShouldWarn already selects (1, 4, every 10th), Debug otherwise. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018XPvbXWnYFj6KyHWfsjbQt --- .../Books/PendingAuthorImportRetryFixture.cs | 30 +++++++++++++++- .../Books/AuthorRetrySchedule.cs | 35 +++++++++++++++++++ .../Services/PendingAuthorImportService.cs | 5 +-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs b/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs index fe0e4d50d..967cd5c9f 100644 --- a/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs +++ b/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs @@ -194,11 +194,39 @@ public void schedule_retry_should_not_turn_a_transient_wait_into_failure_at_the_ Assert.That(item.OverallStatus, Is.EqualTo(PendingImportStatus.Retrying)); Assert.That(item.AudiobookStatus, Is.EqualTo(PendingImportStatus.Pending)); Assert.That(item.EbookStatus, Is.EqualTo(PendingImportStatus.NotRequested)); - Assert.That(item.NextAttemptAt, Is.InRange(before.AddMinutes(3.9), DateTime.UtcNow.AddMinutes(6.1))); + + // A long-running wait stays Retrying forever, but at the capped backoff rather than every five minutes. + Assert.That(item.NextAttemptAt, Is.InRange(before.AddHours(4.79), DateTime.UtcNow.AddHours(7.21))); Assert.That(repositoryProxy.Updated, Is.SameAs(item)); Assert.That(events.Events, Has.None.InstanceOf()); } + [TestCase(1, 60)] + [TestCase(3, 60)] + [TestCase(4, 300)] + [TestCase(15, 300)] + [TestCase(16, 600)] + [TestCase(17, 1200)] + [TestCase(21, 19200)] + [TestCase(22, 21600)] + [TestCase(8727, 21600)] + [TestCase(int.MaxValue, 21600)] + public void pending_import_retry_should_keep_the_first_hour_fast_then_back_off_to_a_ceiling(int attemptCount, double expectedSeconds) + { + Assert.That(AuthorRetrySchedule.PendingImportDelaySeconds(attemptCount), Is.EqualTo(expectedSeconds)); + } + + [Test] + public void shared_author_retry_schedule_should_keep_its_five_minute_ceiling() + { + // Metadata sync and format sync share NextAttempt; only pending imports back off further. + var before = DateTime.UtcNow; + + var next = AuthorRetrySchedule.NextAttempt(500); + + Assert.That(next, Is.InRange(before.AddSeconds(239), DateTime.UtcNow.AddSeconds(361))); + } + [Test] public void expected_version_status_update_should_leave_a_newer_merged_request_unchanged() { diff --git a/src/NzbDrone.Core/Books/AuthorRetrySchedule.cs b/src/NzbDrone.Core/Books/AuthorRetrySchedule.cs index 64dea1042..13c19850b 100644 --- a/src/NzbDrone.Core/Books/AuthorRetrySchedule.cs +++ b/src/NzbDrone.Core/Books/AuthorRetrySchedule.cs @@ -4,6 +4,14 @@ namespace NzbDrone.Core.Books { internal static class AuthorRetrySchedule { + // Pending author imports keep the original cadence for roughly the first hour + // (three one-minute retries, then five-minute retries), then double the delay up + // to this ceiling. A request the metadata server never fulfils still retries + // forever, but a few times a day instead of 288 attempts (each a full author + // refresh) a day. + internal const int PendingImportFastAttempts = 15; + internal const double PendingImportMaxDelaySeconds = 6 * 60 * 60; + public static bool ShouldWarn(int attemptCount) => attemptCount == 1 || attemptCount == 4 || attemptCount % 10 == 0; public static DateTime NextAttempt(int attemptCount) @@ -11,6 +19,33 @@ public static DateTime NextAttempt(int attemptCount) // Shared with pending author requests: three short retries, then a bounded // five-minute delay. Jitter keeps a recovered server from receiving a burst. var seconds = attemptCount <= 3 ? 60 : 300; + return WithJitter(seconds); + } + + public static DateTime NextPendingImportAttempt(int attemptCount) + { + return WithJitter(PendingImportDelaySeconds(attemptCount)); + } + + internal static double PendingImportDelaySeconds(int attemptCount) + { + if (attemptCount <= 3) + { + return 60; + } + + if (attemptCount <= PendingImportFastAttempts) + { + return 300; + } + + // Cap the exponent so very large legacy attempt counts cannot overflow. + var doublings = Math.Min(attemptCount - PendingImportFastAttempts, 16); + return Math.Min(300 * Math.Pow(2, doublings), PendingImportMaxDelaySeconds); + } + + private static DateTime WithJitter(double seconds) + { return DateTime.UtcNow.AddSeconds(seconds * (0.8 + Random.Shared.NextDouble() * 0.4)); } } diff --git a/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs b/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs index 4e498a23e..ae2c86c36 100644 --- a/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs +++ b/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs @@ -1025,7 +1025,7 @@ public void ScheduleRetry(PendingAuthorImport item, string error) current.MaxAttempts = 0; current.LastAttemptAt = DateTime.UtcNow; current.LastError = error; - current.NextAttemptAt = AuthorRetrySchedule.NextAttempt(current.AttemptCount); + current.NextAttemptAt = AuthorRetrySchedule.NextPendingImportAttempt(current.AttemptCount); current.OverallStatus = PendingImportStatus.Retrying; current.UpdatedAt = DateTime.UtcNow; @@ -1042,7 +1042,8 @@ public void ScheduleRetry(PendingAuthorImport item, string error) } CopyMutableState(current, item); - _logger.Warn("Pending import {0} attempt {1} waiting until {2}: {3}", current.Id, current.AttemptCount, current.NextAttemptAt, error); + var level = AuthorRetrySchedule.ShouldWarn(current.AttemptCount) ? LogLevel.Warn : LogLevel.Debug; + _logger.Log(level, "Pending import {0} attempt {1} waiting until {2}: {3}", current.Id, current.AttemptCount, current.NextAttemptAt, error); return; } } From fc3c95d44e347f02e5da4a562687d61eabe30a22 Mon Sep 17 00:00:00 2001 From: Brent <25038394+elric667@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:27:25 +0000 Subject: [PATCH 2/3] Don't push back a request merged in while a retry is scheduled ScheduleRetry re-reads the row and only copies the processed progress when the version is unchanged, but it always replaced NextAttemptAt with the new backoff. If a new request was merged in meanwhile (newer version, due now), it could be delayed by up to seven hours. Keep the newer request's earlier time; the backoff still applies to the request the worker processed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018XPvbXLSvnvziBY9jiTQDr --- .../Books/PendingAuthorImportRetryFixture.cs | 48 +++++++++++++++++++ .../Services/PendingAuthorImportService.cs | 8 +++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs b/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs index 967cd5c9f..a43a4017a 100644 --- a/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs +++ b/src/Chaptarr.Core.Test/Books/PendingAuthorImportRetryFixture.cs @@ -201,6 +201,54 @@ public void schedule_retry_should_not_turn_a_transient_wait_into_failure_at_the_ Assert.That(events.Events, Has.None.InstanceOf()); } + [Test] + public void schedule_retry_should_not_push_back_a_request_merged_in_after_the_worker_read_it() + { + var repository = DispatchProxy.Create(); + var repositoryProxy = (RepositoryProxy)repository; + var subject = new PendingAuthorImportService(repository, DispatchProxy.Create(), new RecordingEventAggregator(), LogManager.GetCurrentClassLogger()); + + // The worker processed version 5 at attempt 99; meanwhile a new request was merged + // in (version 6) and made due straight away. + var processed = new PendingAuthorImport + { + Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 5, + AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested, + OverallStatus = PendingImportStatus.Retrying + }; + var dueNow = DateTime.UtcNow; + repositoryProxy.Active = new PendingAuthorImport + { + Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 6, NextAttemptAt = dueNow, + AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested, + OverallStatus = PendingImportStatus.Pending + }; + + subject.ScheduleRetry(processed, PendingAuthorImportRetryReason.AuthorNotYetAvailable); + + Assert.That(repositoryProxy.Updated.NextAttemptAt, Is.EqualTo(dueNow)); + } + + [Test] + public void schedule_retry_should_back_off_the_request_the_worker_processed() + { + var repository = DispatchProxy.Create(); + var repositoryProxy = (RepositoryProxy)repository; + var subject = new PendingAuthorImportService(repository, DispatchProxy.Create(), new RecordingEventAggregator(), LogManager.GetCurrentClassLogger()); + var item = new PendingAuthorImport + { + Id = 42, ProviderId = "gr:42", AttemptCount = 99, Version = 5, NextAttemptAt = DateTime.UtcNow, + AudiobookStatus = PendingImportStatus.Pending, EbookStatus = PendingImportStatus.NotRequested, + OverallStatus = PendingImportStatus.Retrying + }; + repositoryProxy.Active = item; + var before = DateTime.UtcNow; + + subject.ScheduleRetry(item, PendingAuthorImportRetryReason.AuthorNotYetAvailable); + + Assert.That(repositoryProxy.Updated.NextAttemptAt, Is.GreaterThan(before.AddHours(4.79))); + } + [TestCase(1, 60)] [TestCase(3, 60)] [TestCase(4, 300)] diff --git a/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs b/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs index ae2c86c36..cf42c0d2e 100644 --- a/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs +++ b/src/NzbDrone.Core/Books/Services/PendingAuthorImportService.cs @@ -1025,7 +1025,13 @@ public void ScheduleRetry(PendingAuthorImport item, string error) current.MaxAttempts = 0; current.LastAttemptAt = DateTime.UtcNow; current.LastError = error; - current.NextAttemptAt = AuthorRetrySchedule.NextPendingImportAttempt(current.AttemptCount); + var backoff = AuthorRetrySchedule.NextPendingImportAttempt(current.AttemptCount); + + // A request merged in after this worker read the row may be due sooner; the backoff + // is for the request this worker processed, so it must not push the newer one back. + current.NextAttemptAt = current.Version != item.Version && current.NextAttemptAt < backoff + ? current.NextAttemptAt + : backoff; current.OverallStatus = PendingImportStatus.Retrying; current.UpdatedAt = DateTime.UtcNow; From e3c9ac020b87e769538686ec3e9a98910e6ec370 Mon Sep 17 00:00:00 2001 From: Brent <25038394+elric667@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:51:04 +0000 Subject: [PATCH 3/3] Re-run CI The macOS job failed on LogRedactionBoundaryFixture, a timing test that also fails intermittently on develop. This empty commit starts a new run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018XPvbXLSvnvziBY9jiTQDr