-
Notifications
You must be signed in to change notification settings - Fork 2
/
pop.go
58 lines (47 loc) · 1.31 KB
/
pop.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
package rabbit
import (
"errors"
"log"
"github.com/streadway/amqp"
)
var popConnection *Connection
var popChannel *amqp.Channel
// InitPop intializes the RabbitMQ Connection and Channel for popping messages off of a queue.
func InitPop() {
if popConnection == nil {
popConnection = &Connection{}
}
if popChannel == nil {
popChannel, _ = popConnection.GetConnection().Channel()
}
}
// Pop returns a single item from a RabbitMQ queue. It uses the Subscriber to know which
// queue to pop the item off. This is currently a helper function for the tests so you can
// pop a message off the queue and test it.
func Pop(subscriber *Subscriber) (string, error) {
InitPop()
if popChannel == nil {
errorMessage := "Can't consume message: no channel"
log.Printf(errorMessage)
return "", errors.New(errorMessage)
}
createQueue(popChannel, subscriber)
bindQueue(popChannel, subscriber)
message, _, err := popChannel.Get(
subscriber.Queue, // queue
true, // auto ack
)
logError(err, "Failed while consuming message")
if err == nil {
return string(message.Body), nil
}
return "", err
}
// ResetPopConnection sets popConnection=nil and popChannel=nil for testing purposes
func ResetPopConnection() {
if popConnection != nil {
popConnection.Close()
}
popConnection = nil
popChannel = nil
}