From 1f849187491a0dad4380706f7c749939f5096167 Mon Sep 17 00:00:00 2001 From: Jobobby04 Date: Mon, 8 May 2023 17:56:27 -0400 Subject: [PATCH] Retry a few times if a favorites sync entry times out --- app/src/main/java/exh/GalleryAdder.kt | 35 ++++++++++++++++--- .../java/exh/favorites/FavoritesSyncHelper.kt | 19 +++++----- .../exh/ui/batchadd/BatchAddScreenModel.kt | 9 ++++- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/exh/GalleryAdder.kt b/app/src/main/java/exh/GalleryAdder.kt index 2c7d80416..aff785dbc 100755 --- a/app/src/main/java/exh/GalleryAdder.kt +++ b/app/src/main/java/exh/GalleryAdder.kt @@ -59,6 +59,7 @@ class GalleryAdder( fav: Boolean = false, forceSource: UrlImportableSource? = null, throttleFunc: suspend () -> Unit = {}, + retry: Int = 1 ): GalleryAddEvent { logger.d(context.getString(R.string.gallery_adder_importing_gallery, url, fav.toString(), forceSource)) try { @@ -138,7 +139,7 @@ class GalleryAdder( ) // Fetch and copy details - val newManga = source.getMangaDetails(manga.toSManga()) + val newManga = retry(retry) { source.getMangaDetails(manga.toSManga()) } updateManga.awaitUpdateFromSource(manga, newManga, false) manga = getManga.await(manga.id)!! @@ -149,10 +150,12 @@ class GalleryAdder( // Fetch and copy chapters try { - val chapterList = if (source is EHentai) { - source.getChapterList(manga.toSManga(), throttleFunc) - } else { - source.getChapterList(manga.toSManga()) + val chapterList = retry(retry) { + if (source is EHentai) { + source.getChapterList(manga.toSManga(), throttleFunc) + } else { + source.getChapterList(manga.toSManga()) + } } if (chapterList.isNotEmpty()) { @@ -186,6 +189,28 @@ class GalleryAdder( ) } } + + private inline fun retry(retryCount: Int, block: () -> T): T { + var result: T? = null + var lastError: Exception? = null + + for (i in 1..retryCount) { + try { + result = block() + } catch (e: Exception) { + if (e is EHentai.GalleryNotFoundException) { + throw e + } + lastError = e + } + } + + if (lastError != null) { + throw lastError + } + + return result!! + } } sealed class GalleryAddEvent { diff --git a/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt b/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt index 9d57e2aa6..8374ce7a1 100644 --- a/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt +++ b/app/src/main/java/exh/favorites/FavoritesSyncHelper.kt @@ -246,7 +246,7 @@ class FavoritesSyncHelper(val context: Context) { errorList += errorString } else { status.value = FavoritesSyncStatus.Error(errorString) - throw IgnoredException() + throw IgnoredException(errorString) } } } @@ -296,7 +296,7 @@ class FavoritesSyncHelper(val context: Context) { errorList += errorString } else { status.value = FavoritesSyncStatus.Error(errorString) - throw IgnoredException() + throw IgnoredException(errorString) } } } @@ -361,11 +361,12 @@ class FavoritesSyncHelper(val context: Context) { // Import using gallery adder val result = galleryAdder.addGallery( - context, - "${exh.baseUrl}${it.getUrl()}", - true, - exh, - throttleManager::throttle, + context = context, + url = "${exh.baseUrl}${it.getUrl()}", + fav = true, + forceSource = exh, + throttleFunc = throttleManager::throttle, + retry = 3 ) if (result is GalleryAddEvent.Fail) { @@ -385,7 +386,7 @@ class FavoritesSyncHelper(val context: Context) { errorList += errorString } else { status.value = FavoritesSyncStatus.Error(errorString) - throw IgnoredException() + throw IgnoredException(errorString) } } else if (result is GalleryAddEvent.Success) { insertedMangaCategories += categories[it.category].id to result.manga @@ -401,7 +402,7 @@ class FavoritesSyncHelper(val context: Context) { private fun needWarnThrottle() = throttleManager.throttleTime >= THROTTLE_WARN - class IgnoredException : RuntimeException() + class IgnoredException(message: String) : RuntimeException(message) companion object { private val THROTTLE_WARN = 1.seconds diff --git a/app/src/main/java/exh/ui/batchadd/BatchAddScreenModel.kt b/app/src/main/java/exh/ui/batchadd/BatchAddScreenModel.kt index 4cd37ec1f..a4a25a71c 100644 --- a/app/src/main/java/exh/ui/batchadd/BatchAddScreenModel.kt +++ b/app/src/main/java/exh/ui/batchadd/BatchAddScreenModel.kt @@ -68,7 +68,14 @@ class BatchAddScreenModel( splitGalleries.forEachIndexed { i, s -> ensureActive() - val result = withIOContext { galleryAdder.addGallery(context, s, true) } + val result = withIOContext { + galleryAdder.addGallery( + context = context, + url = s, + fav = true, + retry = 2 + ) + } if (result is GalleryAddEvent.Success) { succeeded.add(s) } else {