Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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 <Datum>“, das Widget zeigt „Status unbekannt“.
- Ein alter Stand löst keine „getrennt“-Benachrichtigung aus, weil er über die Remote-Instanz selbst nichts aussagt.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/at/daschmidt/backrestwidget/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ class MainActivity : Activity() {
view.findViewById<TextView>(R.id.plan_name).text = plan.id
view.findViewById<TextView>(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
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/java/at/daschmidt/backrestwidget/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,29 @@ data class RemoteState(
val state: String,
val lastHeartbeatMs: Long?,
val planIds: List<String>,
/**
* 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<RemoteState>, cachedAtMs: Long, error: String): List<RemoteState> =
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.
Expand Down Expand Up @@ -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))
}
}))
Expand Down Expand Up @@ -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 ->
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/at/daschmidt/backrestwidget/Notifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<RemoteState>, nowMs: Long): List<RemoteState> = 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)
}
}

Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/at/daschmidt/backrestwidget/Refresh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
30 changes: 30 additions & 0 deletions app/src/test/java/at/daschmidt/backrestwidget/SnapshotTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading