Причина синхронизации и временная метка в логах
This commit is contained in:
@@ -3,6 +3,7 @@ package com.github.nullptroma.wallenc.usecases
|
||||
import com.github.nullptroma.wallenc.domain.errors.toWallencException
|
||||
import com.github.nullptroma.wallenc.domain.interfaces.IStorageSyncEngine
|
||||
import com.github.nullptroma.wallenc.domain.tasks.ITaskOrchestrator
|
||||
import com.github.nullptroma.wallenc.domain.tasks.StorageSyncTriggerReason
|
||||
import com.github.nullptroma.wallenc.domain.tasks.TaskId
|
||||
import com.github.nullptroma.wallenc.domain.tasks.TaskLogKey
|
||||
import com.github.nullptroma.wallenc.domain.tasks.TaskLogLevel
|
||||
@@ -31,10 +32,10 @@ class RunStorageSyncUseCase @Inject constructor(
|
||||
|
||||
/**
|
||||
* @param displayTitle заголовок задачи в UI (локализованный на стороне вызова)
|
||||
* @param logReason техническая метка для логов (не для UI)
|
||||
* @param reason источник запуска — попадает в лог пайплайна
|
||||
* @return false, если синхронизация уже в очереди или выполняется — новая задача не создана
|
||||
*/
|
||||
fun enqueue(displayTitle: String, logReason: String): Boolean {
|
||||
fun enqueue(displayTitle: String, reason: StorageSyncTriggerReason): Boolean {
|
||||
if (!running.compareAndSet(false, true)) {
|
||||
return false
|
||||
}
|
||||
@@ -45,36 +46,68 @@ class RunStorageSyncUseCase @Inject constructor(
|
||||
dispatcher = Dispatchers.IO,
|
||||
work = { ctx ->
|
||||
try {
|
||||
ctx.log(TaskLogLevel.Info, TaskLogKey.SyncStarted)
|
||||
ctx.reportProgress(null, TaskProgressLabel.SyncStarted)
|
||||
syncEngine.syncAllGroups { fraction, label ->
|
||||
ctx.reportProgress(fraction, label)
|
||||
}
|
||||
ctx.log(TaskLogLevel.Info, TaskLogKey.SyncFinished)
|
||||
ctx.reportProgress(null, TaskProgressLabel.SyncCompleted)
|
||||
executeSync(
|
||||
reason = reason,
|
||||
reportProgress = { fraction, label ->
|
||||
ctx.reportProgress(fraction, label)
|
||||
},
|
||||
log = { level, key -> ctx.log(level, key) },
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
val err = e.toWallencException()
|
||||
ctx.log(TaskLogLevel.Error, TaskLogKey.SyncFailed(err))
|
||||
ctx.fail(err)
|
||||
ctx.fail(e.toWallencException())
|
||||
} finally {
|
||||
running.set(false)
|
||||
_syncRunning.value = false
|
||||
_activeSyncTaskId.value = null
|
||||
clearRunningState()
|
||||
}
|
||||
},
|
||||
)
|
||||
_activeSyncTaskId.value = taskId
|
||||
return true
|
||||
} catch (t: Throwable) {
|
||||
running.set(false)
|
||||
_syncRunning.value = false
|
||||
_activeSyncTaskId.value = null
|
||||
clearRunningState()
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun runBlocking() {
|
||||
suspend fun runBlocking(reason: StorageSyncTriggerReason) {
|
||||
if (!running.compareAndSet(false, true)) {
|
||||
return
|
||||
}
|
||||
_syncRunning.value = true
|
||||
try {
|
||||
executeSync(
|
||||
reason = reason,
|
||||
reportProgress = { _, _ -> },
|
||||
log = { level, key -> orchestrator.appendPipelineLog(level, key) },
|
||||
)
|
||||
} finally {
|
||||
clearRunningState()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun executeSync(
|
||||
reason: StorageSyncTriggerReason,
|
||||
reportProgress: suspend (fraction: Float?, label: TaskProgressLabel?) -> Unit,
|
||||
log: (TaskLogLevel, TaskLogKey) -> Unit,
|
||||
) {
|
||||
syncReadiness.awaitReady()
|
||||
syncEngine.syncAllGroups()
|
||||
log(TaskLogLevel.Info, TaskLogKey.SyncStarted(reason))
|
||||
reportProgress(null, TaskProgressLabel.SyncStarted)
|
||||
try {
|
||||
syncEngine.syncAllGroups { fraction, label ->
|
||||
reportProgress(fraction, label)
|
||||
}
|
||||
log(TaskLogLevel.Info, TaskLogKey.SyncFinished(reason))
|
||||
reportProgress(null, TaskProgressLabel.SyncCompleted)
|
||||
} catch (e: Exception) {
|
||||
val err = e.toWallencException()
|
||||
log(TaskLogLevel.Error, TaskLogKey.SyncFailed(err, reason))
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
private fun clearRunningState() {
|
||||
running.set(false)
|
||||
_syncRunning.value = false
|
||||
_activeSyncTaskId.value = null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user