-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
71 lines (59 loc) · 1.4 KB
/
report.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
package p2p
import (
"encoding/json"
"net/http"
log "github.com/cihub/seelog"
"library/p2p/common"
)
type reportInfo struct {
serverAddr string
percentComplete float32
}
type reportor struct {
taskID string
cfg *common.Config
client *http.Client
reportChan chan *reportInfo
}
func newReportor(taskID string, cfg *common.Config) *reportor {
r := &reportor{
taskID: taskID,
cfg: cfg,
client: common.CreateHTTPClient(cfg),
reportChan: make(chan *reportInfo, 20),
}
go r.run()
return r
}
func (r *reportor) run() {
for rc := range r.reportChan {
r.reportImp(rc)
}
}
func (r *reportor) DoReport(serverAddr string, pecent float32) {
r.reportChan <- &reportInfo{serverAddr: serverAddr, percentComplete: pecent}
}
func (r *reportor) Close() {
close(r.reportChan)
}
func (r *reportor) reportImp(ri *reportInfo) {
if int(ri.percentComplete) == 100 {
log.Infof("[%s] Report session status... completed", r.taskID)
}
csr := &StatusReport{
TaskID: r.taskID,
IP: r.cfg.Net.IP,
PercentComplete: ri.percentComplete,
}
bs, err := json.Marshal(csr)
if err != nil {
log.Errorf("[%s] Report session status failed. error=%v", r.taskID, err)
return
}
_, err = common.SendHTTPReq(r.cfg, "POST",
ri.serverAddr, "/api/v1/server/tasks/status", bs)
if err != nil {
log.Errorf("[%s] Report session status failed. error=%v", r.taskID, err)
}
return
}