From af2f9d9a75c144f282216d66a907a0b0c617708d Mon Sep 17 00:00:00 2001 From: Solium <53835578+daschmidt1994@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:45:16 +0000 Subject: [PATCH] Mark remote state as stale when peer states cannot be read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the peer-state stream failed (timeout, no answer), the refresh silently reused the remotes of the cached snapshot. The app then kept showing "verbunden · Kontakt vor 3 Tagen" as if it were current, with no hint that the value was days old. Carried-over remotes now get stateAsOfMs (the fetch time of the copy, kept across repeated failures) and fetchError. A stale entry never counts as connected, the plan card shows "Status nicht abrufbar (...) · Stand ", the widget shows "Status unbekannt", and the disconnected alert ignores it because an old copy says nothing about the remote itself. Both fields survive the snapshot cache. --- RELEASE_NOTES.md | 9 +++--- app/build.gradle.kts | 4 +-- .../backrestwidget/BackrestWidget.kt | 1 + .../daschmidt/backrestwidget/MainActivity.kt | 18 +++++++---- .../at/daschmidt/backrestwidget/Models.kt | 25 +++++++++++++++- .../at/daschmidt/backrestwidget/Notifier.kt | 7 +++-- .../at/daschmidt/backrestwidget/Refresh.kt | 8 ++++- .../backrestwidget/AlertRulesTest.kt | 7 +++++ .../daschmidt/backrestwidget/SnapshotTest.kt | 30 +++++++++++++++++++ 9 files changed, 93 insertions(+), 16 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index b352730..531ea4e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,4 @@ -**Neu: Warteschlange** -- Backrest arbeitet Aufgaben nacheinander ab. Fällige, aber noch wartende Backups und Repo-Aufgaben (z. B. hinter einer langen Prüfung am Monatsersten) erscheinen jetzt unter „Wartet in der Warteschlange“ – mit Platz, Fälligkeit und Wartezeit. -- Plan-Karten zeigen „Backup wartet seit … · Platz n in der Warteschlange“ (Start-Knopf ist dann gesperrt), das Widget „wartet seit … · Platz n“. -- Die Statuszeile zählt wartende Aufgaben mit, z. B. „Prüfung läuft · 9 warten“. -- Pläne von Remote-Instanzen sind ausgenommen, sie haben ihre eigene Warteschlange. +**Korrektur: Remote-Instanzen zeigen keinen veralteten Status mehr** +- Konnte die App den Status der Remote-Instanzen nicht abrufen, zeigte sie bisher kommentarlos den letzten gespeicherten Stand – inklusive „verbunden“ und einem tagealten „Kontakt vor …“. +- Jetzt steht dort „Status nicht abrufbar (Fehler) · Stand “, das Widget zeigt „Status unbekannt“. +- Ein alter Stand löst keine „getrennt“-Benachrichtigung aus, weil er über die Remote-Instanz selbst nichts aussagt. diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 70266ee..efeb7c5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,8 +17,8 @@ android { applicationId = "at.daschmidt.backrestwidget" minSdk = 26 targetSdk = 36 - versionCode = 8 - versionName = "0.6.0" + versionCode = 9 + versionName = "0.6.1" } signingConfigs { diff --git a/app/src/main/java/at/daschmidt/backrestwidget/BackrestWidget.kt b/app/src/main/java/at/daschmidt/backrestwidget/BackrestWidget.kt index 3ef5b6d..9608121 100644 --- a/app/src/main/java/at/daschmidt/backrestwidget/BackrestWidget.kt +++ b/app/src/main/java/at/daschmidt/backrestwidget/BackrestWidget.kt @@ -142,6 +142,7 @@ class BackrestWidget : AppWidgetProvider() { val next = plan.nextBackupMs val remote = snapshot.remotes.firstOrNull { it.instance == plan.instance } val detail = when { + remote != null && remote.stale -> "Status unbekannt" remote != null && !remote.connected -> "getrennt" next != null -> "nächstes ${Format.dayAndTime(context, next)}" else -> "kein Zeitplan" diff --git a/app/src/main/java/at/daschmidt/backrestwidget/MainActivity.kt b/app/src/main/java/at/daschmidt/backrestwidget/MainActivity.kt index 5812fbb..1213da9 100644 --- a/app/src/main/java/at/daschmidt/backrestwidget/MainActivity.kt +++ b/app/src/main/java/at/daschmidt/backrestwidget/MainActivity.kt @@ -160,11 +160,19 @@ class MainActivity : Activity() { view.findViewById(R.id.plan_name).text = plan.id view.findViewById(R.id.plan_remote).apply { visibility = if (plan.isRemote) View.VISIBLE else View.GONE - text = listOfNotNull( - "Remote-Instanz ${plan.instance}", - remote?.let { Format.connectionLabel(it.state) }, - remote?.lastHeartbeatMs?.let { "Kontakt ${Format.relative(it)}" }, - ).joinToString(" · ") + text = if (remote?.stateAsOfMs != null) { + listOfNotNull( + "Remote-Instanz ${plan.instance}", + "Status nicht abrufbar (${remote.fetchError ?: "unbekannter Fehler"})", + "Stand ${Format.dateTime(this@MainActivity, remote.stateAsOfMs)}", + ).joinToString(" · ") + } else { + listOfNotNull( + "Remote-Instanz ${plan.instance}", + remote?.let { Format.connectionLabel(it.state) }, + remote?.lastHeartbeatMs?.let { "Kontakt ${Format.relative(it)}" }, + ).joinToString(" · ") + } } val last = plan.lastFinished diff --git a/app/src/main/java/at/daschmidt/backrestwidget/Models.kt b/app/src/main/java/at/daschmidt/backrestwidget/Models.kt index 746cd51..072687d 100644 --- a/app/src/main/java/at/daschmidt/backrestwidget/Models.kt +++ b/app/src/main/java/at/daschmidt/backrestwidget/Models.kt @@ -222,10 +222,29 @@ data class RemoteState( val state: String, val lastHeartbeatMs: Long?, val planIds: List, + /** + * Set when the current refresh could not read peer states and this entry + * was carried over from an earlier snapshot: when that state was fetched. + */ + val stateAsOfMs: Long? = null, + /** Why the peer states could not be read (only with [stateAsOfMs]). */ + val fetchError: String? = null, ) { - val connected: Boolean get() = state == "connected" + /** The state is an old copy, not what the server reports right now. */ + val stale: Boolean get() = stateAsOfMs != null + + /** Only a freshly fetched state can claim a live connection. */ + val connected: Boolean get() = state == "connected" && !stale companion object { + /** + * Keep the remotes of the previous snapshot when peer states could not + * be read, but mark them as an old copy so they are never shown as + * current. An entry that is already stale keeps its original date. + */ + fun carriedOver(cached: List, cachedAtMs: Long, error: String): List = + cached.map { it.copy(stateAsOfMs = it.stateAsOfMs ?: cachedAtMs, fetchError = error) } + /** * Parse a PeerState message. Only plan IDs are taken from the remote * config: its repositories carry plaintext passwords and are ignored. @@ -310,6 +329,8 @@ data class Snapshot( put("instance", r.instance) put("state", r.state) r.lastHeartbeatMs?.let { put("lastHeartbeatMs", it) } + r.stateAsOfMs?.let { put("stateAsOfMs", it) } + r.fetchError?.let { put("fetchError", it) } put("planIds", JSONArray(r.planIds)) } })) @@ -348,6 +369,8 @@ data class Snapshot( state = r.optString("state", "unknown"), lastHeartbeatMs = if (r.has("lastHeartbeatMs")) r.getLong("lastHeartbeatMs") else null, planIds = if (ids == null) emptyList() else (0 until ids.length()).map { ids.getString(it) }, + stateAsOfMs = if (r.has("stateAsOfMs")) r.getLong("stateAsOfMs") else null, + fetchError = r.optString("fetchError").ifEmpty { null }, ) } val repos = o.optJSONArray("repos").objects().map { r -> diff --git a/app/src/main/java/at/daschmidt/backrestwidget/Notifier.kt b/app/src/main/java/at/daschmidt/backrestwidget/Notifier.kt index 3ef3b6e..f4625fb 100644 --- a/app/src/main/java/at/daschmidt/backrestwidget/Notifier.kt +++ b/app/src/main/java/at/daschmidt/backrestwidget/Notifier.kt @@ -45,9 +45,12 @@ object AlertRules { fun unreachable(failedRefreshes: Int, threshold: Int, alreadyNotified: Boolean): Boolean = !alreadyNotified && failedRefreshes >= threshold - /** Remotes that count as disconnected: not connected and without a recent heartbeat. */ + /** + * Remotes that count as disconnected: not connected and without a recent heartbeat. + * An old copy (peer states unreadable) proves nothing about the remote and is skipped. + */ fun disconnected(remotes: List, nowMs: Long): List = remotes.filter { - !it.connected && (it.lastHeartbeatMs == null || nowMs - it.lastHeartbeatMs > REMOTE_SILENCE_MS) + !it.stale && !it.connected && (it.lastHeartbeatMs == null || nowMs - it.lastHeartbeatMs > REMOTE_SILENCE_MS) } } diff --git a/app/src/main/java/at/daschmidt/backrestwidget/Refresh.kt b/app/src/main/java/at/daschmidt/backrestwidget/Refresh.kt index 89db78e..29a8de5 100644 --- a/app/src/main/java/at/daschmidt/backrestwidget/Refresh.kt +++ b/app/src/main/java/at/daschmidt/backrestwidget/Refresh.kt @@ -33,7 +33,13 @@ object Refresher { val operations = runCatching { client.recentOperations() }.getOrNull() // Multihost: client instances of this server, e.g. VPS backed up via Backrest. val remotes = runCatching { client.peerStates().mapNotNull { RemoteState.from(it) } } - .getOrElse { if (it is AuthException) throw it else settings.snapshot?.remotes.orEmpty() } + .getOrElse { + if (it is AuthException) throw it + // Show the last known remotes as an old copy instead of passing them off as current. + val cached = settings.snapshot + val error = if (it is SocketTimeoutException) "Zeitüberschreitung" else it.message ?: it.javaClass.simpleName + RemoteState.carriedOver(cached?.remotes.orEmpty(), cached?.fetchedAtMs ?: now, error) + } val remoteOperations = remotes.flatMap { remote -> remote.planIds.map { remote.instance to it } } .mapNotNull { (instance, planId) -> runCatching { "$instance/$planId" to client.planOperations(instance, planId) }.getOrNull() diff --git a/app/src/test/java/at/daschmidt/backrestwidget/AlertRulesTest.kt b/app/src/test/java/at/daschmidt/backrestwidget/AlertRulesTest.kt index 7386623..7dec80c 100644 --- a/app/src/test/java/at/daschmidt/backrestwidget/AlertRulesTest.kt +++ b/app/src/test/java/at/daschmidt/backrestwidget/AlertRulesTest.kt @@ -91,4 +91,11 @@ class AlertRulesTest { ) assertEquals(listOf("gone", "never"), AlertRules.disconnected(remotes, now).map { it.instance }) } + + @Test + fun oldCopyOfRemoteDoesNotAlert() { + // Peer states unreadable: an old copy says nothing about the remote itself. + val old = RemoteState.carriedOver(listOf(RemoteState("vps", "connected", now - 3 * 24 * hour, emptyList())), now - hour, "Zeitüberschreitung") + assertTrue(AlertRules.disconnected(old, now).isEmpty()) + } } diff --git a/app/src/test/java/at/daschmidt/backrestwidget/SnapshotTest.kt b/app/src/test/java/at/daschmidt/backrestwidget/SnapshotTest.kt index ff80779..9f20a26 100644 --- a/app/src/test/java/at/daschmidt/backrestwidget/SnapshotTest.kt +++ b/app/src/test/java/at/daschmidt/backrestwidget/SnapshotTest.kt @@ -198,6 +198,36 @@ class RemoteInstancesTest { assertFalse(remote.toString().contains("fixture-rest-password")) } + @Test + fun carriedOverRemoteIsNotShownAsConnected() { + val live = RemoteState("netbird-vps", "connected", 1_000L, listOf("netbird-system")) + val old = RemoteState.carriedOver(listOf(live), 5_000L, "Zeitüberschreitung").single() + assertFalse(old.connected) + assertTrue(old.stale) + assertEquals(5_000L, old.stateAsOfMs) + assertEquals("Zeitüberschreitung", old.fetchError) + assertEquals(listOf("netbird-system"), old.planIds) + // A second failed refresh keeps the date of the last real fetch. + val older = RemoteState.carriedOver(listOf(old), 9_000L, "Keine Antwort zu Remote-Instanzen").single() + assertEquals(5_000L, older.stateAsOfMs) + assertEquals("Keine Antwort zu Remote-Instanzen", older.fetchError) + } + + @Test + fun carriedOverStateSurvivesTheCache() { + val dashboard = JSONObject("""{"planSummaries":[{"id":"a"}]}""") + val old = RemoteState.carriedOver(listOf(RemoteState("vps", "connected", 1_000L, emptyList())), 5_000L, "Zeitüberschreitung") + val restored = Snapshot.fromJson(Snapshot.fromDashboard(dashboard, 9_000L, null, old).toJson()).remotes.single() + assertEquals(5_000L, restored.stateAsOfMs) + assertEquals("Zeitüberschreitung", restored.fetchError) + assertFalse(restored.connected) + val fresh = Snapshot.fromJson( + Snapshot.fromDashboard(dashboard, 9_000L, null, listOf(RemoteState("vps", "connected", 1_000L, emptyList()))).toJson() + ).remotes.single() + assertNull(fresh.stateAsOfMs) + assertTrue(fresh.connected) + } + @Test fun peerWithoutConfigUsesKnownPlans() { val remote = RemoteState.from(