-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.go
87 lines (83 loc) · 2.04 KB
/
poll.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
package main
import (
"os"
"runtime"
"time"
"google.golang.org/grpc"
)
func DumpPoll(s *grpc.Server, done chan bool, sigs chan os.Signal, url, token, dir string, d time.Duration) {
runtime.GC()
Info.Printf("Complete GC\n")
DumpRefresh(url, token, dir)
for {
timer := time.NewTimer(d * time.Second)
select {
case <-timer.C:
DumpRefresh(url, token, dir)
case <-sigs:
s.Stop()
done <- true
}
}
}
func DumpRefresh(url, token, dir string) {
ts := time.Now().Unix()
lastDump, err := GetLastDumpId(ts, url, token)
if err != nil {
Error.Printf("Can't get last dump id: %s\n", err.Error())
return
}
if lastDump.Id == "" {
Error.Println("Last dump Id is empty...")
return
}
Info.Printf("Last dump id: %s\n", lastDump.Id)
cachedDump, err := ReadCurrentDumpId(dir + "/current")
if err != nil {
Error.Printf("Can't read cached dump id: %s\n", err.Error())
return
}
if cachedDump.Id == "" {
Warning.Println("Cashed dump Id is empty...")
}
// two states...
if lastDump.CRC != cachedDump.CRC {
Info.Printf("Getting new dump..")
err := FetchDump(lastDump.Id, dir+"/dump.xml", url, token)
if err != nil {
Error.Printf("Can't fetch last dump: %s\n", err.Error())
return
}
Info.Println("Last dump fetched")
// parse xml
if dumpFile, err := os.Open(dir + "/dump.xml"); err != nil {
Error.Printf("Can't open dump file: %s\n", err.Error())
return
} else {
defer dumpFile.Close()
err = Parse(dumpFile)
if err != nil {
Error.Printf("Parse error: %s\n", err.Error())
return
} else {
Info.Printf("Dump parsed")
dumpFile.Close()
runtime.GC()
Info.Printf("Complete GC\n")
}
err = WriteCurrentDumpId(dir+"/current", lastDump)
if err != nil {
Error.Printf("Can't write currentdump file: %s\n", err.Error())
return
}
Info.Println("Last dump metainfo saved")
}
} else if lastDump.Id != cachedDump.Id {
Info.Printf("Not changed, but new dump metainfo")
Parse2(lastDump.UpdateTime)
runtime.GC()
Info.Printf("Complete GC\n")
} else {
Info.Printf("No new dump")
}
}