From 6e7bc89e2f98a6e8487861c3abb8c12605336de4 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 22 Feb 2024 09:15:16 -0300 Subject: [PATCH] Respect when worker was stopped and log worker ID as well as object, because we've seen two scheduled workers running at the same time, requesting a backup at the same time. This should not happen. --- .../seedvault/worker/AppBackupWorker.kt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt index 96ac383f..32c44a24 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/worker/AppBackupWorker.kt @@ -78,13 +78,18 @@ class AppBackupWorker( private val nm: BackupNotificationManager by inject() override suspend fun doWork(): Result { + Log.i(TAG, "Start worker $this ($id)") try { setForeground(createForegroundInfo()) } catch (e: Exception) { Log.e(TAG, "Error while running setForeground: ", e) } return try { - doBackup() + if (isStopped) { + Result.retry() + } else { + doBackup() + } } finally { // schedule next backup, because the old one gets lost // when scheduling a OneTimeWorkRequest with the same unique name via scheduleNow() @@ -98,17 +103,18 @@ class AppBackupWorker( private suspend fun doBackup(): Result { var result: Result = Result.success() try { - Log.i(TAG, "Starting APK backup...") - apkBackupManager.backup() + Log.i(TAG, "Starting APK backup... (stopped: $isStopped)") + if (!isStopped) apkBackupManager.backup() } catch (e: Exception) { Log.e(TAG, "Error backing up APKs: ", e) result = Result.retry() } finally { - Log.i(TAG, "Requesting app data backup...") - val requestSuccess = if (backupRequester.isBackupEnabled) { + Log.i(TAG, "Requesting app data backup... (stopped: $isStopped)") + val requestSuccess = if (!isStopped && backupRequester.isBackupEnabled) { Log.d(TAG, "Backup is enabled, request backup...") backupRequester.requestBackup() } else true + Log.d(TAG, "Have requested backup.") if (!requestSuccess) result = Result.retry() } return result