forked from seedvault-app/seedvault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use our own scheduling when doing d2d backups (experimental)
- Loading branch information
Showing
7 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
app/src/main/java/com/stevesoltys/seedvault/BackupWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 The Calyx Institute | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.stevesoltys.seedvault | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.work.BackoffPolicy | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingPeriodicWorkPolicy.UPDATE | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.Worker | ||
import androidx.work.WorkerParameters | ||
import com.stevesoltys.seedvault.transport.requestBackup | ||
import java.util.Date | ||
import java.util.concurrent.TimeUnit | ||
|
||
class BackupWorker( | ||
appContext: Context, | ||
workerParams: WorkerParameters, | ||
) : Worker(appContext, workerParams) { | ||
|
||
companion object { | ||
private const val UNIQUE_WORK_NAME = "APP_BACKUP" | ||
|
||
fun schedule(appContext: Context) { | ||
val backupConstraints = Constraints.Builder() | ||
.setRequiredNetworkType(NetworkType.UNMETERED) | ||
.setRequiresCharging(true) | ||
.build() | ||
val backupWorkRequest = PeriodicWorkRequestBuilder<BackupWorker>( | ||
repeatInterval = 24, | ||
repeatIntervalTimeUnit = TimeUnit.HOURS, | ||
flexTimeInterval = 2, | ||
flexTimeIntervalUnit = TimeUnit.HOURS, | ||
).setConstraints(backupConstraints) | ||
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.HOURS) | ||
.build() | ||
val workManager = WorkManager.getInstance(appContext) | ||
workManager.enqueueUniquePeriodicWork(UNIQUE_WORK_NAME, UPDATE, backupWorkRequest) | ||
} | ||
|
||
fun unschedule(appContext: Context) { | ||
val workManager = WorkManager.getInstance(appContext) | ||
workManager.cancelUniqueWork(UNIQUE_WORK_NAME) | ||
} | ||
|
||
fun logWorkInfo(appContext: Context) { | ||
val workManager = WorkManager.getInstance(appContext) | ||
workManager.getWorkInfosForUniqueWork(UNIQUE_WORK_NAME).get().forEach { | ||
Log.e( | ||
"BackupWorker", " ${it.state.name} - ${Date(it.nextScheduleTimeMillis)} - " + | ||
"runAttempts: ${it.runAttemptCount}" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun doWork(): Result { | ||
return if (requestBackup(applicationContext)) Result.success() | ||
else Result.retry() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters