/
rmq.go
89 lines (66 loc) · 1.71 KB
/
rmq.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
package amqp
import (
"sync"
"log"
// "github.com/tsuru/config"
"github.com/streadway/amqp"
)
type opsInstance struct {
queueName string //name of the queue
qi *amqpInstance
channel *amqp.Channel
}
type amqpInstance struct {
RmqAddress string
sync.Mutex
}
func (q *opsInstance) Publish(message []byte) error {
channel, err := q.qi.dial(q.queueName) //TODO: revisit if a queueinspect is required for each call.
if err != nil {
return err
}
log.Printf("[QS] - Info - Publishing message to queue - (%s) (%q)", q.queueName, message)
err = channel.Publish(
"",
q.queueName,
false,
false,
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(message),
})
if err != nil {
return err
}
log.Printf("[QS] - Info - Published successfully")
return nil
}
//func (q *opsInstance) Subscribe() {}
//func (q *opsInstance) Unsubscribe() {}
///will return the queue instance
func (qi *amqpInstance) New(name string) (PubSub, error) {
return &opsInstance{queueName: name, qi: qi}, nil
}
func (qi *amqpInstance) dial(queueName string) (*amqp.Channel , error){
/* amqpAddr, err := config.GetString("amqp:url") //setup on cloudifice config - cfs.yml
if err != nil {
amqpAddr = "amqp://172.17.0.5:5672/"
} */
conn, err := amqp.Dial(qi.RmqAddress)
if err != nil {
return nil, err
}
log.Printf(" [QS] Dialed to (%s)", qi.RmqAddress)
channel, err := conn.Channel()
if err != nil {
return nil, err
}
//NOTE: This is a passive call.
//TODO: Does everycall require a check?
q, err := channel.QueueInspect(queueName)
if err != nil {
return nil, err
}
log.Printf(" [x] Connection successful to (%s,%s)", qi.RmqAddress, q.Name)
return channel, err
}