forked from JuenTingShie/ptt-alertor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
39 lines (35 loc) · 964 Bytes
/
counter.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
package counter
import (
log "github.com/Ptt-Alertor/logrus"
"github.com/garyburd/redigo/redis"
"github.com/watain666/ptt-alertor/connections"
"github.com/watain666/ptt-alertor/myutil"
)
func Alert() (int, error) {
conn := connections.Redis()
defer conn.Close()
count, err := redis.Int(conn.Do("GET", "counter:alert"))
if err != nil {
log.WithField("runtime", myutil.BasicRuntimeInfo()).WithError(err).Error()
}
return count, err
}
func IncrAlert() error {
conn := connections.Redis()
defer conn.Close()
count, err := redis.Int(conn.Do("INCR", "counter:alert"))
if err != nil {
log.WithField("runtime", myutil.BasicRuntimeInfo()).WithError(err).Error()
}
publishAlert(count)
return err
}
func publishAlert(count int) error {
conn := connections.Redis()
defer conn.Close()
_, err := conn.Do("PUBLISH", "alert-counter", count)
if err != nil {
log.WithField("runtime", myutil.BasicRuntimeInfo()).WithError(err).Error()
}
return err
}