-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbit_utility.go
144 lines (117 loc) · 3.22 KB
/
rabbit_utility.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/streadway/amqp"
)
type rabbitClient struct {
connString string
queueString string
channel *amqp.Channel
queue amqp.Queue
size int
buffer []string
gpssclient *gpssClient
filePath string
fileBatch *os.File
mode int
}
func makeRabbitClient(connString string, queueString string, size int, gpssclient *gpssClient, mode int, filePath string) *rabbitClient {
client := new(rabbitClient)
client.connString = connString
client.queueString = queueString
client.size = size
client.buffer = make([]string, size)
client.gpssclient = gpssclient
client.mode = mode
// if persistency is set then open the file
if client.mode == 1 {
client.filePath = filePath
var err error
client.fileBatch, err = os.OpenFile(client.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
client.failOnError(err, "error opening text file")
}
}
return client
}
func (client *rabbitClient) failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
}
}
func (client *rabbitClient) connect() (*amqp.Channel, amqp.Queue) {
conn, err := amqp.Dial(client.connString)
client.failOnError(err, "Failed to connect to RabbitMQ")
//defer conn.Close()
ch, err := conn.Channel()
client.failOnError(err, "Failed to open a channel")
//defer ch.Close()
client.channel = ch
q, err := ch.QueueDeclare(
client.queueString, // name
true, // durable
false, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
client.failOnError(err, "Failed to declare a queue")
client.queue = q
return ch, q
}
func (client *rabbitClient) consume(mode bool) {
msgs, err := client.channel.Consume(
client.queue.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
client.failOnError(err, "Failed to register a consumer")
forever := make(chan bool)
go func() {
count := 0
for d := range msgs {
//log.Printf("Received a message: %s", d.Body)
if client.mode == 1 {
// persistence of batch activated
client.writeToFile(string(d.Body))
}
client.buffer[count] = string(d.Body)
count++
if count == client.size {
log.Printf("Batch reached: I'm sending request to write to gpss/gprc server")
client.gpssclient.ConnectToGreenplumDatabase()
client.gpssclient.WriteToGreenplum(client.buffer)
if client.mode == 1 {
// persistence of batch activated
client.truncateFile()
}
client.gpssclient.DisconnectToGreenplumDatabase()
count = 0
// Just for testing purpose
if mode == false {
close(forever)
}
}
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}
func (client *rabbitClient) writeToFile(message string) {
//log.Printf("writing to file")
w := bufio.NewWriter(client.fileBatch)
message = message + "\n"
fmt.Fprintf(w, "%v", message)
w.Flush()
}
func (client *rabbitClient) truncateFile() {
//log.Printf("truncating the file")
os.Truncate(client.filePath, 0)
}