-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
watcher.go
60 lines (45 loc) · 917 Bytes
/
watcher.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
package core
import (
"time"
"github.com/dosco/graphjin/core/internal/sdata"
)
func (g *GraphJin) initDBWatcher() error {
gj := g.Load().(*graphjin)
// no schema polling in production
if gj.prod {
return nil
}
ps := gj.conf.DBSchemaPollDuration
switch {
case ps < (1 * time.Second):
return nil
case ps < (5 * time.Second):
ps = 10 * time.Second
}
go func() {
g.startDBWatcher(ps)
}()
return nil
}
func (g *GraphJin) startDBWatcher(ps time.Duration) {
ticker := time.NewTicker(ps)
defer ticker.Stop()
for range ticker.C {
gj := g.Load().(*graphjin)
dbinfo, err := sdata.GetDBInfo(
gj.db,
gj.dbtype,
gj.conf.Blocklist)
if err != nil {
gj.log.Println(err)
continue
}
if dbinfo.Hash() == gj.dbinfo.Hash() {
continue
}
gj.log.Println("database change detected. reinitializing...")
if err := g.Reload(); err != nil {
gj.log.Println(err)
}
}
}