Parallel library update (#5519)

* Parallel library update

* Almost forgot the terminal operator
This commit is contained in:
jobobby04 2021-07-08 22:35:32 -04:00 committed by GitHub
parent 67128937ca
commit 341c3d179e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ import eu.kanade.tachiyomi.source.model.toSManga
import eu.kanade.tachiyomi.util.chapter.NoChaptersException import eu.kanade.tachiyomi.util.chapter.NoChaptersException
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay
import eu.kanade.tachiyomi.util.lang.withIOContext
import eu.kanade.tachiyomi.util.prepUpdateCover import eu.kanade.tachiyomi.util.prepUpdateCover
import eu.kanade.tachiyomi.util.shouldDownloadNewChapters import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.storage.getUriCompat
@ -47,6 +48,8 @@ import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.supervisorScope
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -270,15 +273,22 @@ class LibraryUpdateService(
* @return an observable delivering the progress of each update. * @return an observable delivering the progress of each update.
*/ */
suspend fun updateChapterList() { suspend fun updateChapterList() {
val semaphore = Semaphore(5)
val progressCount = AtomicInteger(0) val progressCount = AtomicInteger(0)
val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>() val newUpdates = mutableListOf<Pair<LibraryManga, Array<Chapter>>>()
val failedUpdates = mutableListOf<Pair<Manga, String?>>() val failedUpdates = mutableListOf<Pair<Manga, String?>>()
var hasDownloads = false var hasDownloads = false
val loggedServices by lazy { trackManager.services.filter { it.isLogged } } val loggedServices by lazy { trackManager.services.filter { it.isLogged } }
mangaToUpdate.forEach { manga -> withIOContext {
mangaToUpdate.groupBy { it.source }
.values
.map { mangaInSource ->
async {
semaphore.withPermit {
mangaInSource.forEach { manga ->
if (updateJob?.isActive != true) { if (updateJob?.isActive != true) {
return return@async
} }
notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size) notifier.showProgressNotification(manga, progressCount.andIncrement, mangaToUpdate.size)
@ -308,6 +318,11 @@ class LibraryUpdateService(
updateTrackings(manga, loggedServices) updateTrackings(manga, loggedServices)
} }
} }
}
}
}
.awaitAll()
}
notifier.cancelProgressNotification() notifier.cancelProgressNotification()