Organize cleanup code

This commit is contained in:
Fred Boniface 2023-07-21 10:02:55 +01:00
parent 849f23b507
commit 9fa0f7d61f
3 changed files with 22 additions and 3 deletions

View File

@ -54,3 +54,15 @@ func initDataAccess() *mongo.Client {
}
return client
}
func CloseMongoClient() {
if MongoClient != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := MongoClient.Disconnect(ctx); err != nil {
log.Msg.Warn("Error disconnecting MongoDB client: " + err.Error())
} else {
log.Msg.Info("MongoDB client disconnected.")
}
}
}

View File

@ -10,7 +10,14 @@ import (
func main() {
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
defer cleanup()
dbAccess.PrintFromDbPackage()
defer messaging.Disconnect(messaging.Client)
vstp.Subscribe()
}
func cleanup() {
log.Msg.Debug("Cleaning up open connections")
defer messaging.Disconnect(messaging.Client)
defer dbAccess.CloseMongoClient()
log.Msg.Info("Program ready to exit")
}

View File

@ -19,9 +19,9 @@ func Subscribe() {
for {
msg := <-sub.C
if msg.Err != nil {
log.Msg.Error("\nSTOMP Message Error: " + msg.Err.Error())
log.Msg.Error("STOMP Message Error: " + msg.Err.Error())
} else {
log.Msg.Debug("\nSTOMP Message Received")
log.Msg.Debug("STOMP Message Received")
saveToFile(string(msg.Body), "msgBody")
handle(msg)
}