twitch-bot/configwatch.go
Knut Ahlers 5339e866e5
Fix: Do not spam events when file is gone for longer
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-05-24 18:00:50 +02:00

55 lines
981 B
Go

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 (
available bool
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):
if available {
evt <- configChangeEventNotExist
}
available = false
continue
default:
log.WithError(err).Error("Failed to get config stat")
continue
}
if initialized && (info.Size() != size || !info.ModTime().Equal(modTime)) {
evt <- configChangeEventModified
}
available = true
initialized = true
size = info.Size()
modTime = info.ModTime()
}
}