foreground task для фоновой синхронизации

This commit is contained in:
2026-05-22 00:51:29 +03:00
parent 35ba6dd377
commit b00eed901b
5 changed files with 117 additions and 20 deletions

View File

@@ -6,6 +6,7 @@ import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.github.nullptroma.wallenc.domain.tasks.StorageSyncTriggerReason
import com.github.nullptroma.wallenc.usecases.RunStorageSyncUseCase
import com.github.nullptroma.wallenc.usecases.StorageSyncRunOutcome
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import timber.log.Timber
@@ -19,13 +20,19 @@ class StorageSyncWorker @AssistedInject constructor(
override suspend fun doWork(): Result {
Timber.d("Periodic storage sync started (attempt %d)", runAttemptCount)
return runCatching {
syncRunner.runBlocking(StorageSyncTriggerReason.Background)
Timber.d("Periodic storage sync finished")
Result.success()
}.getOrElse { error ->
Timber.w(error, "Periodic storage sync failed")
Result.retry()
return when (val outcome = syncRunner.enqueueAndAwait(StorageSyncTriggerReason.Background)) {
StorageSyncRunOutcome.SkippedAlreadyRunning -> {
Timber.d("Periodic storage sync skipped — already running")
Result.success()
}
StorageSyncRunOutcome.Completed -> {
Timber.d("Periodic storage sync finished")
Result.success()
}
is StorageSyncRunOutcome.Failed -> {
Timber.w(outcome.error, "Periodic storage sync failed")
Result.retry()
}
}
}