Do not restart when there is no Internet

This commit is contained in:
sim 2023-03-23 11:58:16 +01:00
parent 1628a4e718
commit bce4244cb0
3 changed files with 24 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import org.unifiedpush.distributor.nextpush.api.response.SSEResponse
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE
import org.unifiedpush.distributor.nextpush.distributor.Distributor.sendMessage
import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback
import org.unifiedpush.distributor.nextpush.services.RestartWorker
import org.unifiedpush.distributor.nextpush.services.StartService
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createStartErrorNotification
@ -85,6 +86,11 @@ class SSEListener(val context: Context) : EventSourceListener() {
response?.let {
Log.d(TAG, "onFailure: ${it.code}")
}
if (!RestartNetworkCallback.hasInternet) {
Log.d(TAG, "No Internet: do not restart")
FailureHandler.once(eventSource)
return
}
FailureHandler.newFail(context, eventSource)
val delay = when (FailureHandler.nFails) {
1 -> 2 // 2sec

View File

@ -29,13 +29,24 @@ object FailureHandler {
if (this.eventSource == null || this.eventSource == eventSource) {
Log.d(TAG, "EventSource is known or null")
nFails++
if (hasFailed(twice = true)) {
if (nFails == 2) {
createWarningNotification(context)
}
this.eventSource = null
}
}
fun once(eventSource: EventSource?) {
Log.d(TAG, "once/Eventsource: $eventSource")
// ignore fails from a possible old eventSource
// if we are already reconnected
if (this.eventSource == null || this.eventSource == eventSource) {
Log.d(TAG, "EventSource is known or null")
nFails = 1
this.eventSource = null
}
}
fun setMaxFails(context: Context) {
// We set nFails to max to not restart the worker
// and keep it running
@ -49,9 +60,8 @@ object FailureHandler {
eventSource = null
}
fun hasFailed(twice: Boolean = false, orNeverStart: Boolean = true): Boolean {
fun hasFailed(orNeverStart: Boolean = true): Boolean {
// nFails > 0 to be sure it is not actually restarting
return if (orNeverStart) { eventSource == null } else { false } ||
nFails > if (twice) { 1 } else { 0 }
return if (orNeverStart) { eventSource == null } else { false } || nFails > 0
}
}

View File

@ -14,7 +14,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED")
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
if (FailureHandler.hasFailed(orNeverStart = false)) {
Log.d(TAG, "Available: restarting worker")
RestartWorker.run(context, delay = 0)
}
@ -28,7 +28,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
if (!hasInternet) {
hasInternet = true
Log.d(TAG, "Network Capabilities changed")
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
if (FailureHandler.hasFailed(orNeverStart = false)) {
Log.d(TAG, "Internet Cap: restarting worker")
RestartWorker.run(context, delay = 0)
} // else, it retries in max 2sec
@ -71,6 +71,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
companion object {
private var registered = false
private var hasInternet = false
var hasInternet = false
private set
}
}