Skip to content

Commit

Permalink
Improved setting backoff policy
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Feb 17, 2022
1 parent 1081a19 commit fc13750
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.odk.collect.async

import androidx.work.BackoffPolicy
import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingPeriodicWorkPolicy
Expand Down Expand Up @@ -43,31 +42,16 @@ class CoroutineAndWorkManagerScheduler(foregroundContext: CoroutineContext, back
val workManagerInputData = Data.Builder().putAll(inputData).build()

val worker = spec.getWorkManagerAdapter()
val workRequest = PeriodicWorkRequest.Builder(worker, repeatPeriod, TimeUnit.MILLISECONDS)
val builder = PeriodicWorkRequest.Builder(worker, repeatPeriod, TimeUnit.MILLISECONDS)
.addTag(tag)
.setInputData(workManagerInputData)
.setConstraints(constraints)
.build()

workManager.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.REPLACE, workRequest)
}

override fun networkDeferred(tag: String, spec: TaskSpec, repeatPeriod: Long, backoffPolicy: BackoffPolicy, backoffDelay: Long, inputData: Map<String, String>) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val workManagerInputData = Data.Builder().putAll(inputData).build()

val worker = spec.getWorkManagerAdapter()
val workRequest = PeriodicWorkRequest.Builder(worker, repeatPeriod, TimeUnit.MILLISECONDS)
.setBackoffCriteria(backoffPolicy, backoffDelay, TimeUnit.MILLISECONDS)
.addTag(tag)
.setInputData(workManagerInputData)
.setConstraints(constraints)
.build()
if (spec.backoffPolicy != null && spec.backoffDelay != null) {
builder.setBackoffCriteria(spec.backoffPolicy!!, spec.backoffDelay!!, TimeUnit.MILLISECONDS)
}

workManager.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.REPLACE, workRequest)
workManager.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.REPLACE, builder.build())
}

override fun cancelDeferred(tag: String) {
Expand Down
3 changes: 3 additions & 0 deletions async/src/main/java/org/odk/collect/async/TaskSpec.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.odk.collect.async

import android.content.Context
import androidx.work.BackoffPolicy
import java.util.function.Supplier

interface TaskSpec {
val maxRetries: Int?
val backoffPolicy: BackoffPolicy?
val backoffDelay: Long?

/**
* Should return the work to be carried out by the task. The return value of the work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Environment
import androidx.work.BackoffPolicy
import androidx.work.WorkerParameters
import org.odk.collect.android.injection.DaggerUtils
import org.odk.collect.android.instancemanagement.InstanceAutoSender
Expand Down Expand Up @@ -44,6 +45,8 @@ class AutoSendTaskSpec : TaskSpec {
lateinit var instanceAutoSender: InstanceAutoSender

override val maxRetries: Int? = null
override val backoffPolicy: BackoffPolicy? = null
override val backoffDelay: Long? = null

/**
* If the app-level auto-send setting is enabled, send all finalized forms that don't specify not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.odk.collect.android.backgroundwork

import android.content.Context
import androidx.work.BackoffPolicy
import androidx.work.WorkerParameters
import org.odk.collect.android.formmanagement.FormsUpdater
import org.odk.collect.android.injection.DaggerUtils
Expand All @@ -29,6 +30,8 @@ class AutoUpdateTaskSpec : TaskSpec {
lateinit var formsUpdater: FormsUpdater

override val maxRetries: Int? = null
override val backoffPolicy: BackoffPolicy? = null
override val backoffDelay: Long? = null

override fun getTask(context: Context, inputData: Map<String, String>, isLastUniqueExecution: Boolean): Supplier<Boolean> {
DaggerUtils.getComponent(context).inject(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import android.app.Application;

import androidx.work.BackoffPolicy;

import org.jetbrains.annotations.NotNull;
import org.odk.collect.async.Scheduler;
import org.odk.collect.settings.SettingsProvider;
Expand Down Expand Up @@ -66,7 +64,7 @@ private void scheduleAutoUpdate(long periodInMilliseconds, String projectId) {
private void scheduleMatchExactly(long periodInMilliseconds, String projectId) {
HashMap<String, String> inputData = new HashMap<>();
inputData.put(TaskData.DATA_PROJECT_ID, projectId);
scheduler.networkDeferred(getMatchExactlyTag(projectId), new SyncFormsTaskSpec(), periodInMilliseconds, BackoffPolicy.EXPONENTIAL, 60_000, inputData);
scheduler.networkDeferred(getMatchExactlyTag(projectId), new SyncFormsTaskSpec(), periodInMilliseconds, inputData);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.odk.collect.android.backgroundwork

import android.content.Context
import androidx.work.BackoffPolicy
import androidx.work.WorkerParameters
import org.odk.collect.android.formmanagement.FormsUpdater
import org.odk.collect.android.injection.DaggerUtils
Expand All @@ -14,6 +15,8 @@ class SyncFormsTaskSpec : TaskSpec {
lateinit var formsUpdater: FormsUpdater

override val maxRetries = 3
override val backoffPolicy = BackoffPolicy.EXPONENTIAL
override val backoffDelay: Long = 60_000

override fun getTask(context: Context, inputData: Map<String, String>, isLastUniqueExecution: Boolean): Supplier<Boolean> {
DaggerUtils.getComponent(context).inject(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.odk.collect.android.backgroundwork
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.BackoffPolicy
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
Expand Down Expand Up @@ -87,8 +86,6 @@ class FormUpdateAndInstanceSubmitSchedulerTest {
eq("match_exactly:myProject"),
any<SyncFormsTaskSpec>(),
eq(3600000),
eq(BackoffPolicy.EXPONENTIAL),
eq(60_000),
eq(mapOf(TaskData.DATA_PROJECT_ID to "myProject"))
)
}
Expand Down

0 comments on commit fc13750

Please sign in to comment.