From 6a43bf1e10bd047ec4779ea3b5c1091a8435e0b9 Mon Sep 17 00:00:00 2001 From: DerInti Date: Sun, 28 Jul 2024 18:32:58 +0200 Subject: [PATCH] Add fsnotify handling --- Containerfile | 15 +++++++++++++++ cmd/root.go | 18 ++++++++++-------- go.mod | 2 +- go.sum | 2 ++ input/input.yml | 3 +++ 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 Containerfile diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..1724b1e --- /dev/null +++ b/Containerfile @@ -0,0 +1,15 @@ +FROM golang:alpine as builder + +ENV CGO_ENABLED 0 + +RUN go env -w GOCACHE=/go-cache +RUN go env -w GOMODCACHE=/gomod-cache +COPY . /build +WORKDIR /build +RUN --mount=type=cache,target=/gomod-cache --mount=type=cache,target=/go-cache go build -o test ./cmd + +FROM scratch + +COPY --from=builder /build/test / +ENTRYPOINT ["./test"] + diff --git a/cmd/root.go b/cmd/root.go index 6758511..a6a1c2a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "log/slog" "math/rand/v2" "os" + "path" "time" "github.com/fsnotify/fsnotify" @@ -37,7 +38,7 @@ func (db InputDb) Lookup(key int) string { func main() { var db InputDb done := make(chan struct{}, 1) - ticker := time.NewTicker(3 * time.Millisecond) + ticker := time.NewTicker(100 * time.Millisecond) watcher, err := fsnotify.NewWatcher() if err != nil { @@ -47,9 +48,9 @@ func main() { newInputDbFromFile("input/input.yml", &db) slog.Info("Loaded DB", "db", db) - err = watcher.Add("input/input.yml") + err = watcher.Add("input/") if err != nil { - slog.Error("Could not watch path", "path", "input/input.yml") + slog.Error("Could not watch path", "path", "input/") } go func() { @@ -59,12 +60,13 @@ func main() { key := rand.IntN(9) + 1 value := db.Lookup(key) slog.Info("Got Rand", "key", key, "value", value, "ticker", t) - case event, ok := <-watcher.Events: - if !ok { - slog.Error("Could not log Event", "event.name", event.Name, "event.op", event.Op) + case event := <-watcher.Events: + if event.Has(fsnotify.Write) && event.Name == "input/input.yml" { + slog.Info("Got Event", "event.name", event.Name, "event.name.base", path.Base(event.Name), "event.name.dir", path.Dir(event.Name), "event.op", event.Op, "watchlist", watcher.WatchList()) + newInputDbFromFile("input/input.yml", &db) } - slog.Info("Got Event", "event.name", event.Name, "event.op", event.Op) - newInputDbFromFile("input/input.yml", &db) + case err := <-watcher.Errors: + slog.Error("Could not watch", "error", err) } } }() diff --git a/go.mod b/go.mod index 1377d57..4ae37d9 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 ) -require golang.org/x/sys v0.4.0 // indirect +require golang.org/x/sys v0.22.0 // indirect diff --git a/go.sum b/go.sum index 37fe95d..f635c7f 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/input/input.yml b/input/input.yml index 40b57a9..3f13e98 100644 --- a/input/input.yml +++ b/input/input.yml @@ -5,3 +5,6 @@ 4: D 5: E 6: F +7: G +8: H +9: I