Add fsnotify handling
This commit is contained in:
15
Containerfile
Normal file
15
Containerfile
Normal file
@@ -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"]
|
||||||
|
|
||||||
18
cmd/root.go
18
cmd/root.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
@@ -37,7 +38,7 @@ func (db InputDb) Lookup(key int) string {
|
|||||||
func main() {
|
func main() {
|
||||||
var db InputDb
|
var db InputDb
|
||||||
done := make(chan struct{}, 1)
|
done := make(chan struct{}, 1)
|
||||||
ticker := time.NewTicker(3 * time.Millisecond)
|
ticker := time.NewTicker(100 * time.Millisecond)
|
||||||
|
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -47,9 +48,9 @@ func main() {
|
|||||||
newInputDbFromFile("input/input.yml", &db)
|
newInputDbFromFile("input/input.yml", &db)
|
||||||
slog.Info("Loaded DB", "db", db)
|
slog.Info("Loaded DB", "db", db)
|
||||||
|
|
||||||
err = watcher.Add("input/input.yml")
|
err = watcher.Add("input/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Could not watch path", "path", "input/input.yml")
|
slog.Error("Could not watch path", "path", "input/")
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -59,12 +60,13 @@ func main() {
|
|||||||
key := rand.IntN(9) + 1
|
key := rand.IntN(9) + 1
|
||||||
value := db.Lookup(key)
|
value := db.Lookup(key)
|
||||||
slog.Info("Got Rand", "key", key, "value", value, "ticker", t)
|
slog.Info("Got Rand", "key", key, "value", value, "ticker", t)
|
||||||
case event, ok := <-watcher.Events:
|
case event := <-watcher.Events:
|
||||||
if !ok {
|
if event.Has(fsnotify.Write) && event.Name == "input/input.yml" {
|
||||||
slog.Error("Could not log Event", "event.name", event.Name, "event.op", event.Op)
|
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)
|
case err := <-watcher.Errors:
|
||||||
newInputDbFromFile("input/input.yml", &db)
|
slog.Error("Could not watch", "error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -7,4 +7,4 @@ require (
|
|||||||
gopkg.in/yaml.v3 v3.0.1
|
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
|
||||||
|
|||||||
2
go.sum
2
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=
|
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 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
@@ -5,3 +5,6 @@
|
|||||||
4: D
|
4: D
|
||||||
5: E
|
5: E
|
||||||
6: F
|
6: F
|
||||||
|
7: G
|
||||||
|
8: H
|
||||||
|
9: I
|
||||||
|
|||||||
Reference in New Issue
Block a user