timetable-mgr/src/main.go

66 lines
1.4 KiB
Go
Raw Normal View History

2023-07-15 22:32:46 +01:00
package main
import (
2024-03-25 00:42:36 +00:00
"fmt"
"os"
"os/signal"
"syscall"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
"git.fjla.uk/owlboard/timetable-mgr/messaging"
"git.fjla.uk/owlboard/timetable-mgr/vstp"
2023-07-15 22:32:46 +01:00
)
func main() {
2024-03-25 00:42:36 +00:00
cfg, err := helpers.LoadConfig()
if err != nil {
fmt.Println("Error loading configuration", err)
return
}
cfg.PrintConfig()
log.Msg.Info("Initialised OwlBoard timetable-mgr " + helpers.Version)
defer cleanup(cfg)
if cfg.VstpOn {
messaging.StompInit(cfg)
go handleSignals(cfg)
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(cfg *helpers.Configuration) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
log.Msg.Warn("Signal received: " + sig.String())
cleanup(cfg)
os.Exit(1)
}
// Cleans up open connections ready for a clean exit of the program
func cleanup(cfg *helpers.Configuration) {
2023-07-21 10:02:55 +01:00
log.Msg.Debug("Cleaning up open connections")
if cfg.VstpOn {
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
}