forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
amqp_broker.go
199 lines (180 loc) · 4.24 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
194
195
196
197
198
199
// Copyright (c) 2019 Sick Yoon
// This file is part of gocelery which is released under MIT license.
// See file LICENSE for full license details.
package gocelery
import (
"encoding/json"
"fmt"
amqp "github.com/rabbitmq/amqp091-go"
"time"
)
// 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) *AMQPCeleryBroker {
return NewAMQPCeleryBrokerByConnAndChannel(NewAMQPConnection(host))
}
// NewAMQPCeleryBrokerByConnAndChannel creates new AMQPCeleryBroker using AMQP conn and channel
func NewAMQPCeleryBrokerByConnAndChannel(conn *amqp.Connection, channel *amqp.Channel) *AMQPCeleryBroker {
broker := &AMQPCeleryBroker{
Channel: channel,
Connection: conn,
Exchange: NewAMQPExchange("default"),
Queue: NewAMQPQueue("celery"),
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) error {
taskMessage := message.GetTaskMessage()
queueName := "celery"
_, 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) {
select {
case delivery := <-b.consumingChannel:
deliveryAck(delivery)
var taskMessage TaskMessage
if err := json.Unmarshal(delivery.Body, &taskMessage); err != nil {
return nil, err
}
return &taskMessage, nil
default:
return nil, fmt.Errorf("consuming channel is empty")
}
}
// 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
}