Refactor configuration reload
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Søren Rasmussen 2022-08-02 20:59:27 +02:00
parent deb1ef3578
commit 78d6841bd1
5 changed files with 28 additions and 8 deletions

View file

@ -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

View file

@ -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)

View file

@ -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
}

View file

@ -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)
}

View file

@ -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 {