forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_broker.go
111 lines (102 loc) · 2.67 KB
/
redis_broker.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gocelery
import (
"fmt"
"log"
"sync"
"time"
"github.com/garyburd/redigo/redis"
)
// RedisCeleryBroker is CeleryBroker for Redis
type RedisCeleryBroker struct {
*redis.Pool
QueueName string
stopChannel chan bool
workWG sync.WaitGroup
}
type BrokerOptions struct {
f func(*brokerOptions)
}
type brokerOptions struct {
QueueName string
}
func BrokerQueueName(queueName string) BrokerOptions {
return BrokerOptions{func(options *brokerOptions) {
options.QueueName = queueName
}}
}
// NewRedisPool creates pool of redis connections
func NewRedisPool(host string, port int, db int, pass string) *redis.Pool {
return &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", host, port), redis.DialDatabase(db))
if err != nil {
return nil, err
}
if pass != "" {
if _, err = c.Do("AUTH", pass); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func NewRedisCeleryBroker(host string, port int, db int, pass string, options ...BrokerOptions) *RedisCeleryBroker {
do := brokerOptions{"celery"}
for _, opt := range options {
opt.f(&do)
}
return &RedisCeleryBroker{
Pool: NewRedisPool(host, port, db, pass),
QueueName: do.QueueName,
}
}
// SendCeleryMessage sends CeleryMessage to redis queue
func (cb *RedisCeleryBroker) SendCeleryMessage(message *CeleryMessage) error {
jsonBytes, err := json.Marshal(message)
log.Printf("Send Celery message by redis broker: \n%s", jsonBytes)
conn := cb.Get()
defer conn.Close()
_, err = conn.Do("LPUSH", cb.QueueName, jsonBytes)
if err != nil {
return err
}
return nil
}
// GetCeleryMessage retrieves celery message from redis queue
func (cb *RedisCeleryBroker) GetCeleryMessage() (*CeleryMessage, error) {
conn := cb.Get()
defer conn.Close()
messageJSON, err := conn.Do("BLPOP", cb.QueueName, "1")
if err != nil {
return nil, err
}
if messageJSON == nil {
return nil, fmt.Errorf("null message received from redis")
}
messageList := messageJSON.([]interface{})
// check for celery message
if string(messageList[0].([]byte)) != "celery" {
return nil, fmt.Errorf("not a celery message: %v", messageList[0])
}
// parse
var message = &CeleryMessage{}
var msgJson = messageList[1].([]byte)
json.Unmarshal(msgJson, message)
return message, nil
}
// GetTask retrieves task message from redis queue
func (cb *RedisCeleryBroker) GetTask() (*CeleryTask, error) {
celeryMessage, err := cb.GetCeleryMessage()
if err != nil {
return nil, err
}
return Msg2Task(celeryMessage), nil
}