Add fsnotify handling
This commit is contained in:
18
cmd/root.go
18
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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user