From 617bf491eee1d1010dc23c3f6df5d148700feb44 Mon Sep 17 00:00:00 2001 From: MajorTanya <39014446+MajorTanya@users.noreply.github.com> Date: Sun, 18 Feb 2024 19:57:50 +0100 Subject: [PATCH] Fix DelayedTrackingUpdateJob spam on update errors (#411) * Fix DelayedTrackingUpdateJob spam on update errors DelayedTrackingUpdateJob would start spamming when it encountered an error (e.g. a tracker has an issue) and never stop. This seems to stem from a circular dependency between the Job's `doWork` and TrackChapter's `await`. TrackChapter sets up a completely new instance of the DelayedTrackingUpdateJob if any Exception was thrown during the track update. This causes the Job to get replaced (as per the WorkManager's set ExistingWorkPolicy). Because of this, the guard clause at the start of doWork would never trigger, as all instances of the Job would report being the 0th try (because they were completely new instances). This simple fix introduces a boolean `isRetry` parameter to TrackChapter's await method, which is set to `false` by default. DelayedTrackingUpdateJob however sets this parameter to `true`, which means TrackChapter won't try to set up the Job again. * Rename isRetry parameter to setupJobOnFailure This also inverts the logic, so true & false were swapped. --- .../java/eu/kanade/domain/track/interactor/TrackChapter.kt | 6 ++++-- .../kanade/domain/track/service/DelayedTrackingUpdateJob.kt | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/domain/track/interactor/TrackChapter.kt b/app/src/main/java/eu/kanade/domain/track/interactor/TrackChapter.kt index a942d5f94..fdf24ec4e 100644 --- a/app/src/main/java/eu/kanade/domain/track/interactor/TrackChapter.kt +++ b/app/src/main/java/eu/kanade/domain/track/interactor/TrackChapter.kt @@ -21,7 +21,7 @@ class TrackChapter( private val delayedTrackingStore: DelayedTrackingStore, ) { - suspend fun await(context: Context, mangaId: Long, chapterNumber: Double) { + suspend fun await(context: Context, mangaId: Long, chapterNumber: Double, setupJobOnFailure: Boolean = true) { withNonCancellableContext { val tracks = getTracks.await(mangaId) if (tracks.isEmpty()) return@withNonCancellableContext @@ -43,7 +43,9 @@ class TrackChapter( delayedTrackingStore.remove(track.id) } catch (e: Exception) { delayedTrackingStore.add(track.id, chapterNumber) - DelayedTrackingUpdateJob.setupTask(context) + if (setupJobOnFailure) { + DelayedTrackingUpdateJob.setupTask(context) + } throw e } } diff --git a/app/src/main/java/eu/kanade/domain/track/service/DelayedTrackingUpdateJob.kt b/app/src/main/java/eu/kanade/domain/track/service/DelayedTrackingUpdateJob.kt index 1f4e246dc..50589ae9d 100644 --- a/app/src/main/java/eu/kanade/domain/track/service/DelayedTrackingUpdateJob.kt +++ b/app/src/main/java/eu/kanade/domain/track/service/DelayedTrackingUpdateJob.kt @@ -45,7 +45,7 @@ class DelayedTrackingUpdateJob(private val context: Context, workerParams: Worke logcat(LogPriority.DEBUG) { "Updating delayed track item: ${track.mangaId}, last chapter read: ${track.lastChapterRead}" } - trackChapter.await(context, track.mangaId, track.lastChapterRead) + trackChapter.await(context, track.mangaId, track.lastChapterRead, setupJobOnFailure = false) } }