Parallel cover update job

(cherry picked from commit 015e8deb799a652b923649d58f329ad78d69d2b9)
This commit is contained in:
arkon 2021-07-09 17:49:46 -04:00 committed by Jobobby04
parent a844c636a6
commit 5a1f09e904

View File

@ -68,6 +68,9 @@ import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
/**
* This class will take care of updating the chapters of the manga from the library. It can be
@ -351,11 +354,11 @@ class LibraryUpdateService(
*/
suspend fun updateChapterList() {
val semaphore = Semaphore(5)
var progressCount = 0
val currentlyUpdatingManga = mutableListOf<LibraryManga>()
val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
val failedUpdates = mutableListOf<Pair<Manga, String?>>()
var hasDownloads = false
val progressCount = AtomicInteger(0)
val currentlyUpdatingManga = CopyOnWriteArrayList<LibraryManga>()
val newUpdates = CopyOnWriteArrayList<Pair<LibraryManga, Array<Chapter>>>()
val failedUpdates = CopyOnWriteArrayList<Pair<Manga, String?>>()
val hasDownloads = AtomicBoolean(false)
val loggedServices by lazy { trackManager.services.filter { it.isLogged } }
withIOContext {
@ -371,10 +374,10 @@ class LibraryUpdateService(
}
currentlyUpdatingManga.add(manga)
progressCount++
progressCount.andIncrement
notifier.showProgressNotification(
currentlyUpdatingManga,
progressCount,
progressCount.get(),
mangaToUpdate.size
)
@ -384,7 +387,7 @@ class LibraryUpdateService(
if (newChapters.isNotEmpty()) {
if (manga.shouldDownloadNewChapters(db, preferences)) {
downloadChapters(manga, newChapters)
hasDownloads = true
hasDownloads.set(true)
}
// Convert to the manga that contains new chapters
@ -409,7 +412,7 @@ class LibraryUpdateService(
currentlyUpdatingManga.remove(manga)
notifier.showProgressNotification(
currentlyUpdatingManga,
progressCount,
progressCount.get(),
mangaToUpdate.size
)
}
@ -422,7 +425,7 @@ class LibraryUpdateService(
if (newUpdates.isNotEmpty()) {
notifier.showUpdateNotifications(newUpdates)
if (hasDownloads) {
if (hasDownloads.get()) {
DownloadService.start(this)
}
}
@ -503,29 +506,56 @@ class LibraryUpdateService(
}
private suspend fun updateCovers() {
var progressCount = 0
val semaphore = Semaphore(5)
val progressCount = AtomicInteger(0)
val currentlyUpdatingManga = CopyOnWriteArrayList<LibraryManga>()
mangaToUpdate.forEach { manga ->
if (updateJob?.isActive != true) {
return
}
withIOContext {
mangaToUpdate.groupBy { it.source }
.values
.map { mangaInSource ->
async {
semaphore.withPermit {
mangaInSource.forEach { manga ->
if (updateJob?.isActive != true) {
return@async
}
notifier.showProgressNotification(listOf(manga), progressCount++, mangaToUpdate.size)
currentlyUpdatingManga.add(manga)
progressCount.andIncrement
notifier.showProgressNotification(
currentlyUpdatingManga,
progressCount.get(),
mangaToUpdate.size
)
sourceManager.get(manga.source)?.let { source ->
try {
val networkManga = source.getMangaDetails(manga.toMangaInfo())
val sManga = networkManga.toSManga()
manga.prepUpdateCover(coverCache, sManga, true)
sManga.thumbnail_url?.let {
manga.thumbnail_url = it
db.insertManga(manga).executeAsBlocking()
sourceManager.get(manga.source)?.let { source ->
try {
val networkManga =
source.getMangaDetails(manga.toMangaInfo())
val sManga = networkManga.toSManga()
manga.prepUpdateCover(coverCache, sManga, true)
sManga.thumbnail_url?.let {
manga.thumbnail_url = it
db.insertManga(manga).executeAsBlocking()
}
} catch (e: Throwable) {
// Ignore errors and continue
Timber.e(e)
}
}
currentlyUpdatingManga.remove(manga)
notifier.showProgressNotification(
currentlyUpdatingManga,
progressCount.get(),
mangaToUpdate.size
)
}
}
}
} catch (e: Throwable) {
// Ignore errors and continue
Timber.e(e)
}
}
.awaitAll()
}
coverCache.clearMemoryCache()