-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
201 lines (166 loc) · 4.25 KB
/
main.go
File metadata and controls
201 lines (166 loc) · 4.25 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"strconv"
"github.com/KittenConnect/rh-api/model"
"github.com/KittenConnect/rh-api/util"
"github.com/joho/godotenv"
amqp "github.com/rabbitmq/amqp091-go"
)
func failWithError(err error, formatString string, args ...any) {
if err != nil {
util.Err(fmt.Errorf(fmt.Sprintf("%s: %w", formatString), append(args, err)...).Error())
}
}
var RETRY_DELAY = 5
func main() {
err := godotenv.Load()
failWithError(err, "Error loading .env file")
conn, err := amqp.Dial(os.Getenv("RABBITMQ_URL"))
failWithError(err, "Failed to connect to broker")
defer conn.Close()
ch, err := conn.Channel()
failWithError(err, "Failed to open a channel")
incomingQueue := os.Getenv("RABBITMQ_INCOMING_QUEUE")
outgoingQueue := os.Getenv("RABBITMQ_OUTGOING_QUEUE")
if value, ok := os.LookupEnv("RABBITMQ_RETRY_DELAY"); ok {
if i, err := strconv.Atoi(value); err == nil {
RETRY_DELAY = i
}
}
inQ, err := ch.QueueDeclare(
incomingQueue,
true,
false,
false,
false,
nil,
)
failWithError(err, "Failed to declare queue %s", incomingQueue)
outQ, err := ch.QueueDeclare(
outgoingQueue,
true,
false,
false,
false,
nil,
)
failWithError(err, "Failed to declare queue %s", outgoingQueue)
exchangeArgs := map[string]interface{}{
"x-delayed-type": "direct",
}
err = ch.ExchangeDeclare(
incomingQueue,
"x-delayed-message",
true,
false,
false,
false,
exchangeArgs,
)
failWithError(err, "Failed to declare exchange %s", incomingQueue)
err = ch.QueueBind(
incomingQueue, // queue name
incomingQueue, // routing key
incomingQueue, // exchange
false,
nil)
failWithError(err, "Failed to bind queue %s to exchange %s", incomingQueue, incomingQueue)
// Consommation des messages
msgs, err := ch.Consume(
inQ.Name, // nom de la queue
"consumer", // consumer
true, // autoAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
failWithError(err, "Failed to register %s consumer", inQ.Name)
util.Info("Connected to message broker")
netbox := model.NewNetbox()
err = netbox.Connect()
failWithError(err, "Failed to connect to netbox")
if netbox.IsConnected() == false {
util.Err("Unable to connect to netbox")
os.Exit(-1)
}
// cancel context for whole conde
foreverCtx, foreverCancel := context.WithCancel(context.Background())
// Canal pour signaler la fin du programme
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
// catch signal
go func() {
<-sigs
fmt.Printf("You pressed ctrl + C. User interrupted infinite loop.")
foreverCancel()
}()
go func() {
for d := range msgs {
go func() {
msg := model.Message{Timestamp: d.Timestamp, FailCount: 20}
err = json.Unmarshal(d.Body, &msg)
if err != nil {
util.Warn("Error unmarshalling message : %w", err)
return
}
//Make request to the rest of API
err = netbox.CreateOrUpdateVM(msg)
if err != nil {
util.Warn("error creating or updating VM : %w", err)
newMsg := msg
newMsg.FailCount--
if newMsg.FailCount <= 0 {
return
}
newMsgJson, _ := json.Marshal(newMsg)
headers := amqp.Table{
"x-delay": RETRY_DELAY * 1000,
}
chErr := ch.Publish(
incomingQueue,
inQ.Name,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: newMsgJson,
Headers: headers,
})
if chErr != nil {
util.Warn("Error re-publishing message: %s", chErr)
} else {
util.Warn("Re-sent message to RabbitMQ®️: %s", newMsgJson)
}
return
}
util.Success("VM %s is up to date", msg.Hostname)
newMsg := msg
newMsgJson, _ := json.Marshal(newMsg)
chErr := ch.Publish(
"",
outQ.Name,
false,
false,
amqp.Publishing{
ContentType: "application/json",
Body: newMsgJson,
})
if chErr != nil {
util.Warn("Error publishing success message: %s", chErr)
} else {
util.Success("sent success message to RabbitMQ®️: %s", newMsgJson)
}
}()
}
util.Info("End of queue reaches exit now !")
foreverCancel()
}()
util.Info(" [*] Waiting for messages. To exit press CTRL+C")
<-foreverCtx.Done()
}