From e7a0042d7cdb36931c78f4608e00c0c163c0148b Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Tue, 29 Aug 2023 15:30:04 +0300 Subject: [PATCH] Fix the Github automatic backup --- .../backup/github/GitHubAutoBackupManager.kt | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/ivy/wallet/backup/github/GitHubAutoBackupManager.kt b/app/src/main/java/com/ivy/wallet/backup/github/GitHubAutoBackupManager.kt index 7b3ac7e9c9..2ad26cb966 100644 --- a/app/src/main/java/com/ivy/wallet/backup/github/GitHubAutoBackupManager.kt +++ b/app/src/main/java/com/ivy/wallet/backup/github/GitHubAutoBackupManager.kt @@ -2,10 +2,12 @@ package com.ivy.wallet.backup.github import android.content.Context import androidx.hilt.work.HiltWorker +import androidx.work.BackoffPolicy import androidx.work.Constraints import androidx.work.CoroutineWorker import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.NetworkType +import androidx.work.PeriodicWorkRequest import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import androidx.work.WorkerParameters @@ -20,7 +22,6 @@ class GitHubAutoBackupManager @Inject constructor( @ApplicationContext private val context: Context ) { - private val uniqueWorkName = "GITHUB_AUTO_BACKUP_WORK" fun scheduleAutoBackups() { @@ -34,6 +35,11 @@ class GitHubAutoBackupManager @Inject constructor( .setRequiredNetworkType(NetworkType.CONNECTED) .build() ) + .setBackoffCriteria( + BackoffPolicy.EXPONENTIAL, + PeriodicWorkRequest.MIN_BACKOFF_MILLIS, + TimeUnit.MILLISECONDS + ) .build() WorkManager.getInstance(context) @@ -74,13 +80,20 @@ class GitHubBackupWorker @AssistedInject constructor( @Assisted params: WorkerParameters, private val gitHubBackup: GitHubBackup, ) : CoroutineWorker(appContext, params) { + companion object { + const val MAX_RETRIES = 7 + } override suspend fun doWork(): Result { return gitHubBackup.backupData( isAutomatic = true ).fold( ifLeft = { - Result.failure() + if (runAttemptCount <= MAX_RETRIES) { + Result.retry() + } else { + Result.failure() + } }, ifRight = { Result.success()