forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.go
70 lines (56 loc) · 1.44 KB
/
stats.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
package workers
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type stats struct {
Processed int `json:"processed"`
Failed int `json:"failed"`
Jobs interface{} `json:"jobs"`
}
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{})
for _, m := range managers {
queue := m.queueName()
jobs[queue] = make([]*map[string]interface{}, 0)
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,
}
conn := Config.Pool.Get()
defer conn.Close()
conn.Send("multi")
conn.Send("get", Config.Namespace+"stat:processed")
conn.Send("get", Config.Namespace+"stat:failed")
r, err := conn.Do("exec")
if err != nil {
Logger.Println("couldn't retrieve stats:", err)
}
results := r.([]interface{})
if len(results) == 2 {
if results[0] != nil {
stats.Processed, _ = strconv.Atoi(string(results[0].([]byte)))
}
if results[1] != nil {
stats.Failed, _ = strconv.Atoi(string(results[1].([]byte)))
}
}
body, _ := json.MarshalIndent(stats, "", " ")
fmt.Fprintln(w, string(body))
}