Skip to content

Commit

Permalink
Move methods to schedule/cancel cache work to cache helper
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Dec 19, 2024
1 parent 9aa1220 commit c4a68a6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
11 changes: 3 additions & 8 deletions app/src/org/commcare/CommCareApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import org.commcare.tasks.DeleteLogs;
import org.commcare.tasks.LogSubmissionTask;
import org.commcare.tasks.PrimeEntityCache;
import org.commcare.tasks.PrimeEntityCacheHelper;
import org.commcare.tasks.PurgeStaleArchivedFormsTask;
import org.commcare.tasks.templates.ManagedAsyncTask;
import org.commcare.update.UpdateHelper;
Expand Down Expand Up @@ -160,7 +161,6 @@ public class CommCareApplication extends Application implements LifecycleEventOb
public static final int STATE_MIGRATION_QUESTIONABLE = 32;
private static final String DELETE_LOGS_REQUEST = "delete-logs-request";
private static final String CLEAN_RAW_MEDIA_REQUEST = "clean-raw-media-request";
private static final String PRIME_ENTITY_CACHE_REQUEST = "prime-entity-cache-request";
private static final long BACKOFF_DELAY_FOR_UPDATE_RETRY = 5 * 60 * 1000L; // 5 mins
private static final long BACKOFF_DELAY_FOR_FORM_SUBMISSION_RETRY = 5 * 60 * 1000L; // 5 mins
private static final long PERIODICITY_FOR_FORM_SUBMISSION_IN_HOURS = 1;
Expand Down Expand Up @@ -389,6 +389,7 @@ protected void cancelWorkManagerTasks() {
if (currentApp != null) {
WorkManager.getInstance(this).cancelUniqueWork(
FormSubmissionHelper.getFormSubmissionRequestName(currentApp.getUniqueId()));
PrimeEntityCacheHelper.cancelWork();
}
}

Expand Down Expand Up @@ -798,7 +799,7 @@ public void onServiceConnected(ComponentName className, IBinder service) {

purgeLogs();
cleanRawMedia();
primeEntityCache();
PrimeEntityCacheHelper.schedulePrimeEntityCacheWorker();
}

TimedStatsTracker.registerStartSession();
Expand Down Expand Up @@ -829,12 +830,6 @@ public void onServiceDisconnected(ComponentName className) {
sessionServiceIsBinding = true;
}

public void primeEntityCache() {
OneTimeWorkRequest primeEntityCacheRequest = new OneTimeWorkRequest.Builder(PrimeEntityCache.class).build();
WorkManager.getInstance(CommCareApplication.instance())
.enqueueUniqueWork(PRIME_ENTITY_CACHE_REQUEST, ExistingWorkPolicy.KEEP, primeEntityCacheRequest);
}

private void cleanRawMedia() {
OneTimeWorkRequest cleanRawMediaRequest = new OneTimeWorkRequest.Builder(CleanRawMedia.class).build();
WorkManager.getInstance(CommCareApplication.instance())
Expand Down
2 changes: 1 addition & 1 deletion app/src/org/commcare/tasks/DataPullTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private ResultAndError<PullTaskResult> handleBadLocalState(AndroidTransactionPar
if (returnCode == PROGRESS_DONE) {
// Recovery was successful
onSuccessfulSync();
CommCareApplication.instance().primeEntityCache();
PrimeEntityCacheHelper.schedulePrimeEntityCacheWorker();
return new ResultAndError<>(PullTaskResult.DOWNLOAD_SUCCESS);
} else if (returnCode == PROGRESS_RECOVERY_FAIL_SAFE || returnCode == PROGRESS_RECOVERY_FAIL_BAD) {
wipeLoginIfItOccurred();
Expand Down
30 changes: 29 additions & 1 deletion app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.commcare.tasks

import android.util.Pair
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import io.reactivex.functions.Cancellable
import org.commcare.CommCareApplication
import org.commcare.cases.entity.Entity
import org.commcare.entity.PrimeEntityCacheListener
import org.commcare.suite.model.Detail
import org.commcare.suite.model.EntityDatum
import org.commcare.sync.FormSubmissionHelper
import org.commcare.utils.AndroidCommCarePlatform
import org.javarosa.core.model.condition.EvaluationContext
import org.javarosa.core.model.instance.TreeReference
import org.javarosa.core.services.Logger

/**
* Helper to prime cache for all entity screens in the app
Expand All @@ -28,10 +33,33 @@ class PrimeEntityCacheHelper private constructor() : Cancellable {
@Volatile
private var instance: PrimeEntityCacheHelper? = null

const val PRIME_ENTITY_CACHE_REQUEST = "prime-entity-cache-request"

@JvmStatic
fun getInstance() =
instance ?: synchronized(this) {
instance ?: PrimeEntityCacheHelper().also { instance = it }
}

/**
* Schedules a background worker request to prime cache for all
* cache backed entity list screens in the current seated app
*/
@JvmStatic
fun schedulePrimeEntityCacheWorker() {
val primeEntityCacheRequest = OneTimeWorkRequest.Builder(PrimeEntityCache::class.java).build()
WorkManager.getInstance(CommCareApplication.instance())
.enqueueUniqueWork(
PRIME_ENTITY_CACHE_REQUEST,
ExistingWorkPolicy.KEEP,
primeEntityCacheRequest
)
}

@JvmStatic
fun cancelWork() {
WorkManager.getInstance(CommCareApplication.instance()).cancelUniqueWork(PRIME_ENTITY_CACHE_REQUEST)
}
}

/**
Expand Down Expand Up @@ -59,6 +87,7 @@ class PrimeEntityCacheHelper private constructor() : Cancellable {

/**
* Cancel any current cache prime process to expedite cache calculations for given [detail]
* Reschedules the work again in background afterwards
*/
fun expediteDetailWithId(detail: Detail, entities: MutableList<Entity<TreeReference>>) {
cancel()
Expand Down Expand Up @@ -89,7 +118,6 @@ class PrimeEntityCacheHelper private constructor() : Cancellable {

private fun primeCacheForDetail(detail: Detail, sessionDatum: EntityDatum? = null, entities: MutableList<Entity<TreeReference>>? = null) {
if (!detail.shouldCache()) return

currentDetailInProgress = detail.id
entityLoaderHelper = EntityLoaderHelper(detail, evalCtx())

Expand Down

0 comments on commit c4a68a6

Please sign in to comment.