-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamqp_backend.go
236 lines (204 loc) · 5.5 KB
/
amqp_backend.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Gocelery is a task queue implementation for Go modules used to asynchronously execute work outside the HTTP request-response cycle. Celery is an implementation of the task queue concept.
*/
package gocelery
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
// "github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
)
// AMQPCeleryBackend CeleryBackend for AMQP
type AMQPCeleryBackend struct {
*amqp.Channel
Connection *amqp.Connection
Host string
}
// NewAMQPCeleryBackend creates new AMQPCeleryBackend
func NewAMQPCeleryBackend(host string) *AMQPCeleryBackend {
backend := NewAMQPCeleryBackendByConnAndChannel(NewAMQPConnection(host))
backend.Host = host
return backend
}
// NewAMQPCeleryBackendByConnAndChannel creates new AMQPCeleryBackend by AMQP connection and channel
func NewAMQPCeleryBackendByConnAndChannel(conn *amqp.Connection, channel *amqp.Channel) *AMQPCeleryBackend {
backend := &AMQPCeleryBackend{
Channel: channel,
Connection: conn,
}
return backend
}
// Reconnect reconnects to AMQP server
func (b *AMQPCeleryBackend) Reconnect() {
b.Connection.Close()
conn, channel := NewAMQPConnection(b.Host)
b.Channel = channel
b.Connection = conn
}
// GetResult retrieves result from AMQP queue
func (b *AMQPCeleryBackend) GetResult(taskID string) (*ResultMessage, error) {
queueName := strings.Replace(taskID, "-", "", -1)
args := amqp.Table{"x-expires": int32(86400000)}
_, err := b.QueueDeclare(
queueName, // name
true, // durable
true, // autoDelete
false, // exclusive
false, // noWait
args, // args
)
if err != nil {
return nil, err
}
err = b.ExchangeDeclare(
"default",
"direct",
true,
true,
false,
false,
nil,
)
if err != nil {
return nil, err
}
// open channel temporarily
channel, err := b.Consume(queueName, "", false, false, false, false, nil)
if err != nil {
return nil, err
}
var resultMessage ResultMessage
delivery := <-channel
deliveryAck(delivery)
if err := json.Unmarshal(delivery.Body, &resultMessage); err != nil {
return nil, err
}
return &resultMessage, nil
}
// SetResult sets result back to AMQP queue
func (b *AMQPCeleryBackend) SetResult(taskID string, result *ResultMessage) error {
result.ID = taskID
//queueName := taskID
queueName := strings.Replace(taskID, "-", "", -1)
// autodelete is automatically set to true by python
// (406) PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'bc58c0d895c7421eb7cb2b9bbbd8b36f' in vhost '/': received 'true' but current is 'false'
args := amqp.Table{"x-expires": int32(86400000)}
_, err := b.QueueDeclare(
queueName, // name
true, // durable
true, // autoDelete
false, // exclusive
false, // noWait
args, // 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(result)
if err != nil {
return err
}
message := amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: "application/json",
Body: resBytes,
}
return b.Publish(
"",
queueName,
false,
false,
message,
)
}
/*
SimpleConnectionRabbitMQ is for publish one message to the specefic queue.
in this function the message which it will be given from the parameters will be publish into the RABBIT_QUEUE_RESULT which you declare in the .env file.
*/
func SimpleConnectionRabbitMQ(message, username, password, url, queue, task string) error {
// read from .env file
rabbit_url := fmt.Sprintf("amqp://%s:%s@%s/", username, password, url)
var rabbitMQConnenctionStatusCeleryErrorMessage bool = true
for {
// connect to the rabbitMQ
conn, err := amqp.Dial(rabbit_url)
if err != nil {
if rabbitMQConnenctionStatusCeleryErrorMessage {
log.Printf("we have an error in connecting the rabbitMQ!\n%v", err)
rabbitMQConnenctionStatusCeleryErrorMessage = !rabbitMQConnenctionStatusCeleryErrorMessage
}
} else {
rabbitMQConnenctionStatusCeleryErrorMessage = !rabbitMQConnenctionStatusCeleryErrorMessage
_ = rabbitMQConnenctionStatusCeleryErrorMessage
defer conn.Close()
// create channel
ch, err := conn.Channel()
if err != nil {
log.Printf("we have an error in creating the channel!\n%v", err)
}
defer ch.Close()
// create queue
_, err = ch.QueueDeclare(
queue, // name
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Printf("we have an error in creating the queue!\n%v", err)
}
msgKwargs := make(map[string]interface{})
msgKwargs["msg"] = message
messageContent := getTaskMessage(task)
messageContent.Kwargs = msgKwargs
currentTime := time.Now()
nextDay := currentTime.AddDate(0, 0, 1)
messageContent.Expires = &nextDay
resBytes, err := json.Marshal(messageContent)
if err != nil {
return err
}
// publish message into the queue
errPublish := ch.Publish(
"",
queue,
false,
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: "application/json",
Body: resBytes,
},
)
if errPublish != nil {
log.Printf("we have an error in publishing the message in queue!\n%v", errPublish)
return errPublish
}
return nil
}
secondNumber, _ := strconv.Atoi(os.Getenv("RABBIT_INTERVAL"))
time.Sleep(time.Second * time.Duration(secondNumber))
_ = conn
}
}