forked from digitalocean/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_stats.go
41 lines (35 loc) · 1.08 KB
/
api_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
package workers
import (
"encoding/json"
"net/http"
)
func (s *apiServer) Stats(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
allStats := []Stats{}
for _, m := range s.managers {
stats, err := m.GetStats()
if err != nil {
s.logger.Println("couldn't retrieve stats for manager:", err)
} else {
allStats = append(allStats, stats)
}
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(allStats)
}
// Stats containts current stats for a manager
type Stats struct {
Name string `json:"manager_name"`
Processed int64 `json:"processed"`
Failed int64 `json:"failed"`
Jobs map[string][]JobStatus `json:"jobs"`
Enqueued map[string]int64 `json:"enqueued"`
RetryCount int64 `json:"retry_count"`
}
// JobStatus contains the status and data for active jobs of a manager
type JobStatus struct {
Message *Msg `json:"message"`
StartedAt int64 `json:"started_at"`
}