-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtaskqueue.go
More file actions
65 lines (59 loc) · 1.19 KB
/
taskqueue.go
File metadata and controls
65 lines (59 loc) · 1.19 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
package main
import (
"log"
"time"
"github.com/streadway/amqp"
)
const (
MaxRetryCount = 20
SleepDuration = 10
)
func getChannelForMessages(conn *amqp.Connection) (<- chan amqp.Delivery, error) {
ch, err := conn.Channel()
if err != nil {
log.Println(err)
return nil, err
}
q, err := ch.QueueDeclare(
"task_queue", // name
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
log.Println(err)
return nil, err
}
err = ch.Qos(
1, // prefetch count
0, // prefetch size
false, // global
)
if err != nil {
log.Println(err)
return nil, err
}
return ch.Consume(
q.Name, // queue
"", // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
}
func getTaskQueueConnection() (*amqp.Connection, error) {
conn, err := amqp.Dial(configs.Services.RabbitMq)
for retry := 0; retry < MaxRetryCount && err != nil; retry++ {
time.Sleep(SleepDuration * time.Second)
conn, err = amqp.Dial(configs.Services.RabbitMq)
}
if err != nil {
log.Println(err)
return nil, err
}
return conn, nil
}