From b8abeed6c7bddb9d9770f3be93dc41400354783b Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Thu, 13 Jun 2019 07:58:04 +0200 Subject: [PATCH] nsscash: write state on each successful run --- README | 4 ++++ main.go | 4 +++- state.go | 18 ++++-------------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/README b/README index 059fe51..510ba33 100644 --- a/README +++ b/README @@ -95,6 +95,10 @@ running processes! Now configure `nsscash` to run regularly, for example via cron or a systemd timer. +To monitor `nsscash` for errors one can use the last modification time of the +state file (see below). It's written on each successful run and not modified +if an error occurs. + === CONFIGURATION Nsscash is configured through a simple configuration file written in TOML. A diff --git a/main.go b/main.go index b10f57e..5272740 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,9 @@ func main() { if err != nil { log.Fatal(err) } - err = WriteStateIfChanged(cfg.StatePath, state) + // NOTE: Make sure to call WriteState() only if there were no + // errors (see WriteState() and README) + err = WriteState(cfg.StatePath, state) if err != nil { log.Fatal(err) } diff --git a/state.go b/state.go index b0dec1c..fa8dccd 100644 --- a/state.go +++ b/state.go @@ -22,15 +22,11 @@ import ( "io/ioutil" "os" "path/filepath" - "reflect" "time" ) type State struct { LastModified map[string]time.Time - - // Copy of LastModified to write the state file only on changes - origLastModified map[string]time.Time } func LoadState(path string) (*State, error) { @@ -54,19 +50,13 @@ func LoadState(path string) (*State, error) { state.LastModified = make(map[string]time.Time) } - state.origLastModified = make(map[string]time.Time) - for k, v := range state.LastModified { - state.origLastModified[k] = v - } - return &state, nil } -func WriteStateIfChanged(path string, state *State) error { - // State hasn't changed, nothing to do - if reflect.DeepEqual(state.LastModified, state.origLastModified) { - return nil - } +func WriteState(path string, state *State) error { + // Update the state file even if nothing has changed to provide a + // simple way to check if nsscash ran successfully (the state is only + // updated if there were no errors) x, err := json.Marshal(state) if err != nil { -- 2.45.2