-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.go
52 lines (45 loc) · 995 Bytes
/
publisher.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
package ecflow_watchman
import "github.com/go-redis/redis"
type Publisher interface {
Create()
Publish(key string, message []byte)
Close()
}
type RedisPublisher struct {
Client *redis.Client
Pubsubs []*redis.PubSub
Address string
Password string
Database int
}
func (p *RedisPublisher) Create() {
p.Client = redis.NewClient(&redis.Options{
Addr: p.Address,
Password: p.Password,
DB: p.Database,
})
}
func (p *RedisPublisher) CreatePubsub(channelName string) *redis.PubSub {
pubsub := p.Client.Subscribe(channelName)
_, err := pubsub.Receive()
if err != nil {
panic(err)
}
p.Pubsubs = append(p.Pubsubs, pubsub)
return pubsub
}
func (p *RedisPublisher) Close() {
for _, pubsub := range p.Pubsubs {
if pubsub != nil {
pubsub.Close()
}
}
p.Pubsubs = nil
if p.Client != nil {
defer p.Client.Close()
}
}
func (p *RedisPublisher) Publish(key string, message []byte) error {
redisCmd := p.Client.Publish(key, message)
return redisCmd.Err()
}