-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configwatch.go
55 lines (44 loc) · 981 Bytes
/
configwatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()
}
}