forked from open-falcon/falcon-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_reader.go
80 lines (65 loc) · 1.47 KB
/
event_reader.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
71
72
73
74
75
76
77
78
79
80
package cron
import (
"encoding/json"
"time"
log "github.com/Sirupsen/logrus"
"github.com/garyburd/redigo/redis"
cmodel "github.com/open-falcon/falcon-plus/common/model"
"github.com/open-falcon/falcon-plus/modules/alarm/g"
eventmodel "github.com/open-falcon/falcon-plus/modules/alarm/model/event"
)
func ReadHighEvent() {
queues := g.Config().Redis.HighQueues
if len(queues) == 0 {
return
}
for {
event, err := popEvent(queues)
if err != nil {
time.Sleep(time.Second)
continue
}
consume(event, true)
}
}
func ReadLowEvent() {
queues := g.Config().Redis.LowQueues
if len(queues) == 0 {
return
}
for {
event, err := popEvent(queues)
if err != nil {
time.Sleep(time.Second)
continue
}
consume(event, false)
}
}
func popEvent(queues []string) (*cmodel.Event, error) {
count := len(queues)
params := make([]interface{}, count+1)
for i := 0; i < count; i++ {
params[i] = queues[i]
}
// set timeout 0
params[count] = 0
rc := g.RedisConnPool.Get()
defer rc.Close()
reply, err := redis.Strings(rc.Do("BRPOP", params...))
if err != nil {
log.Errorf("get alarm event from redis fail: %v", err)
return nil, err
}
var event cmodel.Event
err = json.Unmarshal([]byte(reply[1]), &event)
if err != nil {
log.Errorf("parse alarm event fail: %v", err)
return nil, err
}
log.Debugf("pop event: %s", event.String())
//insert event into database
eventmodel.InsertEvent(&event)
// events no longer saved in memory
return &event, nil
}