Retry a few times if a favorites sync entry times out

This commit is contained in:
Jobobby04 2023-05-08 17:56:27 -04:00
parent efbdab973e
commit 1f84918749
3 changed files with 48 additions and 15 deletions

View File

@ -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 <T : Any> 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 {

View File

@ -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

View File

@ -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 {