2021-05-24 15:55:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const configChangeCheckInterval = time.Second
|
|
|
|
|
|
|
|
type configChangeEvent uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
configChangeEventUnkown configChangeEvent = iota
|
|
|
|
configChangeEventNotExist
|
|
|
|
configChangeEventModified
|
|
|
|
)
|
|
|
|
|
|
|
|
func watchConfigChanges(filename string, evt chan configChangeEvent) {
|
|
|
|
var (
|
2021-05-24 16:00:50 +00:00
|
|
|
available bool
|
2021-05-24 15:55:12 +00:00
|
|
|
initialized bool
|
|
|
|
size int64
|
|
|
|
modTime time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
for range time.NewTicker(configChangeCheckInterval).C {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
// Fine
|
|
|
|
|
|
|
|
case os.IsNotExist(err):
|
2021-05-24 16:00:50 +00:00
|
|
|
if available {
|
|
|
|
evt <- configChangeEventNotExist
|
|
|
|
}
|
|
|
|
available = false
|
2021-05-24 15:55:12 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.WithError(err).Error("Failed to get config stat")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if initialized && (info.Size() != size || !info.ModTime().Equal(modTime)) {
|
|
|
|
evt <- configChangeEventModified
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:00:50 +00:00
|
|
|
available = true
|
2021-05-24 15:55:12 +00:00
|
|
|
initialized = true
|
|
|
|
size = info.Size()
|
|
|
|
modTime = info.ModTime()
|
|
|
|
}
|
|
|
|
}
|