Make FailureHandler an object

This commit is contained in:
sim 2023-02-24 19:30:01 +01:00
parent cac671224e
commit 842e011b31
5 changed files with 21 additions and 18 deletions

View File

@ -10,6 +10,9 @@ import okhttp3.sse.EventSourceListener
import org.unifiedpush.distributor.nextpush.api.response.SSEResponse import org.unifiedpush.distributor.nextpush.api.response.SSEResponse
import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE
import org.unifiedpush.distributor.nextpush.distributor.Distributor.sendMessage 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 org.unifiedpush.distributor.nextpush.utils.TAG
import java.lang.Exception import java.lang.Exception
import java.util.Calendar import java.util.Calendar
@ -22,7 +25,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
} }
override fun onOpen(eventSource: EventSource, response: Response) { override fun onOpen(eventSource: EventSource, response: Response) {
StartService.newEventSource(context, eventSource) FailureHandler.newEventSource(context, eventSource)
StartService.wakeLock?.let { StartService.wakeLock?.let {
while (it.isHeld) { while (it.isHeld) {
it.release() it.release()
@ -71,7 +74,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
return return
} }
Log.d(TAG, "onClosed: $eventSource") Log.d(TAG, "onClosed: $eventSource")
StartService.newFail(context, eventSource) FailureHandler.newFail(context, eventSource)
RestartWorker.start(context, delay = 0) RestartWorker.start(context, delay = 0)
} }
@ -86,8 +89,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
response?.let { response?.let {
Log.d(TAG, "onFailure: ${it.code}") Log.d(TAG, "onFailure: ${it.code}")
} }
StartService.newFail(context, eventSource) FailureHandler.newFail(context, eventSource)
val delay = when (StartService.nFails) { val delay = when (FailureHandler.nFails) {
1 -> 2 // 2sec 1 -> 2 // 2sec
2 -> 20 // 20sec 2 -> 20 // 20sec
3 -> 60 // 1min 3 -> 60 // 1min

View File

@ -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.NotificationUtils.deleteWarningNotification
import org.unifiedpush.distributor.nextpush.utils.TAG 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 // This is the last eventSource opened
var eventSource: EventSource? private var eventSource: EventSource? = null
fun newEventSource(context: Context, eventSource: EventSource) { fun newEventSource(context: Context, eventSource: EventSource) {
Log.d(TAG, "newEvent/Eventsource: $eventSource") Log.d(TAG, "newEvent/Eventsource: $eventSource")
@ -23,7 +24,8 @@ interface FailureHandler {
fun newFail(context: Context, eventSource: EventSource?) { fun newFail(context: Context, eventSource: EventSource?) {
Log.d(TAG, "newFail/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) { if (this.eventSource == null || this.eventSource == eventSource) {
Log.d(TAG, "EventSource is known or null") Log.d(TAG, "EventSource is known or null")
nFails++ nFails++

View File

@ -14,7 +14,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
Log.d(TAG, "Network is CONNECTED") 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") Log.d(TAG, "networkCallback: restarting worker")
RestartWorker.start(context, delay = 0) RestartWorker.start(context, delay = 0)
} }
@ -25,7 +25,7 @@ class RestartNetworkCallback(val context: Context) : ConnectivityManager.Network
networkCapabilities: NetworkCapabilities networkCapabilities: NetworkCapabilities
) { ) {
Log.d(TAG, "Network Capabilities changed") 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") Log.d(TAG, "networkCallback: restarting worker")
RestartWorker.start(context, delay = 0) RestartWorker.start(context, delay = 0)
} // else, it retries in max 2sec } // else, it retries in max 2sec

View File

@ -39,7 +39,7 @@ class RestartWorker(ctx: Context, params: WorkerParameters) : Worker(ctx, params
Log.d(TAG, "restartDate: ${restartDate.time}") Log.d(TAG, "restartDate: ${restartDate.time}")
if (currentDate.after(restartDate)) { if (currentDate.after(restartDate)) {
Log.d(TAG, "Restarting") 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) StartService.startListener(applicationContext)
} }
} ?: run { } ?: run {

View File

@ -21,7 +21,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
class StartService : Service() { class StartService : Service() {
companion object : FailureHandler { companion object {
private const val WAKE_LOCK_TAG = "NextPush:StartService:lock" private const val WAKE_LOCK_TAG = "NextPush:StartService:lock"
var service: StartService? = null var service: StartService? = null
@ -29,10 +29,10 @@ class StartService : Service() {
var wakeLock: PowerManager.WakeLock? = null var wakeLock: PowerManager.WakeLock? = null
fun startListener(context: Context) { fun startListener(context: Context) {
if (isServiceStarted && !hasFailed()) return if (isServiceStarted && !FailureHandler.hasFailed()) return
Log.d(TAG, "Starting the Listener") Log.d(TAG, "Starting the Listener")
Log.d(TAG, "Service is started: $isServiceStarted") 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) val serviceIntent = Intent(context, StartService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent) context.startForegroundService(serviceIntent)
@ -40,8 +40,6 @@ class StartService : Service() {
context.startService(serviceIntent) context.startService(serviceIntent)
} }
} }
override var nFails = 0
override var eventSource: EventSource? = null
} }
private val networkCallback = RestartNetworkCallback(this) private val networkCallback = RestartNetworkCallback(this)
@ -80,7 +78,7 @@ class StartService : Service() {
fun stopService(block: () -> Unit = {}) { fun stopService(block: () -> Unit = {}) {
Log.d(TAG, "Stopping Service") Log.d(TAG, "Stopping Service")
isServiceStarted = false isServiceStarted = false
clearFails() FailureHandler.clearFails()
apiDestroy() apiDestroy()
networkCallback.unregister() networkCallback.unregister()
wakeLock?.let { wakeLock?.let {
@ -93,7 +91,7 @@ class StartService : Service() {
} }
private fun startService() { private fun startService() {
if (isServiceStarted && !hasFailed()) return if (isServiceStarted && !FailureHandler.hasFailed()) return
isServiceStarted = true isServiceStarted = true
try { try {