修复校验超时判断

This commit is contained in:
kunfei
2022-02-14 17:28:37 +08:00
parent 4cee457074
commit 8102a2a26d
2 changed files with 9 additions and 9 deletions
@@ -143,10 +143,11 @@ class Coroutine<T>(
val value = executeBlock(scope, context, timeMillis ?: 0L, block) val value = executeBlock(scope, context, timeMillis ?: 0L, block)
ensureActive() ensureActive()
success?.let { dispatchCallback(this, value, it) } success?.let { dispatchCallback(this, value, it) }
} catch (e: CancellationException) {
Timber.e("任务取消")
} catch (e: Throwable) { } catch (e: Throwable) {
Timber.e(e) Timber.e(e)
if (e is CancellationException && e !is TimeoutCancellationException) {
return@launch
}
val consume: Boolean = errorReturn?.value?.let { value -> val consume: Boolean = errorReturn?.value?.let { value ->
success?.let { dispatchCallback(this, value, it) } success?.let { dispatchCallback(this, value, it) }
true true
@@ -8,11 +8,9 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.* import io.legado.app.model.*
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.book.source.manage.BookSourceActivity
@@ -23,14 +21,17 @@ import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
/**
* 校验书源
*/
class CheckSourceService : BaseService() { class CheckSourceService : BaseService() {
private var threadCount = AppConfig.threadCount private var threadCount = AppConfig.threadCount
private var searchCoroutine = private var searchCoroutine =
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher() Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
private var tasks = CompositeCoroutine()
private val allIds = ArrayList<String>() private val allIds = ArrayList<String>()
private val checkedIds = ArrayList<String>() private val checkedIds = ArrayList<String>()
private var processIndex = 0 private var processIndex = 0
@@ -71,7 +72,6 @@ class CheckSourceService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
Debug.finishChecking() Debug.finishChecking()
tasks.clear()
searchCoroutine.close() searchCoroutine.close()
postEvent(EventBus.CHECK_SOURCE_DONE, 0) postEvent(EventBus.CHECK_SOURCE_DONE, 0)
} }
@@ -81,7 +81,6 @@ class CheckSourceService : BaseService() {
toastOnUi("已有书源在校验,等完成后再试") toastOnUi("已有书源在校验,等完成后再试")
return return
} }
tasks.clear()
allIds.clear() allIds.clear()
checkedIds.clear() checkedIds.clear()
allIds.addAll(ids) allIds.addAll(ids)
@@ -102,7 +101,7 @@ class CheckSourceService : BaseService() {
synchronized(this) { synchronized(this) {
processIndex++ processIndex++
} }
execute(context = searchCoroutine) { launch(IO) {
if (index < allIds.size) { if (index < allIds.size) {
val sourceUrl = allIds[index] val sourceUrl = allIds[index]
appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source -> appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source ->