Do not fail polling/refresh if one topics fails, closes binwiederhier/ntfy#27

This commit is contained in:
Philipp Heckel 2021-12-11 19:40:32 -05:00
parent 425ab45e84
commit 459e1ff84b
3 changed files with 31 additions and 33 deletions

View file

@ -7,10 +7,7 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.ActionMode import android.view.*
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -309,10 +306,12 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
private fun refreshAllSubscriptions() { private fun refreshAllSubscriptions() {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
try { Log.d(TAG, "Polling for new notifications")
Log.d(TAG, "Polling for new notifications") var errors = 0
var newNotificationsCount = 0 var errorMessage = "" // First error
repository.getSubscriptions().forEach { subscription -> var newNotificationsCount = 0
repository.getSubscriptions().forEach { subscription ->
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic) val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
val newNotifications = repository.onlyNewNotifications(subscription.id, notifications) val newNotifications = repository.onlyNewNotifications(subscription.id, notifications)
newNotifications.forEach { notification -> newNotifications.forEach { notification ->
@ -326,24 +325,24 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
broadcaster?.send(subscription, notification, result.muted) broadcaster?.send(subscription, notification, result.muted)
} }
} }
} } catch (e: Exception) {
val toastMessage = if (newNotificationsCount == 0) { val topic = topicShortUrl(subscription.baseUrl, subscription.topic)
getString(R.string.refresh_message_no_results) if (errorMessage == "") errorMessage = "$topic: ${e.message}"
} else { errors++
getString(R.string.refresh_message_result, newNotificationsCount)
}
runOnUiThread {
Toast.makeText(this@MainActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
Log.d(TAG, "Finished polling for new notifications")
} catch (e: Exception) {
Log.e(TAG, "Polling failed: ${e.message}", e)
runOnUiThread {
Toast.makeText(this@MainActivity, getString(R.string.refresh_message_error, e.message), Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
} }
} }
val toastMessage = if (errors > 0) {
getString(R.string.refresh_message_error, errors, errorMessage)
} else if (newNotificationsCount == 0) {
getString(R.string.refresh_message_no_results)
} else {
getString(R.string.refresh_message_result, newNotificationsCount)
}
runOnUiThread {
Toast.makeText(this@MainActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
Log.d(TAG, "Finished polling for new notifications")
} }
} }

View file

@ -29,8 +29,8 @@ class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx,
val broadcaster = BroadcastService(applicationContext) val broadcaster = BroadcastService(applicationContext)
val api = ApiService() val api = ApiService()
try { repository.getSubscriptions().forEach{ subscription ->
repository.getSubscriptions().forEach{ subscription -> try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic) val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
val newNotifications = repository val newNotifications = repository
.onlyNewNotifications(subscription.id, notifications) .onlyNewNotifications(subscription.id, notifications)
@ -44,18 +44,17 @@ class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx,
broadcaster.send(subscription, notification, result.muted) broadcaster.send(subscription, notification, result.muted)
} }
} }
} catch (e: Exception) {
Log.e(TAG, "Failed checking messages: ${e.message}", e)
} }
Log.d(TAG, "Finished polling for new notifications")
return@withContext Result.success()
} catch (e: Exception) {
Log.e(TAG, "Failed checking messages: ${e.message}", e)
return@withContext Result.failure()
} }
Log.d(TAG, "Finished polling for new notifications")
return@withContext Result.success()
} }
} }
companion object { companion object {
const val VERSION = BuildConfig.VERSION_CODE const val VERSION = BuildConfig.VERSION_CODE
const val TAG = "NtfyPollWorker" const val TAG = "NtfyPollWorker"
const val WORK_NAME_PERIODIC = "NtfyPollWorkerPeriodic" const val WORK_NAME_PERIODIC = "NtfyPollWorkerPeriodic"
} }

View file

@ -23,7 +23,7 @@
<!-- Common refresh toasts --> <!-- Common refresh toasts -->
<string name="refresh_message_result">%1$d notification(s) received</string> <string name="refresh_message_result">%1$d notification(s) received</string>
<string name="refresh_message_no_results">Everything is up-to-date</string> <string name="refresh_message_no_results">Everything is up-to-date</string>
<string name="refresh_message_error">Could not refresh topic: %1$s</string> <string name="refresh_message_error">%1$d subscription(s) could not be refreshed\n\n%2$s</string>
<!-- Main activity: Action bar --> <!-- Main activity: Action bar -->
<string name="main_action_bar_title">Subscribed topics</string> <string name="main_action_bar_title">Subscribed topics</string>