From 516630215c3adfb32577a27bea2d9a09be58b03b Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 17 Oct 2021 12:07:23 +0200 Subject: [PATCH] Worker DB pruning --- .../androidaps/receivers/KeepAliveReceiver.kt | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt index 5fa6709e26..94d75d342b 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt @@ -7,10 +7,8 @@ import android.app.PendingIntent.FLAG_IMMUTABLE import android.content.Context import android.content.Intent import android.os.SystemClock -import androidx.work.OneTimeWorkRequest -import androidx.work.WorkManager -import androidx.work.Worker -import androidx.work.WorkerParameters +import androidx.work.* +import com.google.common.util.concurrent.ListenableFuture import dagger.android.DaggerBroadcastReceiver import dagger.android.HasAndroidInjector import info.nightscout.androidaps.BuildConfig @@ -55,7 +53,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { } class KeepAliveWorker( - context: Context, + private val context: Context, params: WorkerParameters ) : Worker(context, params) { @@ -96,10 +94,26 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { checkPump() checkAPS() maintenancePlugin.deleteLogs(30) + workerDbStatus() return Result.success() } + // When Worker DB grows too much, work operations become slow + // Library is cleaning DB every 7 days which may not be sufficient for NSClient full sync + private fun workerDbStatus() { + val workQuery = WorkQuery.Builder + .fromStates(listOf(WorkInfo.State.FAILED, WorkInfo.State.SUCCEEDED)) + .build() + + val workInfo: ListenableFuture> = WorkManager.getInstance(context).getWorkInfos(workQuery) + aapsLogger.debug(LTag.CORE, "WorkManager size is ${workInfo.get().size}") + if (workInfo.get().size > 1000) { + WorkManager.getInstance(context).pruneWork() + aapsLogger.debug(LTag.CORE, "WorkManager pruning ....") + } + } + // Usually deviceStatus is uploaded through LoopPlugin after every loop cycle. // if there is no BG available, we have to upload anyway to have correct // IOB displayed in NS