Улучшена фоновая синхронизация
This commit is contained in:
@@ -16,6 +16,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
class RunStorageSyncUseCase(
|
||||
private val orchestrator: ITaskOrchestrator,
|
||||
private val syncEngine: IStorageSyncEngine,
|
||||
private val syncReadiness: StorageSyncReadiness,
|
||||
) {
|
||||
private val running = AtomicBoolean(false)
|
||||
|
||||
@@ -71,6 +72,7 @@ class RunStorageSyncUseCase(
|
||||
}
|
||||
|
||||
suspend fun runBlocking() {
|
||||
syncReadiness.awaitReady()
|
||||
syncEngine.syncAllGroups()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.github.nullptroma.wallenc.usecases
|
||||
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageSyncGroupStore
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IVaultsManager
|
||||
import kotlinx.coroutines.delay
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
* Ожидание готовности хранилищ перед синхронизацией (холодный старт из WorkManager).
|
||||
*/
|
||||
class StorageSyncReadiness(
|
||||
private val vaultsManager: IVaultsManager,
|
||||
private val groupStore: IStorageSyncGroupStore,
|
||||
private val findStorageUseCase: FindStorageUseCase,
|
||||
) {
|
||||
suspend fun awaitReady(
|
||||
timeoutMs: Long = DEFAULT_TIMEOUT_MS,
|
||||
pollIntervalMs: Long = POLL_INTERVAL_MS,
|
||||
) {
|
||||
val groups = groupStore.getGroups()
|
||||
if (groups.isEmpty()) {
|
||||
return
|
||||
}
|
||||
val requiredUuids = groups.flatMap { it.storageUuids }.toSet()
|
||||
val deadline = System.currentTimeMillis() + timeoutMs
|
||||
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (!isAnyVaultScanning()) {
|
||||
break
|
||||
}
|
||||
delay(pollIntervalMs)
|
||||
}
|
||||
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
if (requiredUuids.all { uuid -> findStorageUseCase.find(uuid) != null }) {
|
||||
return
|
||||
}
|
||||
delay(pollIntervalMs)
|
||||
}
|
||||
}
|
||||
|
||||
private fun isAnyVaultScanning(): Boolean =
|
||||
vaultsManager.vaults.value.any { it.storagesScanInProgress.value }
|
||||
|
||||
private companion object {
|
||||
private const val DEFAULT_TIMEOUT_MS = 120_000L
|
||||
private const val POLL_INTERVAL_MS = 250L
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user