Use static var to track SSE status

This commit is contained in:
sim 2023-03-26 18:34:46 +02:00
parent 881e442ac6
commit bea744db18
2 changed files with 15 additions and 6 deletions

View File

@ -30,8 +30,6 @@ class SSEListener(val context: Context) : EventSourceListener() {
it.release() it.release()
} }
} }
started = false
pinged = false
try { try {
Log.d(TAG, "onOpen: " + response.code) Log.d(TAG, "onOpen: " + response.code)
} catch (e: Exception) { } catch (e: Exception) {
@ -85,7 +83,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
Log.d(TAG, "onClosed: $eventSource") Log.d(TAG, "onClosed: $eventSource")
eventSource.cancel() eventSource.cancel()
if (!shouldRestart()) return if (!shouldRestart()) return
FailureHandler.newFail(context, eventSource, started, pinged) FailureHandler.newFail(context, eventSource)
clearVars()
RestartWorker.run(context, delay = 0) RestartWorker.run(context, delay = 0)
} }
@ -102,9 +101,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
if (!RestartNetworkCallback.hasInternet) { if (!RestartNetworkCallback.hasInternet) {
Log.d(TAG, "No Internet: do not restart") Log.d(TAG, "No Internet: do not restart")
FailureHandler.once(eventSource) FailureHandler.once(eventSource)
clearVars()
return return
} }
FailureHandler.newFail(context, eventSource, started, pinged) FailureHandler.newFail(context, eventSource)
clearVars()
val delay = when (FailureHandler.nFails) { val delay = when (FailureHandler.nFails) {
1 -> 2 // 2sec 1 -> 2 // 2sec
2 -> 5 // 5sec 2 -> 5 // 5sec
@ -133,6 +134,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
return true return true
} }
private fun clearVars() {
started = false
pinged = false
}
companion object { companion object {
var lastEventDate: Calendar? = null var lastEventDate: Calendar? = null
var keepalive = 900 var keepalive = 900

View File

@ -3,6 +3,7 @@ package org.unifiedpush.distributor.nextpush.services
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import okhttp3.sse.EventSource import okhttp3.sse.EventSource
import org.unifiedpush.distributor.nextpush.api.SSEListener
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification
@ -29,7 +30,7 @@ object FailureHandler {
nFailsBeforePing = 0 nFailsBeforePing = 0
} }
fun newFail(context: Context, eventSource: EventSource?, started: Boolean, pinged: Boolean) { fun newFail(context: Context, eventSource: EventSource?) {
Log.d(TAG, "newFail/Eventsource: $eventSource") Log.d(TAG, "newFail/Eventsource: $eventSource")
// ignore fails from a possible old eventSource // ignore fails from a possible old eventSource
// if we are already reconnected // if we are already reconnected
@ -39,7 +40,8 @@ object FailureHandler {
if (nFails == 2) { if (nFails == 2) {
showWarningNotification(context) showWarningNotification(context)
} }
if (started && !pinged) { if (SSEListener.started && !SSEListener.pinged) {
Log.d(TAG, "The service has started and it has never received a ping.")
nFailsBeforePing++ nFailsBeforePing++
if (nFailsBeforePing == 3) { if (nFailsBeforePing == 3) {
showNoPingNotification(context) showNoPingNotification(context)
@ -70,6 +72,7 @@ object FailureHandler {
fun clearFails() { fun clearFails() {
nFails = 0 nFails = 0
nFailsBeforePing = 0
eventSource = null eventSource = null
} }