Refactor configuration reload
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
deb1ef3578
commit
78d6841bd1
5 changed files with 28 additions and 8 deletions
|
@ -38,7 +38,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) {
|
||||||
|
|
||||||
chConfigChange := make(chan configuration.Configuration, 1)
|
chConfigChange := make(chan configuration.Configuration, 1)
|
||||||
chn := configuration.NewChangeNotifier()
|
chn := configuration.NewChangeNotifier()
|
||||||
viper.OnConfigChange(chn.OnConfigChange)
|
defer chn.Close()
|
||||||
|
|
||||||
// Configuration updates
|
// Configuration updates
|
||||||
chn.Subscribe(chConfigChange)
|
chn.Subscribe(chConfigChange)
|
||||||
|
@ -46,6 +46,7 @@ func mainLoop(ctx context.Context, wg *sync.WaitGroup, js nats.JetStream) {
|
||||||
chn.Subscribe(ctrl.ConfigUpdates)
|
chn.Subscribe(ctrl.ConfigUpdates)
|
||||||
//chn.Subscribe(display.ConfiguUpdate)
|
//chn.Subscribe(display.ConfiguUpdate)
|
||||||
chn.Notify(configuration.Global())
|
chn.Notify(configuration.Global())
|
||||||
|
viper.OnConfigChange(chn.OnConfigChange)
|
||||||
viper.WatchConfig()
|
viper.WatchConfig()
|
||||||
|
|
||||||
// NATS
|
// NATS
|
||||||
|
|
|
@ -29,10 +29,10 @@ func main() {
|
||||||
defer sentry.Flush(10 * time.Second)
|
defer sentry.Flush(10 * time.Second)
|
||||||
|
|
||||||
// Configuration
|
// Configuration
|
||||||
configuration.LoadConfiguration()
|
configuration.Initialize()
|
||||||
|
config := configuration.LoadConfiguration()
|
||||||
|
|
||||||
// NATS
|
// NATS
|
||||||
config := configuration.Global()
|
|
||||||
servers := strings.Join(config.NATS.Servers, ",")
|
servers := strings.Join(config.NATS.Servers, ",")
|
||||||
userInfo := nats.UserInfo(config.NATS.Username, config.NATS.Password)
|
userInfo := nats.UserInfo(config.NATS.Username, config.NATS.Password)
|
||||||
nc, err := nats.Connect(servers, userInfo)
|
nc, err := nats.Connect(servers, userInfo)
|
||||||
|
|
|
@ -24,7 +24,7 @@ func Global() (c Configuration) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfiguration() {
|
func Initialize() {
|
||||||
setDefaults()
|
setDefaults()
|
||||||
|
|
||||||
viper.AddConfigPath("/etc")
|
viper.AddConfigPath("/etc")
|
||||||
|
@ -32,6 +32,11 @@ func LoadConfiguration() {
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
viper.SetConfigName("fermentord")
|
viper.SetConfigName("fermentord")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadConfiguration() Configuration {
|
||||||
|
globalLock.Lock()
|
||||||
|
defer globalLock.Unlock()
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
log.Printf("Error loading configuration: %v", err)
|
log.Printf("Error loading configuration: %v", err)
|
||||||
|
@ -43,7 +48,7 @@ func LoadConfiguration() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
globalLock.Lock()
|
|
||||||
globalConfig = *config
|
globalConfig = *config
|
||||||
globalLock.Unlock()
|
|
||||||
|
return globalConfig
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,30 @@
|
||||||
package configuration
|
package configuration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
|
"github.com/getsentry/sentry-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChangeNotifier struct {
|
type ChangeNotifier struct {
|
||||||
channels []chan<- Configuration
|
channels []chan<- Configuration
|
||||||
|
hub *sentry.Hub
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChangeNotifier() *ChangeNotifier {
|
func NewChangeNotifier() *ChangeNotifier {
|
||||||
return &ChangeNotifier{
|
return &ChangeNotifier{
|
||||||
channels: make([]chan<- Configuration, 0),
|
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) {
|
func (p *ChangeNotifier) Subscribe(ch chan<- Configuration) {
|
||||||
p.channels = append(p.channels, ch)
|
p.channels = append(p.channels, ch)
|
||||||
}
|
}
|
||||||
|
@ -24,13 +33,17 @@ func (p *ChangeNotifier) Notify(config Configuration) {
|
||||||
for _, ch := range p.channels {
|
for _, ch := range p.channels {
|
||||||
select {
|
select {
|
||||||
case ch <- config:
|
case ch <- config:
|
||||||
|
|
||||||
default:
|
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) {
|
func (p *ChangeNotifier) OnConfigChange(in fsnotify.Event) {
|
||||||
log.Print("Reloading configuration")
|
log.Print("Reloading configuration")
|
||||||
config := Global()
|
config := LoadConfiguration()
|
||||||
p.Notify(config)
|
p.Notify(config)
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,8 @@ func (p *LCD) Close() error {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.hub.Flush(10 * time.Second)
|
||||||
|
|
||||||
return p.bus.Close()
|
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) {
|
func (p *LCD) Run(ctx context.Context, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer p.hub.Flush(10 * time.Second)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
Loading…
Reference in a new issue