NSCv3: improve scheduling

This commit is contained in:
Milos Kozak 2023-01-05 20:30:40 +01:00
parent 86b8d763e6
commit c25c9cedbf
2 changed files with 19 additions and 16 deletions

View file

@ -196,18 +196,18 @@ class NSClientV3Plugin @Inject constructor(
disposable += rxBus disposable += rxBus
.toObservable(EventNewBG::class.java) .toObservable(EventNewBG::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ scheduleExecution() }, fabricPrivacy::logException) .subscribe({ scheduleExecution("NEW_BG") }, fabricPrivacy::logException)
disposable += rxBus disposable += rxBus
.toObservable(EventNewHistoryData::class.java) .toObservable(EventNewHistoryData::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ scheduleExecution() }, fabricPrivacy::logException) .subscribe({ scheduleExecution("NEW_DATA") }, fabricPrivacy::logException)
runLoop = Runnable { runLoop = Runnable {
handler.postDelayed(runLoop, REFRESH_INTERVAL) handler.postDelayed(runLoop, REFRESH_INTERVAL)
executeLoop() executeLoop("MAIN_LOOP")
} }
handler.postDelayed(runLoop, REFRESH_INTERVAL) handler.postDelayed(runLoop, REFRESH_INTERVAL)
executeLoop() executeLoop("START")
} }
override fun onStop() { override fun onStop() {
@ -273,7 +273,7 @@ class NSClientV3Plugin @Inject constructor(
} }
override fun resend(reason: String) { override fun resend(reason: String) {
executeLoop() executeLoop("RESEND")
} }
override fun pause(newState: Boolean) { override fun pause(newState: Boolean) {
@ -654,13 +654,17 @@ class NSClientV3Plugin @Inject constructor(
} }
fun scheduleNewExecution() { fun scheduleNewExecution() {
var toTime = lastLoadedSrvModified.collections.entries + T.mins(6).plus(T.secs(0)).msecs() var origin = "5_MIN_AFTER_BG"
if (toTime < dateUtil.now()) toTime = dateUtil.now() + T.mins(1).plus(T.secs(0)).msecs() var toTime = lastLoadedSrvModified.collections.entries + T.mins(5).plus(T.secs(10)).msecs()
handler.postDelayed({ executeLoop() }, toTime - dateUtil.now()) if (toTime < dateUtil.now()) {
toTime = dateUtil.now() + T.mins(1).plus(T.secs(0)).msecs()
origin = "1_MIN_OLD_DATA"
}
handler.postDelayed({ executeLoop(origin) }, toTime - dateUtil.now())
rxBus.send(EventNSClientNewLog("NEXT", dateUtil.dateAndTimeAndSecondsString(toTime))) rxBus.send(EventNSClientNewLog("NEXT", dateUtil.dateAndTimeAndSecondsString(toTime)))
} }
private fun executeLoop() { private fun executeLoop(origin: String) {
if (sp.getBoolean(R.string.key_ns_client_paused, false)) { if (sp.getBoolean(R.string.key_ns_client_paused, false)) {
rxBus.send(EventNSClientNewLog("RUN", "paused")) rxBus.send(EventNSClientNewLog("RUN", "paused"))
return return
@ -670,9 +674,9 @@ class NSClientV3Plugin @Inject constructor(
return return
} }
if (workIsRunning(arrayOf(JOB_NAME))) if (workIsRunning(arrayOf(JOB_NAME)))
rxBus.send(EventNSClientNewLog("RUN", "Already running")) rxBus.send(EventNSClientNewLog("RUN", "Already running $origin"))
else { else {
rxBus.send(EventNSClientNewLog("RUN", "Starting next round")) rxBus.send(EventNSClientNewLog("RUN", "Starting next round $origin"))
WorkManager.getInstance(context) WorkManager.getInstance(context)
.beginUniqueWork( .beginUniqueWork(
"NSCv3Load", "NSCv3Load",
@ -700,12 +704,12 @@ class NSClientV3Plugin @Inject constructor(
private val eventWorker = Executors.newSingleThreadScheduledExecutor() private val eventWorker = Executors.newSingleThreadScheduledExecutor()
private var scheduledEventPost: ScheduledFuture<*>? = null private var scheduledEventPost: ScheduledFuture<*>? = null
private fun scheduleExecution() { private fun scheduleExecution(origin: String) {
class PostRunnable : Runnable { class PostRunnable : Runnable {
override fun run() { override fun run() {
scheduledEventPost = null scheduledEventPost = null
executeLoop() executeLoop(origin)
} }
} }
// cancel waiting task to prevent sending multiple posts // cancel waiting task to prevent sending multiple posts

View file

@ -57,6 +57,7 @@ class LoadBgWorker(
sgvs = response.values sgvs = response.values
response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it } response.lastServerModified?.let { nsClientV3Plugin.lastLoadedSrvModified.collections.entries = it }
nsClientV3Plugin.storeLastLoadedSrvModified() nsClientV3Plugin.storeLastLoadedSrvModified()
nsClientV3Plugin.scheduleNewExecution() // Idea is to run after 5 min after last BG
} }
aapsLogger.debug("SGVS: $sgvs") aapsLogger.debug("SGVS: $sgvs")
if (sgvs.isNotEmpty()) { if (sgvs.isNotEmpty()) {
@ -97,9 +98,7 @@ class LoadBgWorker(
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded nsClientV3Plugin.lastLoadedSrvModified.collections.entries = lastLoaded
nsClientV3Plugin.storeLastLoadedSrvModified() nsClientV3Plugin.storeLastLoadedSrvModified()
} }
rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
.dateAndTimeAndSecondsString(lastLoaded)}"))
nsClientV3Plugin.scheduleNewExecution() // Idea is to run after 5 min after last BG
WorkManager.getInstance(context) WorkManager.getInstance(context)
.beginUniqueWork( .beginUniqueWork(
NSClientV3Plugin.JOB_NAME, NSClientV3Plugin.JOB_NAME,