-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjazz.go
185 lines (161 loc) · 4.13 KB
/
jazz.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package jazz
import (
"github.com/streadway/amqp"
"gopkg.in/yaml.v2"
"io"
)
// Connection is a struct which holds all necessary data for RabbitMQ connection
type Connection struct {
c *amqp.Connection
}
// Connect connects to RabbitMQ by dsn and return Connection object which uses openned connection during function calls issued later in code
func Connect(dsn string) (*Connection, error) {
conn, err := amqp.Dial(dsn)
if err != nil {
return nil, err
}
return &Connection{conn}, nil
}
// DecodeYaml reads yaml with specification of all exchanges and queues from io.Reader
func DecodeYaml(r io.Reader) (Settings, error) {
s := Settings{}
dec := yaml.NewDecoder(r)
err := dec.Decode(&s)
if err != nil {
return s, err
}
return s, nil
}
// CreateScheme creates all exchanges, queues and bindinges between them as specified in yaml string
func (c *Connection) CreateScheme(s Settings) error {
ch, err := c.c.Channel()
if err != nil {
return err
}
// Create exchanges according to settings
for name, e := range s.Exchanges {
err = ch.ExchangeDeclarePassive(name, e.Type, e.Durable, e.Autodelete, e.Internal, e.Nowait, nil)
if err == nil {
continue
}
ch, err = c.c.Channel()
if err != nil {
return err
}
err = ch.ExchangeDeclare(name, e.Type, e.Durable, e.Autodelete, e.Internal, e.Nowait, nil)
if err != nil {
return err
}
}
// Create queues according to settings
for name, q := range s.Queues {
_, err := ch.QueueDeclarePassive(name, q.Durable, q.Autodelete, q.Exclusive, q.Nowait, nil)
if err == nil {
continue
}
ch, err = c.c.Channel()
if err != nil {
return err
}
_, err = ch.QueueDeclare(name, q.Durable, q.Autodelete, q.Exclusive, q.Nowait, nil)
if err != nil {
return err
}
}
// Create bindings only now that everything is setup.
// (If the bindings were created in one run together with exchanges and queues,
// it would be possible to create binding to not yet existent queue.
// This way it's still possible but now is an error on the user side)
for name, e := range s.Exchanges {
for _, b := range e.Bindings {
err = ch.ExchangeBind(name, b.Key, b.Exchange, b.Nowait, nil)
if err != nil {
return err
}
}
}
for name, q := range s.Queues {
for _, b := range q.Bindings {
err = ch.QueueBind(name, b.Key, b.Exchange, b.Nowait, nil)
if err != nil {
return err
}
}
}
ch.Close()
return nil
}
// DeleteScheme deletes all queues and exchanges (together with bindings) as specified in yaml string
func (c *Connection) DeleteScheme(s Settings) error {
ch, err := c.c.Channel()
if err != nil {
return err
}
for name := range s.Exchanges {
err = ch.ExchangeDelete(name, false, false)
if err != nil {
return err
}
}
for name := range s.Queues {
_, err = ch.QueueDelete(name, false, false, false)
if err != nil {
return err
}
}
ch.Close()
return nil
}
// Close closes connection to RabbitMQ
func (c *Connection) Close() error {
return c.c.Close()
}
// SendMessage publishes plain text message to an exchange with specific routing key
func (c *Connection) SendMessage(ex, key, msg string) error {
ch, err := c.c.Channel()
if err != nil {
return err
}
err = ch.Publish(ex, key, false, false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(msg),
})
if err != nil {
return err
}
return ch.Close()
}
// SendBlob publishes byte blob message to an exchange with specific routing key
func (c *Connection) SendBlob(ex, key string, msg []byte) error {
ch, err := c.c.Channel()
if err != nil {
return err
}
err = ch.Publish(ex, key, false, false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "application/octet-stream",
Body: msg,
})
if err != nil {
return err
}
return ch.Close()
}
// ProcessQueue calls handler function on each message delivered to a queue
func (c *Connection) ProcessQueue(name string, f func([]byte)) error {
ch, err := c.c.Channel()
if err != nil {
return err
}
msgs, err := ch.Consume(name, "", true, false, false, false, nil)
if err != nil {
return err
}
for d := range msgs {
f(d.Body)
}
return nil
}