From 242ccd3b70ce5d36f0d197d652b039b626927c84 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 12:12:22 +0100 Subject: [PATCH] fix(#516): don't retry a run only blocked series failed, back up on any interval switch --- .../calendula/data/backup/AutoBackup.kt | 10 +++++-- .../calendula/data/prefs/SettingsPrefs.kt | 28 +++++++++++++++---- .../ui/calendars/CalendarsViewModel.kt | 17 +++++------ .../calendula/data/prefs/SettingsPrefsTest.kt | 11 ++++++++ docs/ARCHITECTURE.md | 7 +++-- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt index 8b08ac63..084045f9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt @@ -78,8 +78,8 @@ object BackupScheduler { /** * Reconcile the scheduled work with the saved settings: enqueue a unique - * periodic request when backup is on and a folder is set, otherwise cancel - * it. WorkManager persists the request across reboots on its own. + * periodic request when backup is on, a folder is set and the interval + * isn't switched off (#516), otherwise cancel it. WorkManager persists the request across reboots on its own. * * The first run is delayed by one interval so it doesn't follow an immediate * [runNow] with a near-identical copy. With backing up after changes on @@ -267,7 +267,8 @@ class BackupWorker( return if (outcome == RunOutcome.Failed && !changeRun) Result.retry() else Result.success() } - private enum class RunOutcome { Written, PartlyFailed, Failed, NothingToDo } + /** [Blocked]: failed only on series no retry can back up, so it isn't retried. */ + private enum class RunOutcome { Written, PartlyFailed, Failed, Blocked, NothingToDo } /** How a stable copy's write went: under its own name, under one the provider picked, or not at all. */ private enum class LatestWrite { Landed, Renamed, Failed } @@ -397,11 +398,13 @@ class BackupWorker( var written = 0 var failed = 0 + var blocked = 0 val landedLatest = mutableSetOf() for (plan in plans) { val target = plan.target if (!plan.backedUp(intervalOn)) { failed++ + blocked++ continue } if (changeRun) { @@ -447,6 +450,7 @@ class BackupWorker( return when { failed == 0 && written == 0 -> RunOutcome.NothingToDo failed == 0 -> RunOutcome.Written + written == 0 && failed == blocked -> RunOutcome.Blocked written == 0 -> RunOutcome.Failed else -> RunOutcome.PartlyFailed } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt index 9e707163..15cabfea 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt @@ -231,6 +231,10 @@ class SettingsPrefs @Inject constructor( val autoBackupLedger: Flow = prefs.flow(SettingsKeys.autoBackupLedger) val autoBackupOnChange: Flow = prefs.flow(SettingsKeys.autoBackupOnChange) val autoBackupScheduled: Flow = prefs.snapshot { it.autoBackupScheduled } + + /** Read together, so the screen never shows one updated without the other. */ + val autoBackupOnChangeAndScheduled: Flow> = + prefs.snapshot { it[SettingsKeys.autoBackupOnChange] to it.autoBackupScheduled } val autoBackupFingerprints: Flow = prefs.flow(SettingsKeys.autoBackupFingerprints) val specialDatesEnabled: Flow = prefs.flow(SettingsKeys.specialDatesEnabled) val specialDatesTypes: Flow> = prefs.snapshot { it.specialDatesTypes } @@ -525,13 +529,27 @@ class SettingsPrefs @Inject constructor( suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep) - suspend fun setAutoBackupOnChange(enabled: Boolean) { - prefs.edit { applyAutoBackupOnChange(it, enabled) } + /** Returns whether this switched the interval on or off. */ + suspend fun setAutoBackupOnChange(enabled: Boolean): Boolean = editAutoBackupScheduled { + applyAutoBackupOnChange(it, enabled) } - /** Off only sticks while backing up after changes is on; see [applyAutoBackupOnChange]. */ - suspend fun setAutoBackupScheduled(enabled: Boolean) { - prefs.edit { if (enabled || it[SettingsKeys.autoBackupOnChange]) it[SettingsKeys.autoBackupScheduledStored] = enabled } + /** + * Off only sticks while backing up after changes is on; see + * [applyAutoBackupOnChange]. Returns whether this switched the interval. + */ + suspend fun setAutoBackupScheduled(enabled: Boolean): Boolean = editAutoBackupScheduled { + if (enabled || it[SettingsKeys.autoBackupOnChange]) it[SettingsKeys.autoBackupScheduledStored] = enabled + } + + private suspend fun editAutoBackupScheduled(transform: (MutablePreferences) -> Unit): Boolean { + var switched = false + prefs.edit { + val before = it.autoBackupScheduled + transform(it) + switched = it.autoBackupScheduled != before + } + return switched } /** diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt index b57cf0d0..2e81d0c9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/CalendarsViewModel.kt @@ -109,7 +109,7 @@ class CalendarsViewModel @Inject constructor( settingsPrefs.autoBackupSelection, settingsPrefs.autoBackupCombined, settingsPrefs.autoBackupKeep, - combine(settingsPrefs.autoBackupOnChange, settingsPrefs.autoBackupScheduled, ::Pair), + settingsPrefs.autoBackupOnChangeAndScheduled, ) { base, selection, combined, keep, (onChange, scheduled) -> base.copy(selection = selection, combined = combined, keep = keep, onChange = onChange, scheduled = scheduled) } @@ -296,20 +296,21 @@ class CalendarsViewModel @Inject constructor( fun setAutoBackupOnChange(enabled: Boolean) { viewModelScope.launch { - val wasScheduled = settingsPrefs.autoBackupScheduled.first() - settingsPrefs.setAutoBackupOnChange(enabled) + val switched = settingsPrefs.setAutoBackupOnChange(enabled) reschedule() - if (!wasScheduled && settingsPrefs.autoBackupScheduled.first()) runBackupIfActive() + if (switched) runBackupIfActive() } } - /** Back on, it backs up right away rather than an interval later. */ + /** + * Switching the interval backs up right away: back on, rather than an + * interval later; off, so a series only its dated files covered shows up. + */ fun setAutoBackupScheduled(enabled: Boolean) { viewModelScope.launch { - val wasScheduled = settingsPrefs.autoBackupScheduled.first() - settingsPrefs.setAutoBackupScheduled(enabled) + val switched = settingsPrefs.setAutoBackupScheduled(enabled) reschedule() - if (!wasScheduled && settingsPrefs.autoBackupScheduled.first()) runBackupIfActive() + if (switched) runBackupIfActive() } } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt index 3846afa5..587add9f 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt @@ -962,6 +962,17 @@ class SettingsPrefsTest { assertThat(prefs.autoBackupScheduled.first()).isTrue() } + @Test + fun `the interval setters report whether they switched it`(@TempDir tempDir: Path) = runTest { + val prefs = testSettings(tempDir) + assertThat(prefs.setAutoBackupScheduled(false)).isFalse() + assertThat(prefs.setAutoBackupOnChange(true)).isFalse() + assertThat(prefs.setAutoBackupScheduled(false)).isTrue() + assertThat(prefs.setAutoBackupScheduled(false)).isFalse() + assertThat(prefs.setAutoBackupOnChange(false)).isTrue() + assertThat(prefs.autoBackupOnChangeAndScheduled.first()).isEqualTo(false to true) + } + @Test fun `switching the interval off without after-change backups leaves nothing behind`(@TempDir tempDir: Path) = runTest { val prefs = testSettings(tempDir) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f57cd554..9d51307d 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -647,9 +647,10 @@ While the option is on, the interval itself can be switched off off without the option): the periodic work is cancelled and no run writes or prunes dated files. Run-now then rewrites every stable copy, change runs only the changed ones, and a series whose stable name a file of the user's holds -counts as failed, since nothing else backs it up. Turning the option off sets -the interval back on, at the interval it had, and turning the interval back on -either way backs up right away. +counts as failed (not retried), since nothing else backs it up. Turning the +option off sets the interval back on, at the interval it had; any switch of the +interval backs up right away. What the text above leaves to the next scheduled +run then waits for the next run-now. **A backup carries the series' deleted and moved occurrences.** `EXDATE` lives on the master row and is part of `EventExportProjection`; `IcsWriter` marks an