timetable-mgr/src/main.go

52 lines
1.1 KiB
Go
Raw Normal View History

2023-07-15 22:32:46 +01:00
package main
import (
"os"
"os/signal"
"syscall"
2023-07-17 12:48:36 +01:00
"git.fjla.uk/owlboard/mq-client/dbAccess"
2023-07-18 14:09:28 +01:00
"git.fjla.uk/owlboard/mq-client/helpers"
"git.fjla.uk/owlboard/mq-client/log"
"git.fjla.uk/owlboard/mq-client/messaging"
2023-07-19 21:31:00 +01:00
"git.fjla.uk/owlboard/mq-client/vstp"
2023-07-15 22:32:46 +01:00
)
func main() {
2023-07-18 14:09:28 +01:00
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
2023-07-21 10:02:55 +01:00
defer cleanup()
go handleSignals()
2023-07-19 21:31:00 +01:00
vstp.Subscribe()
2023-07-18 14:09:28 +01:00
}
2023-07-21 10:02:55 +01:00
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
func handleSignals() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
log.Msg.Warn("Signal received: " + sig.String())
cleanup()
os.Exit(1)
}
// Cleans up open connections ready for a clean exit of the program
2023-07-21 10:02:55 +01:00
func cleanup() {
log.Msg.Debug("Cleaning up open connections")
if messaging.Client != nil {
log.Msg.Info("Closing STOMP Client")
messaging.Disconnect(messaging.Client)
}
if dbAccess.MongoClient != nil {
log.Msg.Info("Closing MongoDB Client")
dbAccess.CloseMongoClient()
}
2023-07-21 10:02:55 +01:00
log.Msg.Info("Program ready to exit")
if log.Msg != nil {
log.Msg.Sync()
}
2023-07-21 10:02:55 +01:00
}