Fix DelayedTrackingUpdateJob spam on update errors

* 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.

Co-authored-by: MajorTanya <39014446+MajorTanya@users.noreply.github.com>
This commit is contained in:
Secozzi 2024-06-06 00:56:22 +02:00
parent dcd34b53bb
commit af7471e446
No known key found for this signature in database
GPG key ID: 71E9C97D8DDC2662
4 changed files with 10 additions and 6 deletions

View file

@ -22,7 +22,7 @@ class TrackEpisode(
private val delayedTrackingStore: DelayedAnimeTrackingStore,
) {
suspend fun await(context: Context, animeId: Long, episodeNumber: Double) {
suspend fun await(context: Context, animeId: Long, episodeNumber: Double, setupJobOnFailure: Boolean = true) {
withNonCancellableContext {
val tracks = getTracks.await(animeId)
if (tracks.isEmpty()) return@withNonCancellableContext
@ -44,7 +44,9 @@ class TrackEpisode(
delayedTrackingStore.removeAnimeItem(track.id)
} else {
delayedTrackingStore.addAnime(track.id, episodeNumber)
DelayedAnimeTrackingUpdateJob.setupTask(context)
if (setupJobOnFailure) {
DelayedAnimeTrackingUpdateJob.setupTask(context)
}
}
}
}

View file

@ -47,7 +47,7 @@ class DelayedAnimeTrackingUpdateJob(private val context: Context, workerParams:
"Updating delayed track item: ${animeTrack.animeId}" +
", last chapter read: ${animeTrack.lastEpisodeSeen}"
}
trackEpisode.await(context, animeTrack.animeId, animeTrack.lastEpisodeSeen)
trackEpisode.await(context, animeTrack.animeId, animeTrack.lastEpisodeSeen, setupJobOnFailure = false)
}
}

View file

@ -21,7 +21,7 @@ class TrackChapter(
private val delayedTrackingStore: DelayedMangaTrackingStore,
) {
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
@ -45,7 +45,9 @@ class TrackChapter(
delayedTrackingStore.removeMangaItem(track.id)
} catch (e: Exception) {
delayedTrackingStore.addManga(track.id, chapterNumber)
DelayedMangaTrackingUpdateJob.setupTask(context)
if (setupJobOnFailure) {
DelayedMangaTrackingUpdateJob.setupTask(context)
}
throw e
}
}

View file

@ -45,7 +45,7 @@ class DelayedMangaTrackingUpdateJob(private val context: Context, workerParams:
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)
}
}