timetable-mgr/src/main.go

52 lines
1.1 KiB
Go

package main
import (
"os"
"os/signal"
"syscall"
"git.fjla.uk/owlboard/mq-client/dbAccess"
"git.fjla.uk/owlboard/mq-client/helpers"
"git.fjla.uk/owlboard/mq-client/log"
"git.fjla.uk/owlboard/mq-client/messaging"
"git.fjla.uk/owlboard/mq-client/vstp"
)
func main() {
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
defer cleanup()
go handleSignals()
vstp.Subscribe()
}
// 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
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()
}
log.Msg.Info("Program ready to exit")
if log.Msg != nil {
log.Msg.Sync()
}
}