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.
This commit is contained in:
parent
0d7156789e
commit
6e7bc89e2f
1 changed files with 11 additions and 5 deletions
|
@ -78,13 +78,18 @@ class AppBackupWorker(
|
||||||
private val nm: BackupNotificationManager by inject()
|
private val nm: BackupNotificationManager by inject()
|
||||||
|
|
||||||
override suspend fun doWork(): Result {
|
override suspend fun doWork(): Result {
|
||||||
|
Log.i(TAG, "Start worker $this ($id)")
|
||||||
try {
|
try {
|
||||||
setForeground(createForegroundInfo())
|
setForeground(createForegroundInfo())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Error while running setForeground: ", e)
|
Log.e(TAG, "Error while running setForeground: ", e)
|
||||||
}
|
}
|
||||||
return try {
|
return try {
|
||||||
doBackup()
|
if (isStopped) {
|
||||||
|
Result.retry()
|
||||||
|
} else {
|
||||||
|
doBackup()
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
// schedule next backup, because the old one gets lost
|
// schedule next backup, because the old one gets lost
|
||||||
// when scheduling a OneTimeWorkRequest with the same unique name via scheduleNow()
|
// when scheduling a OneTimeWorkRequest with the same unique name via scheduleNow()
|
||||||
|
@ -98,17 +103,18 @@ class AppBackupWorker(
|
||||||
private suspend fun doBackup(): Result {
|
private suspend fun doBackup(): Result {
|
||||||
var result: Result = Result.success()
|
var result: Result = Result.success()
|
||||||
try {
|
try {
|
||||||
Log.i(TAG, "Starting APK backup...")
|
Log.i(TAG, "Starting APK backup... (stopped: $isStopped)")
|
||||||
apkBackupManager.backup()
|
if (!isStopped) apkBackupManager.backup()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Error backing up APKs: ", e)
|
Log.e(TAG, "Error backing up APKs: ", e)
|
||||||
result = Result.retry()
|
result = Result.retry()
|
||||||
} finally {
|
} finally {
|
||||||
Log.i(TAG, "Requesting app data backup...")
|
Log.i(TAG, "Requesting app data backup... (stopped: $isStopped)")
|
||||||
val requestSuccess = if (backupRequester.isBackupEnabled) {
|
val requestSuccess = if (!isStopped && backupRequester.isBackupEnabled) {
|
||||||
Log.d(TAG, "Backup is enabled, request backup...")
|
Log.d(TAG, "Backup is enabled, request backup...")
|
||||||
backupRequester.requestBackup()
|
backupRequester.requestBackup()
|
||||||
} else true
|
} else true
|
||||||
|
Log.d(TAG, "Have requested backup.")
|
||||||
if (!requestSuccess) result = Result.retry()
|
if (!requestSuccess) result = Result.retry()
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Reference in a new issue