Use semaphores instead of channels where possible
Use correct starting title in auto-migrator
This commit is contained in:
parent
d04ffb7ea9
commit
7ec18861bf
@ -16,14 +16,16 @@ import exh.ui.base.BaseExhController
|
||||
import exh.util.await
|
||||
import kotlinx.android.synthetic.main.eh_migration_process.*
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import rx.schedulers.Schedulers
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
class MigrationProcedureController(bundle: Bundle? = null) : BaseExhController(bundle), CoroutineScope {
|
||||
override val layoutId = R.layout.eh_migration_process
|
||||
|
||||
private var titleText = "Migrate manga (1/300)"
|
||||
private var titleText = "Migrate manga"
|
||||
|
||||
private var adapter: MigrationProcedureAdapter? = null
|
||||
|
||||
@ -108,20 +110,12 @@ class MigrationProcedureController(bundle: Bundle? = null) : BaseExhController(b
|
||||
it.id != mangaSource.id
|
||||
}
|
||||
if(config.useSourceWithMostChapters) {
|
||||
val sourceQueue = Channel<CatalogueSource>(Channel.RENDEZVOUS)
|
||||
launch {
|
||||
validSources.forEachIndexed { index, catalogueSource ->
|
||||
sourceQueue.send(catalogueSource)
|
||||
manga.progress.send(validSources.size to index)
|
||||
}
|
||||
sourceQueue.close()
|
||||
}
|
||||
val sourceSemaphore = Semaphore(3)
|
||||
val processedSources = AtomicInteger()
|
||||
|
||||
val results = mutableListOf<Pair<Manga, Int>>()
|
||||
|
||||
(1 .. 3).map {
|
||||
launch {
|
||||
for(source in sourceQueue) {
|
||||
validSources.map { source ->
|
||||
async {
|
||||
sourceSemaphore.withPermit {
|
||||
try {
|
||||
supervisorScope {
|
||||
val searchResult = if (config.enableLenientSearch) {
|
||||
@ -133,17 +127,19 @@ class MigrationProcedureController(bundle: Bundle? = null) : BaseExhController(b
|
||||
if(searchResult != null) {
|
||||
val localManga = smartSearchEngine.networkToLocalManga(searchResult, source.id)
|
||||
val chapters = source.fetchChapterList(localManga).toSingle().await(Schedulers.io())
|
||||
results += localManga to chapters.size
|
||||
manga.progress.send(validSources.size to processedSources.incrementAndGet())
|
||||
localManga to chapters.size
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
logger.e("Failed to search in source: ${source.id}!", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}.forEach { it.join() }
|
||||
|
||||
results.maxBy { it.second }?.first
|
||||
}.mapNotNull { it.await() }.maxBy { it.second }?.first
|
||||
} else {
|
||||
validSources.forEachIndexed { index, source ->
|
||||
val searchResult = try {
|
||||
|
Loading…
x
Reference in New Issue
Block a user