-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpoll.go
122 lines (89 loc) · 2.42 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
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
122
package main
import (
"os"
"runtime"
"time"
"google.golang.org/grpc"
"github.com/usher2/u2ckdump/internal/logger"
)
// DumpPoll - poll "vygruzki" service for new dumps.
func DumpPoll(s *grpc.Server, done chan<- struct{}, kill <-chan struct{}, url, token, dir string, d time.Duration) {
timer := time.NewTimer(time.Millisecond)
defer timer.Stop()
for {
select {
case <-timer.C:
DumpRefresh(url, token, dir)
timer.Reset(d * time.Second)
case <-kill:
close(done)
return
}
}
}
// DumpRefresh - try to fetch new dump.
func DumpRefresh(url, token, dir string) {
ts := time.Now().Unix()
lastDump, err := GetLastDumpID(ts, url, token)
if err != nil {
logger.Error.Printf("Can't get last dump id: %s\n", err.Error())
return
}
if lastDump.ID == "" {
logger.Error.Println("Last dump Id is empty...")
return
}
logger.Info.Printf("Last dump id: %s\n", lastDump.ID)
cachedDump, err := ReadCurrentDumpID(dir + "/current")
if err != nil {
logger.Error.Printf("Can't read cached dump id: %s\n", err.Error())
// TODO: investigate thi case.
// return
}
if cachedDump.ID == "" {
logger.Warning.Println("Cashed dump Id is empty...")
}
// TDO: Why hear?
defer runtime.GC()
// two states...
switch {
case lastDump.CRC != cachedDump.CRC:
logger.Info.Printf("Getting new dump..")
err := FetchDump(lastDump.ID, dir+"/dump.zip", url, token)
if err != nil {
logger.Error.Printf("Can't fetch last dump: %s\n", err.Error())
return
}
logger.Info.Println("Last dump fetched")
err = DumpUnzip(dir+"/dump.zip", dir+"/dump.xml")
if err != nil {
logger.Error.Printf("Can't extract last dump: %s\n", err.Error())
return
}
logger.Info.Println("Last dump extracted")
// parse xml
dumpFile, err := os.Open(dir + "/dump.xml")
if err != nil {
logger.Error.Printf("Can't open dump file: %s\n", err.Error())
return
}
defer dumpFile.Close()
err = Parse(dumpFile)
if err != nil {
logger.Error.Printf("Parse error: %s\n", err.Error())
return
}
logger.Info.Printf("Dump parsed")
err = WriteCurrentDumpID(dir+"/current", lastDump)
if err != nil {
logger.Error.Printf("Can't write currentdump file: %s\n", err.Error())
return
}
logger.Info.Println("Last dump metainfo saved")
case lastDump.ID != cachedDump.ID:
logger.Info.Printf("Not changed, but new dump metainfo")
UpdateDumpTime(lastDump.UpdateTime)
default:
logger.Info.Printf("No new dump")
}
}