WorkManager 的 OneTimeWorkRequest 的重试策略/机制是什么
What are the retry strategy/ mechanism for WorkManager's OneTimeWorkRequest
我有以下的临时工
// Create a Constraints that defines when the task should run
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresBatteryNotLow(true)
// Many other constraints are available, see the
// Constraints.Builder reference
.build();
OneTimeWorkRequest oneTimeWorkRequest =
new OneTimeWorkRequest.Builder(SyncWorker.class)
.setConstraints(constraints)
.addTag(SyncWorker.TAG)
.build();
根据https://developer.android.com/topic/libraries/architecture/workmanager
// (Returning RETRY tells WorkManager to try this task again
// later; FAILURE says not to try again.)
我在想,如果SyncWorker
一直返回RETRY
,WorkManager
的重试策略是什么?例如,WorkManager
的最大重试次数是多少?文档对此并不清楚。
默认为BackoffPolicy.EXPONENTIAL
。
我们仅在您通过返回 WorkerResult.RETRY
要求我们 RETRY
时或当您的 Worker
所需的约束现在未满足时才重试。所以例如如果您需要 NETWORK
约束,现在设备丢失了其活动的 Network
连接 - 那么 Worker
将停止并自动重试(当满足约束时)。
有关详细信息,请查看 docs。
下面的示例在退出前在捕捉到异常时重试 3 次。
class RepeatWorker(context : Context, params : WorkerParameters)
: Worker(context, params) {
private fun doSomeThing() {
// do something
}
override fun doWork(): Result {
if (runAttemptCount > 3) {
return Result.failure()
}
try {
doSomeThing()
}
catch (e: Exception) {
e.printStackTrace()
return Result.retry()
}
return Result.success()
}
}
注意:默认 BackoffPolicy 是指数的,其中 30 秒后第一次重试(最短重试周期为 10 秒,最大重试周期不超过 18000 秒/5 小时)。
fun start() : LiveData<WorkInfo> {
val WORK_NAME = "SingleBackupWorker"
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val work = OneTimeWorkRequestBuilder<BackupWorker>()
.setConstraints(constraints)
.setInitialDelay(5, TimeUnit.SECONDS)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
WorkManager.getInstance().enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, work)
return WorkManager.getInstance().getWorkInfoByIdLiveData(work.id)
}
从 runAttemptCount
获取任何工作的当前 运行 尝试计数。请注意,对于周期性工作,此值会在周期之间重置。 Link:-
https://developer.android.com/reference/androidx/work/ListenableWorker#getRunAttemptCount()
示例:
override fun doWork(): Result {
if (runAttemptCount < maxRetryConstantIWant) {
.....
.....
.....
} else { Result.Failure }
}
这里runAttemptCount
是worker方法
我有以下的临时工
// Create a Constraints that defines when the task should run
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)
.setRequiresBatteryNotLow(true)
// Many other constraints are available, see the
// Constraints.Builder reference
.build();
OneTimeWorkRequest oneTimeWorkRequest =
new OneTimeWorkRequest.Builder(SyncWorker.class)
.setConstraints(constraints)
.addTag(SyncWorker.TAG)
.build();
根据https://developer.android.com/topic/libraries/architecture/workmanager
// (Returning RETRY tells WorkManager to try this task again
// later; FAILURE says not to try again.)
我在想,如果SyncWorker
一直返回RETRY
,WorkManager
的重试策略是什么?例如,WorkManager
的最大重试次数是多少?文档对此并不清楚。
默认为BackoffPolicy.EXPONENTIAL
。
我们仅在您通过返回 WorkerResult.RETRY
要求我们 RETRY
时或当您的 Worker
所需的约束现在未满足时才重试。所以例如如果您需要 NETWORK
约束,现在设备丢失了其活动的 Network
连接 - 那么 Worker
将停止并自动重试(当满足约束时)。
有关详细信息,请查看 docs。
下面的示例在退出前在捕捉到异常时重试 3 次。
class RepeatWorker(context : Context, params : WorkerParameters)
: Worker(context, params) {
private fun doSomeThing() {
// do something
}
override fun doWork(): Result {
if (runAttemptCount > 3) {
return Result.failure()
}
try {
doSomeThing()
}
catch (e: Exception) {
e.printStackTrace()
return Result.retry()
}
return Result.success()
}
}
注意:默认 BackoffPolicy 是指数的,其中 30 秒后第一次重试(最短重试周期为 10 秒,最大重试周期不超过 18000 秒/5 小时)。
fun start() : LiveData<WorkInfo> {
val WORK_NAME = "SingleBackupWorker"
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val work = OneTimeWorkRequestBuilder<BackupWorker>()
.setConstraints(constraints)
.setInitialDelay(5, TimeUnit.SECONDS)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 1, TimeUnit.MINUTES)
.build()
WorkManager.getInstance().enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, work)
return WorkManager.getInstance().getWorkInfoByIdLiveData(work.id)
}
从 runAttemptCount
获取任何工作的当前 运行 尝试计数。请注意,对于周期性工作,此值会在周期之间重置。 Link:-
https://developer.android.com/reference/androidx/work/ListenableWorker#getRunAttemptCount()
示例:
override fun doWork(): Result {
if (runAttemptCount < maxRetryConstantIWant) {
.....
.....
.....
} else { Result.Failure }
}
这里runAttemptCount
是worker方法