package main import ( "context" "fmt" "io" "time" ) // WatchEvery is how often the watch looks at the pools. const WatchEvery = time.Minute // The module's one long-running process (`zfs-tools watch`), run by the node-engine. It is what the // module's health is declared on (novox/hq ADR 0240: a tool check stands on a long-running resource): the // node-engine judges the process alive, and asks zfs_health whether the pools are. The watch itself // writes no file and changes nothing: each minute it reads the pools, and says in its journal when their // health changes, so the journal holds when a pool went DEGRADED and when it came back. // Watch keeps the last verdict, to say only changes. type Watch struct { M *Machine Out io.Writer last string } // Look reads the pools once and says the verdict when it changed. func (w *Watch) Look(ctx context.Context) { h := w.M.Health(ctx) verdict := "healthy" if !h.Healthy { verdict = "UNHEALTHY" } line := verdict + ": " + h.Why if line != w.last { fmt.Fprintf(w.Out, "%s %s\n", w.M.Now().Format(time.RFC3339), line) w.last = line } } // Run looks every WatchEvery until the context ends. func (w *Watch) Run(ctx context.Context) { w.Look(ctx) t := time.NewTicker(WatchEvery) defer t.Stop() for { select { case <-ctx.Done(): return case <-t.C: w.Look(ctx) } } }