forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware_stats.go
46 lines (36 loc) · 919 Bytes
/
middleware_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
package workers
import (
"time"
)
type MiddlewareStats struct{}
func (l *MiddlewareStats) Call(queue string, message *Msg, next func() bool) (acknowledge bool) {
defer func() {
if e := recover(); e != nil {
incrementStats("failed")
panic(e)
}
}()
acknowledge = next()
incrementStats("processed")
return
}
func incrementStats(metric string) {
conn := Config.Pool.Get()
defer conn.Close()
today := time.Now().UTC().Format("2006-01-02")
err := conn.Send("multi")
if err != nil {
Logger.Println("couldn't send multi:", err)
}
err = conn.Send("incr", Config.Namespace+"stat:"+metric)
if err != nil {
Logger.Println("could not incr stat:", err)
}
err = conn.Send("incr", Config.Namespace+"stat:"+metric+":"+today)
if err != nil {
Logger.Println("could not incr stat for today:", err)
}
if _, err := conn.Do("exec"); err != nil {
Logger.Println("couldn't save stats:", err)
}
}