Worker DB pruning

This commit is contained in:
Milos Kozak 2021-10-17 12:07:23 +02:00
parent 3093a84d65
commit 516630215c

View file

@ -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<List<WorkInfo>> = 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