Skip to content

Commit

Permalink
Respect when worker was stopped
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
grote committed Feb 22, 2024
1 parent ae8a950 commit 82e7057
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -99,13 +104,13 @@ class AppBackupWorker(
var result: Result = Result.success()
try {
Log.i(TAG, "Starting APK backup...")
apkBackupManager.backup()
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) {
val requestSuccess = if (!isStopped && backupRequester.isBackupEnabled) {
Log.d(TAG, "Backup is enabled, request backup...")
backupRequester.requestBackup()
} else true
Expand Down

0 comments on commit 82e7057

Please sign in to comment.