2022-08-02 14:04:06 +00:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
2022-08-02 18:59:27 +00:00
|
|
|
"fmt"
|
2022-08-02 14:04:06 +00:00
|
|
|
"log"
|
2022-08-02 18:59:27 +00:00
|
|
|
"time"
|
2022-08-02 14:04:06 +00:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2022-08-02 18:59:27 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2022-08-02 14:04:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChangeNotifier struct {
|
|
|
|
channels []chan<- Configuration
|
2022-08-02 18:59:27 +00:00
|
|
|
hub *sentry.Hub
|
2022-08-02 14:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangeNotifier() *ChangeNotifier {
|
|
|
|
return &ChangeNotifier{
|
|
|
|
channels: make([]chan<- Configuration, 0),
|
2022-08-02 18:59:27 +00:00
|
|
|
hub: sentry.CurrentHub().Clone(),
|
2022-08-02 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 18:59:27 +00:00
|
|
|
func (p *ChangeNotifier) Close() {
|
|
|
|
p.hub.Flush(10 * time.Second)
|
|
|
|
}
|
|
|
|
|
2022-08-02 14:04:06 +00:00
|
|
|
func (p *ChangeNotifier) Subscribe(ch chan<- Configuration) {
|
|
|
|
p.channels = append(p.channels, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ChangeNotifier) Notify(config Configuration) {
|
|
|
|
for _, ch := range p.channels {
|
|
|
|
select {
|
|
|
|
case ch <- config:
|
2022-08-02 18:59:27 +00:00
|
|
|
|
2022-08-02 14:04:06 +00:00
|
|
|
default:
|
2022-08-02 18:59:27 +00:00
|
|
|
err := fmt.Errorf("channel overflow on a ChangeNotifier notification channel")
|
|
|
|
p.hub.CaptureException(err)
|
|
|
|
log.Print(err)
|
2022-08-02 14:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ChangeNotifier) OnConfigChange(in fsnotify.Event) {
|
|
|
|
log.Print("Reloading configuration")
|
2022-08-02 18:59:27 +00:00
|
|
|
config := LoadConfiguration()
|
2022-08-02 14:04:06 +00:00
|
|
|
p.Notify(config)
|
|
|
|
}
|