-
-
Notifications
You must be signed in to change notification settings - Fork 45
CCCT-2019 Periodic Fetch For Release Toggles #3520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
conroy-ricketts
wants to merge
11
commits into
master
Choose a base branch
from
task/CCCT-2019-release-toggles-periodic-fetch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
46e4d07
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts 75fecc7
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts 4317e6f
Merge branch 'master' of https://github.com/dimagi/commcare-android i…
conroy-ricketts 82e7795
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts e47c69e
Merge branch 'master' of https://github.com/dimagi/commcare-android i…
conroy-ricketts 6c177a4
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts 88bc3f5
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts 8487a32
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts c279be9
CCCT-2020 Fetch Release Toggles For New Opportunities
conroy-ricketts 683ddaa
Merge branch 'master' of https://github.com/dimagi/commcare-android i…
conroy-ricketts 6bab11f
CCCT-2019 Release Toggles Periodic Fetch
conroy-ricketts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
100 changes: 100 additions & 0 deletions
100
app/src/org/commcare/connect/workers/ConnectReleaseTogglesWorker.kt
This file contains hidden or 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,100 @@ | ||
| package org.commcare.connect.workers | ||
|
|
||
| import android.content.Context | ||
| import androidx.work.Constraints | ||
| import androidx.work.CoroutineWorker | ||
| import androidx.work.ExistingPeriodicWorkPolicy | ||
| import androidx.work.ExistingWorkPolicy | ||
| import androidx.work.NetworkType | ||
| import androidx.work.OneTimeWorkRequest | ||
| import androidx.work.PeriodicWorkRequest | ||
| import androidx.work.WorkManager | ||
| import androidx.work.WorkerParameters | ||
| import org.commcare.android.database.connect.models.ConnectReleaseToggleRecord | ||
| import org.commcare.connect.PersonalIdManager | ||
| import org.commcare.connect.network.connectId.PersonalIdApiHandler | ||
| import org.javarosa.core.services.Logger | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.coroutines.resume | ||
| import kotlin.coroutines.suspendCoroutine | ||
|
|
||
| class ConnectReleaseTogglesWorker( | ||
| val context: Context, | ||
| workerParameters: WorkerParameters, | ||
| ) : CoroutineWorker(context, workerParameters) { | ||
| override suspend fun doWork(): Result { | ||
| val personalIdManager = PersonalIdManager.getInstance() | ||
| if (!personalIdManager.isloggedIn()) { | ||
| return Result.failure() | ||
| } | ||
|
|
||
| val user = personalIdManager.getUser(context) | ||
|
|
||
| return suspendCoroutine { continuation -> | ||
| object : PersonalIdApiHandler<List<ConnectReleaseToggleRecord>>() { | ||
| override fun onFailure( | ||
| errorCode: PersonalIdOrConnectApiErrorCodes, | ||
| t: Throwable?, | ||
| ) { | ||
| Logger.exception("Failed to get feature release toggles in background!", t) | ||
|
|
||
| if (errorCode == PersonalIdOrConnectApiErrorCodes.NETWORK_ERROR) { | ||
| continuation.resume(Result.retry()) | ||
| } else { | ||
| continuation.resume(Result.failure()) | ||
| } | ||
| } | ||
|
|
||
| override fun onSuccess(data: List<ConnectReleaseToggleRecord>) { | ||
| continuation.resume(Result.success()) | ||
| } | ||
| }.getReleaseToggles(context, user.userId, user.password) | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val WORK_NAME = "connect_release_toggles_fetch_worker" | ||
|
|
||
| fun scheduleOneTimeFetch(context: Context) { | ||
| val workRequest = | ||
| OneTimeWorkRequest | ||
| .Builder(ConnectReleaseTogglesWorker::class.java) | ||
| .setConstraints(getWorkConstraints()) | ||
| .build() | ||
|
|
||
| WorkManager.getInstance(context).enqueueUniqueWork( | ||
| WORK_NAME, | ||
| ExistingWorkPolicy.KEEP, | ||
| workRequest, | ||
| ) | ||
| } | ||
|
|
||
| fun schedulePeriodicFetch(context: Context) { | ||
| val workRequest = | ||
| PeriodicWorkRequest | ||
| .Builder( | ||
| ConnectReleaseTogglesWorker::class.java, | ||
| 4, | ||
| TimeUnit.HOURS, | ||
| ).setConstraints(getWorkConstraints()) | ||
| .build() | ||
|
|
||
| WorkManager.getInstance(context).enqueueUniquePeriodicWork( | ||
| WORK_NAME, | ||
| ExistingPeriodicWorkPolicy.KEEP, | ||
| workRequest, | ||
| ) | ||
| } | ||
|
|
||
| fun cancelPeriodicFetch(context: Context) { | ||
| WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME) | ||
| } | ||
|
|
||
| private fun getWorkConstraints() = | ||
| Constraints | ||
| .Builder() | ||
| .setRequiredNetworkType(NetworkType.CONNECTED) | ||
| .setRequiresBatteryNotLow(true) | ||
| .build() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
WorkManager enqueueUniqueWork enqueueUniquePeriodicWork same work name collision Android💡 Result:
Yes—
enqueueUniqueWork()andenqueueUniquePeriodicWork()share the same “unique work name” namespace (within your app), so using the sameuniqueWorkNamecan collide.ExistingPeriodicWorkPolicyto anExistingWorkPolicyand creates aWorkContinuationImpl(this, uniqueWorkName, existingWorkPolicy, ...). That means the periodic work is inserted into the same unique-name conflict resolution path as one-time unique work. [3]What happens on collision
If you call one after the other with the same name, whichever you enqueue second will apply its existing-work policy against whatever is already enqueued under that name:
ExistingWorkPolicy(KEEP,REPLACE,APPEND, etc.). [2]ExistingPeriodicWorkPolicy(KEEP,CANCEL_AND_REENQUEUE, and newerUPDATE, etc.). [4]Recommendation
Use different unique names for periodic vs one-time work (e.g.,
"sync_periodic"vs"sync_oneoff"), and use tags if you want to group/observe/cancel both kinds together. [1]Sources:
[1] Android Developers – Managing work / Unique Work https://developer.android.com/develop/background-work/background-tasks/persistent/how-to/manage-work
[2] Android Developers –
WorkManager.enqueueUniqueWorkAPI https://developer.android.com/reference/androidx/work/WorkManager[3] AOSP/AndroidX source –
WorkManagerImpl.createWorkContinuationForUniquePeriodicWork(...)usesWorkContinuationImpl(..., uniqueWorkName, existingWorkPolicy, ...)https://android.googlesource.com/platform/frameworks/support/+/3739ee65f12c3d007d44999afb6786b7575aa50a/work/workmanager/src/main/java/androidx/work/impl/WorkManagerImpl.java[4] Android Developers –
ExistingPeriodicWorkPolicyhttps://developer.android.com/reference/androidx/work/ExistingPeriodicWorkPolicy🌐 Web query:
Android WorkManager unique work name global namespace one-time periodic💡 Result:
uniqueWorkNameis only “global” within your app’s WorkManager database (your app/package sandbox)—it will not collide with other apps’ work, even if they use the same string name. [1]enqueueUniqueWork(name, ExistingWorkPolicy, OneTimeWorkRequest)for one-time unique work/chains [2]enqueueUniquePeriodicWork(name, ExistingPeriodicWorkPolicy, PeriodicWorkRequest)for periodic unique work [2]WorkManager’s “unique work” concept is intended to ensure only one active instance for a given unique name. [3]
uniqueWorkNamefor unrelated work, especially mixing “one-time” and “periodic” concepts—use distinct names/prefixes. [2] [3]ExistingWorkPolicy(e.g.,KEEP,REPLACE,APPEND, etc.). [2]ExistingPeriodicWorkPolicy(e.g.,KEEP,REPLACE,UPDATE). [2] [4]Sources:
[1] Stack Overflow (app sandbox / no cross-app collisions) (stackoverflow.com)
[2] Android Developers –
WorkManagerAPI reference (developer.android.com)[3] Android Developers – “Managing work” (Unique Work concept) (developer.android.com)
[4] Android Developers –
ExistingPeriodicWorkPolicy(developer.android.com)Citations:
🏁 Script executed:
Repository: dimagi/commcare-android
Length of output: 4349
Use separate unique work names for one-time and periodic fetches.
Both
scheduleOneTimeFetch()andschedulePeriodicFetch()share the same unique work name namespace in WorkManager. When both are used, the second enqueue silently applies itsExistingWorkPolicyagainst the first, potentially skipping one of the fetches. This defeats the intent of having independent fetch mechanisms.🛠️ Suggested fix (separate names)
📝 Committable suggestion
🤖 Prompt for AI Agents