Skip to content

Commit 60eda57

Browse files
authored
refactor + fix (#6)
* fix: catch ctrl+c + end of amqp queue in main message loop * refactor: Change useless ch.PublishWithContex to ch.Publish, refer to amqp manual for more informations * fix: chamge NewNetbox retunr pointer to a model.Netbox insted of copy of struct * refactor: pack all WritableVirtualMachineWiithConfigContext to VirtualMachine->Get()
1 parent d4ad0ec commit 60eda57

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

main.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"os"
8+
"os/signal"
9+
"strconv"
10+
711
"github.com/KittenConnect/rh-api/model"
812
"github.com/KittenConnect/rh-api/util"
913
"github.com/joho/godotenv"
1014
amqp "github.com/rabbitmq/amqp091-go"
11-
"os"
12-
"strconv"
13-
"time"
1415
)
1516

1617
func failWithError(err error, formatString string, args ...any) {
1718
if err != nil {
18-
util.Err(fmt.Errorf(fmt.Sprintf("%s: %w",formatString), append(args, err)...).Error())
19+
util.Err(fmt.Errorf(fmt.Sprintf("%s: %w", formatString), append(args, err)...).Error())
1920
}
2021
}
2122

@@ -107,8 +108,18 @@ func main() {
107108
os.Exit(-1)
108109
}
109110

111+
// cancel context for whole conde
112+
foreverCtx, foreverCancel := context.WithCancel(context.Background())
113+
110114
// Canal pour signaler la fin du programme
111-
forever := make(chan bool)
115+
sigs := make(chan os.Signal, 1)
116+
signal.Notify(sigs, os.Interrupt)
117+
// catch signal
118+
go func() {
119+
<-sigs
120+
fmt.Printf("You pressed ctrl + C. User interrupted infinite loop.")
121+
foreverCancel()
122+
}()
112123

113124
go func() {
114125
for d := range msgs {
@@ -125,10 +136,6 @@ func main() {
125136
if err != nil {
126137
util.Warn("error creating or updating VM : %w", err)
127138

128-
dur, _ := time.ParseDuration("10s")
129-
ctx, cancel := context.WithTimeout(context.Background(), dur)
130-
defer cancel()
131-
132139
newMsg := msg
133140
newMsg.FailCount--
134141

@@ -142,8 +149,7 @@ func main() {
142149
"x-delay": RETRY_DELAY * 1000,
143150
}
144151

145-
chErr := ch.PublishWithContext(
146-
ctx,
152+
chErr := ch.Publish(
147153
incomingQueue,
148154
inQ.Name,
149155
false,
@@ -165,16 +171,11 @@ func main() {
165171

166172
util.Success("VM %s is up to date", msg.Hostname)
167173

168-
dur, _ := time.ParseDuration("10s")
169-
ctx, cancel := context.WithTimeout(context.Background(), dur)
170-
defer cancel()
171-
172174
newMsg := msg
173175

174176
newMsgJson, _ := json.Marshal(newMsg)
175177

176-
chErr := ch.PublishWithContext(
177-
ctx,
178+
chErr := ch.Publish(
178179
"",
179180
outQ.Name,
180181
false,
@@ -191,8 +192,10 @@ func main() {
191192
}
192193
}()
193194
}
195+
util.Info("End of queue reaches exit now !")
196+
foreverCancel()
194197
}()
195198

196199
util.Info(" [*] Waiting for messages. To exit press CTRL+C")
197-
<-forever
200+
<-foreverCtx.Done()
198201
}

model/VirtualMachine.go

+6-16
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ func NewVM(n *Netbox, msg Message) *VirtualMachine {
3636
return vm
3737
}
3838

39-
func (vm *VirtualMachine) Get() models.WritableVirtualMachineWithConfigContext {
39+
// dev chnage name ?
40+
// Get: return tableVirtualMachineWithConfigContext for vm
41+
func (vm *VirtualMachine) Get() *models.WritableVirtualMachineWithConfigContext {
4042
// todo: implement netbox func
41-
return models.WritableVirtualMachineWithConfigContext{
43+
return &models.WritableVirtualMachineWithConfigContext{
4244
Cluster: &vm.Cluster.ID,
4345
Name: &vm.Name,
4446
Status: vm.Status,
@@ -50,17 +52,7 @@ func (vm *VirtualMachine) Get() models.WritableVirtualMachineWithConfigContext {
5052
}
5153

5254
func (vm *VirtualMachine) Create(msg Message) (*virtualization.VirtualizationVirtualMachinesCreateCreated, error) {
53-
conf := models.WritableVirtualMachineWithConfigContext{
54-
Cluster: &vm.Cluster.ID,
55-
Name: &vm.Name,
56-
Status: vm.Status,
57-
58-
CustomFields: map[string]interface{}{
59-
"kc_serial_": msg.GetSerial(),
60-
},
61-
}
62-
63-
params := virtualization.NewVirtualizationVirtualMachinesCreateParams().WithData(&conf)
55+
params := virtualization.NewVirtualizationVirtualMachinesCreateParams().WithData(vm.Get())
6456
return vm.n.Client.Virtualization.VirtualizationVirtualMachinesCreate(params, nil)
6557
}
6658

@@ -70,10 +62,8 @@ func (vm *VirtualMachine) CreateOrUpdate(msg Message) {
7062

7163
// Update vm infos to netbox
7264
func (vm *VirtualMachine) Update() error {
73-
data := vm.Get()
74-
7565
updateParams := &virtualization.VirtualizationVirtualMachinesPartialUpdateParams{
76-
Data: &data,
66+
Data: vm.Get(),
7767
ID: vm.NetboxId,
7868
}
7969

model/netbox.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ type Netbox struct {
2727
}
2828

2929
// NewNetbox return a fresh Netbox object
30-
func NewNetbox() Netbox {
30+
func NewNetbox() *Netbox {
3131
nbx := Netbox{
3232
ctx: context.Background(),
3333
Client: nil,
3434

3535
_isConnected: false,
3636
}
3737

38-
return nbx
38+
return &nbx
3939
}
4040

4141
func (n *Netbox) IsConnected() bool {
@@ -54,7 +54,7 @@ func (n *Netbox) Connect() error {
5454
}
5555

5656
func (n *Netbox) GetDefaultTimeout() time.Duration {
57-
return time.Duration(30) * time.Second
57+
return 30 * time.Second
5858
}
5959

6060
func (n *Netbox) getIpAddress(ip string) *models.WritableIPAddress {

0 commit comments

Comments
 (0)