Make FailureHandler an object
This commit is contained in:
parent
cac671224e
commit
842e011b31
|
@ -10,6 +10,9 @@ import okhttp3.sse.EventSourceListener
|
|||
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.RestartWorker
|
||||
import org.unifiedpush.distributor.nextpush.services.StartService
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
import java.lang.Exception
|
||||
import java.util.Calendar
|
||||
|
@ -22,7 +25,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
}
|
||||
|
||||
override fun onOpen(eventSource: EventSource, response: Response) {
|
||||
StartService.newEventSource(context, eventSource)
|
||||
FailureHandler.newEventSource(context, eventSource)
|
||||
StartService.wakeLock?.let {
|
||||
while (it.isHeld) {
|
||||
it.release()
|
||||
|
@ -71,7 +74,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
return
|
||||
}
|
||||
Log.d(TAG, "onClosed: $eventSource")
|
||||
StartService.newFail(context, eventSource)
|
||||
FailureHandler.newFail(context, eventSource)
|
||||
RestartWorker.start(context, delay = 0)
|
||||
}
|
||||
|
||||
|
@ -86,8 +89,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
|
|||
response?.let {
|
||||
Log.d(TAG, "onFailure: ${it.code}")
|
||||
}
|
||||
StartService.newFail(context, eventSource)
|
||||
val delay = when (StartService.nFails) {
|
||||
FailureHandler.newFail(context, eventSource)
|
||||
val delay = when (FailureHandler.nFails) {
|
||||
1 -> 2 // 2sec
|
||||
2 -> 20 // 20sec
|
||||
3 -> 60 // 1min
|
||||
|
|
|
@ -7,12 +7,13 @@ import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createWarnin
|
|||
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
interface FailureHandler {
|
||||
object FailureHandler {
|
||||
|
||||
var nFails: Int
|
||||
var nFails = 0
|
||||
private set
|
||||
|
||||
// This is the last eventSource opened
|
||||
var eventSource: EventSource?
|
||||
private var eventSource: EventSource? = null
|
||||
|
||||
fun newEventSource(context: Context, eventSource: EventSource) {
|
||||
Log.d(TAG, "newEvent/Eventsource: $eventSource")
|
||||
|
@ -23,7 +24,8 @@ interface FailureHandler {
|
|||
|
||||
fun newFail(context: Context, eventSource: EventSource?) {
|
||||
Log.d(TAG, "newFail/Eventsource: $eventSource")
|
||||
// no eventSource or the last opened
|
||||
// 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++
|
||||
|
|
|
@ -14,7 +14,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
|
|||
|
||||
override fun onAvailable(network: Network) {
|
||||
Log.d(TAG, "Network is CONNECTED")
|
||||
if (StartService.hasFailed(twice = true, orNeverStart = false)) {
|
||||
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
|
||||
Log.d(TAG, "networkCallback: restarting worker")
|
||||
RestartWorker.start(context, delay = 0)
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
|
|||
networkCapabilities: NetworkCapabilities
|
||||
) {
|
||||
Log.d(TAG, "Network Capabilities changed")
|
||||
if (StartService.hasFailed(twice = true, orNeverStart = false)) {
|
||||
if (FailureHandler.hasFailed(twice = true, orNeverStart = false)) {
|
||||
Log.d(TAG, "networkCallback: restarting worker")
|
||||
RestartWorker.start(context, delay = 0)
|
||||
} // else, it retries in max 2sec
|
||||
|
|
|
@ -39,7 +39,7 @@ class RestartWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params
|
|||
Log.d(TAG, "restartDate: ${restartDate.time}")
|
||||
if (currentDate.after(restartDate)) {
|
||||
Log.d(TAG, "Restarting")
|
||||
StartService.setMaxFails(applicationContext) // Max, will keep using the current worker
|
||||
FailureHandler.setMaxFails(applicationContext) // Max, will keep using the current worker
|
||||
StartService.startListener(applicationContext)
|
||||
}
|
||||
} ?: run {
|
||||
|
|
|
@ -21,7 +21,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
|
|||
|
||||
class StartService : Service() {
|
||||
|
||||
companion object : FailureHandler {
|
||||
companion object {
|
||||
private const val WAKE_LOCK_TAG = "NextPush:StartService:lock"
|
||||
|
||||
var service: StartService? = null
|
||||
|
@ -29,10 +29,10 @@ class StartService : Service() {
|
|||
var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
fun startListener(context: Context) {
|
||||
if (isServiceStarted && !hasFailed()) return
|
||||
if (isServiceStarted && !FailureHandler.hasFailed()) return
|
||||
Log.d(TAG, "Starting the Listener")
|
||||
Log.d(TAG, "Service is started: $isServiceStarted")
|
||||
Log.d(TAG, "nFails: $nFails")
|
||||
Log.d(TAG, "nFails: $FailureHandler.nFails")
|
||||
val serviceIntent = Intent(context, StartService::class.java)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(serviceIntent)
|
||||
|
@ -40,8 +40,6 @@ class StartService : Service() {
|
|||
context.startService(serviceIntent)
|
||||
}
|
||||
}
|
||||
override var nFails = 0
|
||||
override var eventSource: EventSource? = null
|
||||
}
|
||||
|
||||
private val networkCallback = RestartNetworkCallback(this)
|
||||
|
@ -80,7 +78,7 @@ class StartService : Service() {
|
|||
fun stopService(block: () -> Unit = {}) {
|
||||
Log.d(TAG, "Stopping Service")
|
||||
isServiceStarted = false
|
||||
clearFails()
|
||||
FailureHandler.clearFails()
|
||||
apiDestroy()
|
||||
networkCallback.unregister()
|
||||
wakeLock?.let {
|
||||
|
@ -93,7 +91,7 @@ class StartService : Service() {
|
|||
}
|
||||
|
||||
private fun startService() {
|
||||
if (isServiceStarted && !hasFailed()) return
|
||||
if (isServiceStarted && !FailureHandler.hasFailed()) return
|
||||
isServiceStarted = true
|
||||
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue