-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (104 loc) · 2.36 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"errors"
"fmt"
"log"
"net/http"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/fsnotify/fsnotify"
"github.com/jonaz/gograce"
"github.com/koding/multiconfig"
)
var watcherCount int64
func main() {
config := &Config{}
multiconfig.New().MustLoad(config)
config.Parse()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
srv, shutdown := gograce.NewServerWithTimeout(5 * time.Second)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if config.Debug {
log.Printf("fsnotify: %s", event)
}
if event.Op == fsnotify.Remove { // support the way k8s changes mounted configmaps
f := config.ByRealPath(event.Name)
if f == nil {
continue
}
atomic.AddInt64(&watcherCount, -1)
updateWatcher(config, watcher, f)
config.SignalPid(f.realPath)
continue
}
if event.Op == fsnotify.Write {
config.SignalPid(event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
case <-shutdown:
log.Println("exiting on signal")
return
}
}
}()
addWatchers(watcher, config)
http.HandleFunc("/health", healthHandler)
srv.Handler = http.DefaultServeMux
srv.Addr = ":8080"
err = srv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Println(err)
}
wg.Wait()
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
if watcherCount <= 0 {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "no watchers found!")
}
fmt.Fprintf(w, "ok")
}
func updateWatcher(config *Config, w *fsnotify.Watcher, f *file) {
if config.Debug {
log.Printf("updateWatcher: %#v\n", f)
}
rp, err := filepath.EvalSymlinks(f.originalPath)
if err != nil {
log.Println("error updateWatcher find realPath:", err)
return
}
f.realPath = rp
addWatcher(w, f.realPath)
if config.Debug {
log.Printf("added watcher: %#v\n", f)
}
}
func addWatchers(w *fsnotify.Watcher, config *Config) {
for _, v := range config.Files() {
addWatcher(w, v.realPath)
}
}
func addWatcher(w *fsnotify.Watcher, path string) {
log.Printf("adding watcher for %s ", path)
err := w.Add(path)
if err != nil {
log.Printf("error adding watcher %s: %s\n", path, err)
}
atomic.AddInt64(&watcherCount, 1)
}