Skip to content

Commit

Permalink
feat: watch property file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Aug 18, 2024
1 parent 6e5cb46 commit 8b858fe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/flanksource/gomplate/v3 v3.24.22 // indirect
github.com/flanksource/is-healthy v1.0.26 // indirect
github.com/flanksource/kubectl-neat v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ github.com/envoyproxy/protoc-gen-validate v0.9.1/go.mod h1:OKNgG7TCp5pF4d6XftA0+
github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA=
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
Expand All @@ -707,6 +709,8 @@ github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
12 changes: 6 additions & 6 deletions logger/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,12 @@ func (s SlogLogger) Debugf(format string, args ...interface{}) {
}

func (s SlogLogger) handle(r slog.Record, format string, args ...interface{}) {
caller := GetCaller(r.PC)
if fileLogger, ok := namedLoggers.Load(caller); ok {
if !fileLogger.IsLevelEnabled(FromSlogLevel(r.Level)) {
return
}
}
// caller := GetCaller(r.PC)
// if fileLogger, ok := namedLoggers.Load(caller); ok {
// if !fileLogger.IsLevelEnabled(FromSlogLevel(r.Level)) {
// return
// }
// }
if jsonLogs {
if s.Prefix != "" {
r.Add("logger", s.Prefix)
Expand Down
49 changes: 43 additions & 6 deletions properties/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@ import (
"strconv"
"strings"
"sync"

"github.com/fsnotify/fsnotify"
)

var Global = &Properties{
m: make(map[string]string),
}

var LoadFile = func(filename string) error {
return Global.LoadFile(filename)
}

type Properties struct {
m map[string]string
filename string
listeners []func(*Properties)
lock sync.RWMutex
close func()
Reload func()
}

func (p *Properties) RegisterListener(fn func(*Properties)) {
Expand All @@ -28,9 +37,9 @@ func (p *Properties) RegisterListener(fn func(*Properties)) {

func (p *Properties) Set(key string, value any) {
p.lock.Lock()
defer p.notify()
defer p.lock.Unlock()
p.m[key] = fmt.Sprintf("%v", value)
p.notify()
}

func (p *Properties) notify() {
Expand Down Expand Up @@ -63,10 +72,6 @@ func (p *Properties) Update(props map[string]string) {
}
}

func LoadFile(filename string) error {
return Global.LoadFile(filename)
}

func (p *Properties) LoadFile(filename string) error {
if !path.IsAbs(filename) {
cwd, _ := os.Getwd()
Expand All @@ -80,7 +85,13 @@ func (p *Properties) LoadFile(filename string) error {
return err
}
defer file.Close()
slog.Info(fmt.Sprintf("loading properties from %s", filename))
p.filename = filename

if p.close == nil {
p.close = p.Watch()
}

slog.Info(fmt.Sprintf("Loading properties from %s", filename))
var props = make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
Expand Down Expand Up @@ -161,3 +172,29 @@ func (p *Properties) Int(def int, key string) int {
}
return def
}

func (p *Properties) Watch() func() {
if p.close != nil {
return p.close
}
slog.Info(fmt.Sprintf("Watching %s for changes", p.filename))
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
slog.Warn("Failed to create watcher for properties file: " + err.Error())
}

_ = watcher.Add(path.Dir(p.filename))

go func() {

for e := range watcher.Events {
if e.Name == p.filename && e.Op != fsnotify.Chmod {
p.LoadFile(p.filename)
}
}
}()
return func() {
_ = watcher.Close()
}
}

0 comments on commit 8b858fe

Please sign in to comment.