forked from rahuljayaraman/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.go
More file actions
190 lines (166 loc) · 4.71 KB
/
Copy pathstats.go
File metadata and controls
190 lines (166 loc) · 4.71 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package workers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type stats struct {
Processed int `json:"processed"`
Failed int `json:"failed"`
Jobs interface{} `json:"jobs"`
Enqueued interface{} `json:"enqueued"`
Retries int64 `json:"retries"`
}
func Stats(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
jobs := make(map[string][]*map[string]interface{})
enqueued := make(map[string]string)
for _, m := range managers {
queue := m.queueName()
jobs[queue] = make([]*map[string]interface{}, 0)
enqueued[queue] = ""
for _, worker := range m.workers {
message := worker.currentMsg
startedAt := worker.startedAt
if message != nil && startedAt > 0 {
jobs[queue] = append(jobs[queue], &map[string]interface{}{
"message": message,
"started_at": startedAt,
})
}
}
}
stats := stats{
0,
0,
jobs,
enqueued,
0,
}
conn := Config.Pool.Get()
defer conn.Close()
conn.Send("multi")
conn.Send("get", Config.Namespace+"stat:processed")
conn.Send("get", Config.Namespace+"stat:failed")
conn.Send("zcard", Config.Namespace+RETRY_KEY)
for key, _ := range enqueued {
conn.Send("llen", fmt.Sprintf("%squeue:%s", Config.Namespace, key))
}
r, err := conn.Do("exec")
if err != nil {
Logger.Println("couldn't retrieve stats:", err)
}
results := r.([]interface{})
if len(results) == (3 + len(enqueued)) {
for index, result := range results {
if index == 0 && result != nil {
stats.Processed, _ = strconv.Atoi(string(result.([]byte)))
continue
}
if index == 1 && result != nil {
stats.Failed, _ = strconv.Atoi(string(result.([]byte)))
continue
}
if index == 2 && result != nil {
stats.Retries = result.(int64)
continue
}
queueIndex := 0
for key, _ := range enqueued {
if queueIndex == (index - 3) {
enqueued[key] = fmt.Sprintf("%d", result.(int64))
}
queueIndex++
}
}
}
body, _ := json.MarshalIndent(stats, "", " ")
fmt.Fprintln(w, string(body))
}
type queueMessage struct {
Queue string `json:"queue"`
Identifier string `json:"identifier"`
}
type identifierStatus struct {
Status bool `json:"status"`
Error error `json:"error"`
Details map[string]interface{} `json:"details"`
}
// CheckQueueData checks whether identifier is part of args in messages in queue
func CheckQueueData(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
var payload queueMessage
response := identifierStatus{Status: false}
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&payload)
if err != nil {
response.Error = err
return
}
response.Status, response.Details, response.Error = IdentifierInQueue(payload.Queue, payload.Identifier)
if !response.Status {
response.Status, response.Details, response.Error = CheckIdentifierInRetry(payload.Identifier)
}
body, _ := json.Marshal(response)
fmt.Fprintln(w, string(body))
}
// IdentifierInQueue checks whether identifier is present in message of worker queue
func IdentifierInQueue(srcQueue, identifier string) (bool, map[string]interface{}, error) {
for _, m := range managers {
queue := m.queueName()
if queue == srcQueue {
for _, worker := range m.workers {
message := worker.currentMsg
startedAt := worker.startedAt
if message != nil && startedAt > 0 {
args, err := message.Args().Array()
if err != nil {
return false, nil, err
}
for _, arg := range args {
if arg == identifier {
return true, map[string]interface{}{
"message": message,
"started_at": startedAt,
}, nil
}
}
}
}
}
}
return false, nil, nil
}
// CheckIdentifierInRetry checks whether identifier is present in retry queue
// Caution: In case of large retry queue, this function can be slow
func CheckIdentifierInRetry(identifier string) (bool, map[string]interface{}, error) {
conn := Config.Pool.Get()
defer conn.Close()
conn.Send("multi")
conn.Send("ZRANGE", Config.Namespace+RETRY_KEY, 0, -1)
r, err := conn.Do("exec")
if err != nil {
return false, nil, err
}
results := r.([]interface{})
var object map[string]interface{}
for _, alpha := range results[0].([]interface{}) {
err = json.Unmarshal(alpha.([]byte), &object)
if err != nil {
return false, nil, err
}
if args, ok := object["args"]; ok {
for _, arg := range args.([]interface{}) {
if arg.(string) == identifier {
return true, map[string]interface{}{
"message": object,
}, nil
}
}
}
}
return false, nil, nil
}