From c0b2fd5f21272ce300dfcc0ac4121b01a906d8a6 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 14 Mar 2023 20:00:04 +0100 Subject: [PATCH] do not treat snapshot check failures as fatal errors --- main.go | 19 +++++++------------ restic/wrapper.go | 6 ++++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index d0279fe..0765e64 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "brainbaking.com/restictray/restic" - "errors" "fyne.io/systray" "github.com/rs/zerolog" "github.com/rs/zerolog/log" @@ -34,7 +33,7 @@ func addMenuError(err error) { }() } -func handleError(err error) { +func handleFatalError(err error) { log.Err(err).Msg("") systray.ResetMenu() addMenuError(err) @@ -64,16 +63,12 @@ func main() { } // Updates snapshot information in restic wrapper and menu labels. This is a blocking call. +// This should be resilient as it's frequently called. func updateSnapshots(cnf *restic.Config, mnu *resticmenu) { err := wrapper.UpdateLatestSnapshots(cnf) if err != nil { - if errors.Is(err, restic.ResticCmdTimeoutError) { - log.Warn().Err(err).Msg("update snapshot timeout") - mnu.latestSnapshot.SetTitle("‼️ Latest: timeout while updating") - mnu.backupNowEnable() - } else { - handleError(err) - } + log.Warn().Err(err).Msg("update snapshot") + mnu.latestSnapshot.SetTitle("‼️ " + err.Error()) return } @@ -131,7 +126,7 @@ func onSystrayReady() { cnf, err := restic.ReadConfig() if err != nil { - handleError(err) + handleFatalError(err) return } @@ -139,7 +134,7 @@ func onSystrayReady() { backupCheckTime := make(chan bool, 1) hourlyBackupCheckFn := func() { - time.Sleep(1 * time.Minute) + time.Sleep(1 * time.Hour) backupCheckTime <- true } go hourlyBackupCheckFn() @@ -217,7 +212,7 @@ func buildMenu() *resticmenu { func mountBackups(cnf *restic.Config) { err := wrapper.MountBackups(cnf) if err != nil { - handleError(err) + handleFatalError(err) } } diff --git a/restic/wrapper.go b/restic/wrapper.go index 04a5fd2..8d8ae77 100644 --- a/restic/wrapper.go +++ b/restic/wrapper.go @@ -110,6 +110,7 @@ func openFolder(folder string) error { } // UpdateLatestSnapshots updates LatestSnapshots or returns an error. If timed out, returns an error as well. +// Returns a ResticCmdTimeoutError if no response from restic after cmdTimeout func (w *Wrapper) UpdateLatestSnapshots(c *Config) error { cmd := resticCmd("--json", "--password-file", PasswordFile(), "-r", c.Repository, "snapshots") @@ -125,11 +126,11 @@ func (w *Wrapper) UpdateLatestSnapshots(c *Config) error { case <-done: case <-time.After(cmdTimeout): cmd.Process.Kill() - return fmt.Errorf("restic snapshots cmd: %w", ResticCmdTimeoutError) + return fmt.Errorf("snapshot cmd: %w", ResticCmdTimeoutError) } if err != nil { - return fmt.Errorf("restic snapshots cmd: %s: %w", string(out), err) + return fmt.Errorf("snapshot cmd: %s: %w", string(out), err) } err = json.Unmarshal(out, &w.LatestSnapshots) @@ -142,6 +143,7 @@ func (w *Wrapper) UpdateLatestSnapshots(c *Config) error { } // Backup uses "restic backup" to create a new snapshot. This is a blocking call. +// Returns a ResticCmdTimeoutError if no response from restic after cmdTimeout func (w *Wrapper) Backup(c *Config) error { cmd := resticCmd("--password-file", PasswordFile(), "-r", c.Repository, "--exclude-file", ExcludeFile(), "backup", c.Backup, "--no-scan") stdout, _ := cmd.StdoutPipe()