@@ -4,18 +4,19 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "os"
8
+ "os/signal"
9
+ "strconv"
10
+
7
11
"github.com/KittenConnect/rh-api/model"
8
12
"github.com/KittenConnect/rh-api/util"
9
13
"github.com/joho/godotenv"
10
14
amqp "github.com/rabbitmq/amqp091-go"
11
- "os"
12
- "strconv"
13
- "time"
14
15
)
15
16
16
17
func failWithError (err error , formatString string , args ... any ) {
17
18
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 ())
19
20
}
20
21
}
21
22
@@ -107,8 +108,18 @@ func main() {
107
108
os .Exit (- 1 )
108
109
}
109
110
111
+ // cancel context for whole conde
112
+ foreverCtx , foreverCancel := context .WithCancel (context .Background ())
113
+
110
114
// 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
+ }()
112
123
113
124
go func () {
114
125
for d := range msgs {
@@ -125,10 +136,6 @@ func main() {
125
136
if err != nil {
126
137
util .Warn ("error creating or updating VM : %w" , err )
127
138
128
- dur , _ := time .ParseDuration ("10s" )
129
- ctx , cancel := context .WithTimeout (context .Background (), dur )
130
- defer cancel ()
131
-
132
139
newMsg := msg
133
140
newMsg .FailCount --
134
141
@@ -142,8 +149,7 @@ func main() {
142
149
"x-delay" : RETRY_DELAY * 1000 ,
143
150
}
144
151
145
- chErr := ch .PublishWithContext (
146
- ctx ,
152
+ chErr := ch .Publish (
147
153
incomingQueue ,
148
154
inQ .Name ,
149
155
false ,
@@ -165,16 +171,11 @@ func main() {
165
171
166
172
util .Success ("VM %s is up to date" , msg .Hostname )
167
173
168
- dur , _ := time .ParseDuration ("10s" )
169
- ctx , cancel := context .WithTimeout (context .Background (), dur )
170
- defer cancel ()
171
-
172
174
newMsg := msg
173
175
174
176
newMsgJson , _ := json .Marshal (newMsg )
175
177
176
- chErr := ch .PublishWithContext (
177
- ctx ,
178
+ chErr := ch .Publish (
178
179
"" ,
179
180
outQ .Name ,
180
181
false ,
@@ -191,8 +192,10 @@ func main() {
191
192
}
192
193
}()
193
194
}
195
+ util .Info ("End of queue reaches exit now !" )
196
+ foreverCancel ()
194
197
}()
195
198
196
199
util .Info (" [*] Waiting for messages. To exit press CTRL+C" )
197
- <- forever
200
+ <- foreverCtx . Done ()
198
201
}
0 commit comments