From c0fd47b0668224eddc8fbddb6df394a7193e70e2 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 25 Jun 2023 14:31:05 -0400 Subject: [PATCH] Retry DelayedTrackingUpdateJob up to 3 times if all items not updated --- .../track/service/DelayedTrackingUpdateJob.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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 edc1cb1dbf..3959c67071 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 @@ -19,19 +19,24 @@ import tachiyomi.domain.track.interactor.GetTracks import tachiyomi.domain.track.interactor.InsertTrack import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.util.concurrent.TimeUnit +import kotlin.time.Duration.Companion.minutes +import kotlin.time.toJavaDuration class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) { override suspend fun doWork(): Result { + if (runAttemptCount > 3) { + return Result.failure() + } + val getTracks = Injekt.get() val insertTrack = Injekt.get() val trackManager = Injekt.get() val delayedTrackingStore = Injekt.get() - val results = withIOContext { + withIOContext { delayedTrackingStore.getItems() .mapNotNull { val track = getTracks.awaitOne(it.trackId) @@ -40,7 +45,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) } track?.copy(lastChapterRead = it.lastChapterRead.toDouble()) } - .mapNotNull { track -> + .forEach { track -> try { val service = trackManager.getService(track.syncId) if (service != null && service.isLogged) { @@ -49,15 +54,13 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) insertTrack.await(track) } delayedTrackingStore.remove(track.id) - null } catch (e: Exception) { logcat(LogPriority.ERROR, e) - false } } } - return if (results.isNotEmpty()) Result.failure() else Result.success() + return if (delayedTrackingStore.getItems().isEmpty()) Result.success() else Result.retry() } companion object { @@ -70,7 +73,7 @@ class DelayedTrackingUpdateJob(context: Context, workerParams: WorkerParameters) val request = OneTimeWorkRequestBuilder() .setConstraints(constraints) - .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 20, TimeUnit.SECONDS) + .setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 5.minutes.toJavaDuration()) .addTag(TAG) .build()