From 78d6841bd13ece95d3d738b3b9e920403626cd3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Tue, 2 Aug 2022 20:59:27 +0200 Subject: [PATCH] Refactor configuration reload --- cmd/fermentord/loop.go | 3 ++- cmd/fermentord/main.go | 4 ++-- internal/configuration/config.go | 11 ++++++++--- internal/configuration/observer.go | 15 ++++++++++++++- internal/lcd/hd44780.go | 3 ++- 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cmd/fermentord/loop.go b/cmd/fermentord/loop.go index 8924577..b3fddf2 100644 --- a/cmd/fermentord/loop.go +++ b/cmd/fermentord/loop.go @@ -38,7 +38,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { chConfigChange := make(chan configuration.Configuration, 1) chn := configuration.NewChangeNotifier() - viper.OnConfigChange(chn.OnConfigChange) + defer chn.Close() // Configuration updates chn.Subscribe(chConfigChange) @@ -46,6 +46,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) { chn.Subscribe(ctrl.ConfigUpdates) //chn.Subscribe(display.ConfiguUpdate) chn.Notify(configuration.Global()) + viper.OnConfigChange(chn.OnConfigChange) viper.WatchConfig() // NATS diff --git a/cmd/fermentord/main.go b/cmd/fermentord/main.go index 8c0f262..1996348 100644 --- a/cmd/fermentord/main.go +++ b/cmd/fermentord/main.go @@ -29,10 +29,10 @@ func main() { defer sentry.Flush(10 * time.Second) // Configuration - configuration.LoadConfiguration() + configuration.Initialize() + config := configuration.LoadConfiguration() // NATS - config := configuration.Global() servers := strings.Join(config.NATS.Servers, ",") userInfo := nats.UserInfo(config.NATS.Username, config.NATS.Password) nc, err := nats.Connect(servers, userInfo) diff --git a/internal/configuration/config.go b/internal/configuration/config.go index 9759cf1..0adbeb1 100644 --- a/internal/configuration/config.go +++ b/internal/configuration/config.go @@ -24,7 +24,7 @@ func Global() (c Configuration) { return } -func LoadConfiguration() { +func Initialize() { setDefaults() viper.AddConfigPath("/etc") @@ -32,6 +32,11 @@ func LoadConfiguration() { viper.AddConfigPath(".") viper.SetConfigName("fermentord") viper.SetConfigType("toml") +} + +func LoadConfiguration() Configuration { + globalLock.Lock() + defer globalLock.Unlock() if err := viper.ReadInConfig(); err != nil { log.Printf("Error loading configuration: %v", err) @@ -43,7 +48,7 @@ func LoadConfiguration() { log.Fatal(err) } - globalLock.Lock() globalConfig = *config - globalLock.Unlock() + + return globalConfig } diff --git a/internal/configuration/observer.go b/internal/configuration/observer.go index 00d0b6d..7401069 100644 --- a/internal/configuration/observer.go +++ b/internal/configuration/observer.go @@ -1,21 +1,30 @@ package configuration import ( + "fmt" "log" + "time" "github.com/fsnotify/fsnotify" + "github.com/getsentry/sentry-go" ) type ChangeNotifier struct { channels []chan<- Configuration + hub *sentry.Hub } func NewChangeNotifier() *ChangeNotifier { return &ChangeNotifier{ channels: make([]chan<- Configuration, 0), + hub: sentry.CurrentHub().Clone(), } } +func (p *ChangeNotifier) Close() { + p.hub.Flush(10 * time.Second) +} + func (p *ChangeNotifier) Subscribe(ch chan<- Configuration) { p.channels = append(p.channels, ch) } @@ -24,13 +33,17 @@ func (p *ChangeNotifier) Notify(config Configuration) { for _, ch := range p.channels { select { case ch <- config: + default: + err := fmt.Errorf("channel overflow on a ChangeNotifier notification channel") + p.hub.CaptureException(err) + log.Print(err) } } } func (p *ChangeNotifier) OnConfigChange(in fsnotify.Event) { log.Print("Reloading configuration") - config := Global() + config := LoadConfiguration() p.Notify(config) } diff --git a/internal/lcd/hd44780.go b/internal/lcd/hd44780.go index d1e1c5c..4a516b9 100644 --- a/internal/lcd/hd44780.go +++ b/internal/lcd/hd44780.go @@ -79,6 +79,8 @@ func (p *LCD) Close() error { log.Print(err) } + p.hub.Flush(10 * time.Second) + return p.bus.Close() } @@ -120,7 +122,6 @@ func (p *LCD) SetState(state controllers.ChamberState) { func (p *LCD) Run(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() - defer p.hub.Flush(10 * time.Second) for { select {