forked from jowenshaw/gethscan
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reloadconfig.go
56 lines (51 loc) · 1.09 KB
/
reloadconfig.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
package params
import (
"github.com/anyswap/CrossChain-Bridge/log"
"github.com/fsnotify/fsnotify"
)
// WatchAndReloadScanConfig reload scan config if modified
func WatchAndReloadScanConfig(cf chan bool) {
log.Info("start job of watch and reload config")
watch, err := fsnotify.NewWatcher()
if err != nil {
log.Error("fsnotify.NewWatcher failed", "err", err)
return
}
defer func() {
err = watch.Close()
if err != nil {
log.Error("fsnotify close watcher failed", "err", err)
}
}()
err = watch.Add(configFile)
if err != nil {
log.Error("watch.Add config file failed", "err", err)
return
}
log.Info("watch.Add config file success", "configFile", configFile)
ops := []fsnotify.Op{
fsnotify.Create,
fsnotify.Write,
}
for {
select {
case ev, ok := <-watch.Events:
if !ok {
continue
}
log.Info("fsnotify watch event", "event", ev)
for _, op := range ops {
if ev.Op&op == op {
ReloadConfig()
cf <- true
break
}
}
case werr, ok := <-watch.Errors:
if !ok {
continue
}
log.Warn("fsnotify watch error", "err", werr)
}
}
}