forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp_broker.go
193 lines (174 loc) · 4.16 KB
/
amqp_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
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
186
187
188
189
190
191
192
193
package gocelery
import (
"encoding/json"
"time"
"github.com/streadway/amqp"
)
// AMQPExchange stores AMQP Exchange configuration
type AMQPExchange struct {
Name string
Type string
Durable bool
AutoDelete bool
}
// NewAMQPExchange creates new AMQPExchange
func NewAMQPExchange(name string) *AMQPExchange {
return &AMQPExchange{
Name: name,
Type: "direct",
Durable: true,
AutoDelete: true,
}
}
// AMQPQueue stores AMQP Queue configuration
type AMQPQueue struct {
Name string
Durable bool
AutoDelete bool
}
// NewAMQPQueue creates new AMQPQueue
func NewAMQPQueue(name string) *AMQPQueue {
return &AMQPQueue{
Name: name,
Durable: true,
AutoDelete: false,
}
}
//AMQPCeleryBroker is RedisBroker for AMQP
type AMQPCeleryBroker struct {
*amqp.Channel
connection *amqp.Connection
exchange *AMQPExchange
queue *AMQPQueue
consumingChannel <-chan amqp.Delivery
rate int
}
// NewAMQPConnection creates new AMQP channel
func NewAMQPConnection(host string) (*amqp.Connection, *amqp.Channel) {
connection, err := amqp.Dial(host)
if err != nil {
panic(err)
}
channel, err := connection.Channel()
if err != nil {
panic(err)
}
return connection, channel
}
// NewAMQPCeleryBroker creates new AMQPCeleryBroker
func NewAMQPCeleryBroker(host string, queueName string) *AMQPCeleryBroker {
connection, channel := NewAMQPConnection(host)
return NewAMQPCeleryBrokerByConnAndChannel(connection, channel, queueName)
}
// NewAMQPCeleryBrokerByConnAndChannel creates new AMQPCeleryBroker using AMQP conn and channel
func NewAMQPCeleryBrokerByConnAndChannel(conn *amqp.Connection, channel *amqp.Channel, queueName string) *AMQPCeleryBroker {
// ensure exchange is initialized
broker := &AMQPCeleryBroker{
Channel: channel,
connection: conn,
exchange: NewAMQPExchange("default"),
queue: NewAMQPQueue(queueName),
rate: 4,
}
if err := broker.CreateExchange(); err != nil {
panic(err)
}
if err := broker.CreateQueue(); err != nil {
panic(err)
}
if err := broker.Qos(broker.rate, 0, false); err != nil {
panic(err)
}
if err := broker.StartConsumingChannel(); err != nil {
panic(err)
}
return broker
}
// StartConsumingChannel spawns receiving channel on AMQP queue
func (b *AMQPCeleryBroker) StartConsumingChannel() error {
channel, err := b.Consume(b.queue.Name, "", false, false, false, false, nil)
if err != nil {
return err
}
b.consumingChannel = channel
return nil
}
// SendCeleryMessage sends CeleryMessage to broker
func (b *AMQPCeleryBroker) SendCeleryMessage(message *CeleryMessage, queueName string) error {
taskMessage := message.GetTaskMessage()
//log.Printf("sending task ID %s\n", taskMessage.ID)
_, err := b.QueueDeclare(
queueName, // name
true, // durable
false, // autoDelete
false, // exclusive
false, // noWait
nil, // args
)
if err != nil {
return err
}
err = b.ExchangeDeclare(
"default",
"direct",
true,
true,
false,
false,
nil,
)
if err != nil {
return err
}
resBytes, err := json.Marshal(taskMessage)
if err != nil {
return err
}
publishMessage := amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: "application/json",
Body: resBytes,
}
return b.Publish(
"",
queueName,
false,
false,
publishMessage,
)
}
// GetTaskMessage retrieves task message from AMQP queue
func (b *AMQPCeleryBroker) GetTaskMessage() (*TaskMessage, error) {
delivery := <-b.consumingChannel
delivery.Ack(false)
var taskMessage TaskMessage
if err := json.Unmarshal(delivery.Body, &taskMessage); err != nil {
return nil, err
}
return &taskMessage, nil
}
// CreateExchange declares AMQP exchange with stored configuration
func (b *AMQPCeleryBroker) CreateExchange() error {
return b.ExchangeDeclare(
b.exchange.Name,
b.exchange.Type,
b.exchange.Durable,
b.exchange.AutoDelete,
false,
false,
nil,
)
}
// CreateQueue declares AMQP Queue with stored configuration
func (b *AMQPCeleryBroker) CreateQueue() error {
_, err := b.QueueDeclare(
b.queue.Name,
b.queue.Durable,
b.queue.AutoDelete,
false,
false,
nil,
)
return err
}