-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchanges.go
More file actions
79 lines (68 loc) · 1.83 KB
/
exchanges.go
File metadata and controls
79 lines (68 loc) · 1.83 KB
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
package rmqworker
import (
"strings"
"github.com/matrixbotio/constants-lib"
"github.com/streadway/amqp"
"github.com/matrixbotio/rmqworker-lib/pkg/errs"
)
// rmqExchangeDeclare - declare RMQ exchange
func (r *RMQHandler) rmqExchangeDeclare(RMQChannel *amqp.Channel, task RMQExchangeDeclareTask) errs.APIError {
r.rlock()
defer r.runlock()
args := amqp.Table{}
if task.MessagesLifetime > 0 {
args["x-message-ttl"] = task.MessagesLifetime
}
err := RMQChannel.ExchangeDeclare(
task.ExchangeName, // name
task.ExchangeType, // type
true, // durable
false, // auto-deleted
false, // internal
false, // no-wait
args, // arguments
)
if err != nil {
return constants.Error(
"SERVICE_REQ_FAILED",
"failed to declare "+task.ExchangeName+" rmq exchange: "+err.Error(),
)
}
return nil
}
// DeclareExchanges - declare RMQ exchanges list.
// exchange name -> exchange type
func (r *RMQHandler) DeclareExchanges(exchangeTypes map[string]string) errs.APIError {
ch, err := r.getChannel()
if err != nil {
return err
}
for exchangeName, exchangeType := range exchangeTypes {
err := r.rmqExchangeDeclare(ch, RMQExchangeDeclareTask{
ExchangeName: exchangeName,
ExchangeType: exchangeType,
})
if err != nil {
return err
}
}
return nil
}
// IsQueueExists - is queue exists? /ᐠ。ꞈ。ᐟ\
func (r *RMQHandler) IsQueueExists(name string) (bool, errs.APIError) {
ch, err := r.getChannel()
if err != nil {
return false, err
}
_, rmqErr := ch.QueueDeclarePassive(name, true, false, false, false, nil)
if rmqErr != nil {
if strings.Contains(rmqErr.Error(), "NOT_FOUND - no queue") {
return false, nil
}
return false, constants.Error(
"SERVICE_REQ_FAILED",
"failed to get queue `"+name+"` state: "+rmqErr.Error(),
)
}
return true, nil
}