/
reporter.go
137 lines (122 loc) · 2.97 KB
/
reporter.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package reporter
import (
"encoding/json"
"errors"
"log"
"strconv"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/google/uuid"
"github.com/mrk21/sandbox/go_jobworker/pkg/jobque"
"github.com/mrk21/sandbox/go_jobworker/pkg/redislua"
)
type Reporter struct {
rclient *redis.Client
que *jobque.JobQueue
reportDuration time.Duration
id string
}
type ReportData struct {
ExecRate float64 `json:"execRate"`
QueSize int `json:"queSize"`
}
func New(rclient *redis.Client, que *jobque.JobQueue, reportDuration time.Duration) (*Reporter, error) {
uuid, err := uuid.NewRandom()
if err != nil {
return nil, err
}
r := &Reporter{
rclient: rclient,
que: que,
reportDuration: reportDuration,
id: uuid.String(),
}
return r, nil
}
func (r *Reporter) Report() ([]ReportData, error) {
data, err := r.rclient.LRange("jobworker:report:data", 0, 49).Result()
if err == redis.Nil {
return []ReportData{}, nil
} else if err != nil {
return []ReportData{}, err
}
result := []ReportData{}
for _, d := range data {
item := ReportData{}
json.Unmarshal([]byte(d), &item)
result = append(result, item)
}
return result, nil
}
func (r *Reporter) RecordLoop(errch chan error) {
lua := redislua.NewScript(`
local loop_id_key = KEYS[1]
local loop_id = ARGV[1]
local report_duration = ARGV[2]
if redis.call("EXISTS", loop_id_key) > 0 and loop_id ~= redis.call("GET", loop_id_key) then
return 0
end
redis.call("SET", loop_id_key, loop_id, "EX", report_duration + 5)
return 1
`)
keys := []string{"jobworker:record_loop"}
args := []interface{}{
r.id,
int(r.reportDuration / time.Second),
}
ticker := time.NewTicker(r.reportDuration)
defer ticker.Stop()
for {
select {
case <-ticker.C:
result, err := lua.Exec(r.rclient, keys, args...)
if err != nil {
errch <- err
return
}
switch val := result.(type) {
case int64:
if val == 0 {
time.Sleep(r.reportDuration)
continue
}
default:
errch <- errors.New("invalid type")
return
}
current := time.Now().Unix()
from := strconv.Itoa(int(current) - 4)
to := strconv.Itoa(int(current))
counts, err := r.rclient.ZRangeByScore("jobworker:count_history", redis.ZRangeBy{Min: from, Max: to}).Result()
if err != nil {
errch <- err
return
}
totalCount := 0
for _, data := range counts {
item := strings.Split(data, ",")
val, err := strconv.Atoi(item[1])
if err != nil {
errch <- err
return
}
totalCount += val
}
execRate := float64(totalCount) / float64(r.reportDuration/time.Second)
log.Print("## per seconds:", execRate)
queSize, err := r.que.Size()
if err != nil {
errch <- err
return
}
json, err := json.Marshal(ReportData{ExecRate: execRate, QueSize: int(queSize)})
if err != nil {
errch <- err
return
}
r.rclient.LPush("jobworker:report:data", json)
r.rclient.LTrim("jobworker:report:data", 0, 49)
}
}
}