2023-07-15 22:32:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-25 00:42:36 +00:00
|
|
|
"fmt"
|
2023-07-21 12:12:16 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2024-03-25 14:03:19 +00:00
|
|
|
"time"
|
2023-07-21 12:12:16 +01:00
|
|
|
|
2024-03-26 11:44:19 +00:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/background"
|
2024-03-25 11:26:07 +00:00
|
|
|
"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()
|
|
|
|
|
2024-03-23 11:34:52 +00:00
|
|
|
log.Msg.Info("Initialised OwlBoard timetable-mgr " + helpers.Version)
|
2023-07-21 12:12:16 +01:00
|
|
|
|
2024-03-25 12:21:59 +00:00
|
|
|
dbAccess.InitDataAccess(cfg)
|
|
|
|
dbAccess.PushVersionToDb()
|
|
|
|
|
2024-03-25 14:03:19 +00:00
|
|
|
// Initialise a `stop` channel to signal goroutines to cleanup
|
|
|
|
stop := make(chan struct{})
|
|
|
|
|
2024-03-25 12:21:59 +00:00
|
|
|
// Handle signals from the OS
|
2024-03-25 14:03:19 +00:00
|
|
|
go handleSignals(cfg, stop)
|
2024-03-25 12:21:59 +00:00
|
|
|
|
2024-03-25 14:03:19 +00:00
|
|
|
// Defer cleanup task
|
|
|
|
//defer cleanup(cfg, stop)
|
2023-07-21 12:12:16 +01:00
|
|
|
|
2024-03-25 12:21:59 +00:00
|
|
|
// Start CIF Task Ticker
|
2024-03-26 11:44:19 +00:00
|
|
|
background.InitTicker(cfg, stop)
|
2024-03-25 12:21:59 +00:00
|
|
|
|
2024-03-25 11:26:07 +00:00
|
|
|
if cfg.VstpOn {
|
|
|
|
messaging.StompInit(cfg)
|
|
|
|
vstp.Subscribe()
|
|
|
|
}
|
2024-03-25 12:21:59 +00:00
|
|
|
|
2024-03-25 14:03:19 +00:00
|
|
|
<-stop
|
2023-07-18 14:09:28 +01:00
|
|
|
}
|
2023-07-21 10:02:55 +01:00
|
|
|
|
2023-07-21 12:12:16 +01:00
|
|
|
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
2024-03-25 14:03:19 +00:00
|
|
|
func handleSignals(cfg *helpers.Configuration, stop chan<- struct{}) {
|
2023-07-21 12:12:16 +01:00
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
sig := <-sigChan
|
|
|
|
log.Msg.Warn("Signal received: " + sig.String())
|
2024-03-25 14:03:19 +00:00
|
|
|
cleanup(cfg, stop)
|
2023-07-21 12:12:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleans up open connections ready for a clean exit of the program
|
2024-03-25 14:03:19 +00:00
|
|
|
func cleanup(cfg *helpers.Configuration, stop chan<- struct{}) {
|
2023-07-21 10:02:55 +01:00
|
|
|
log.Msg.Debug("Cleaning up open connections")
|
2024-03-25 11:26:07 +00:00
|
|
|
if cfg.VstpOn {
|
|
|
|
if messaging.Client != nil {
|
|
|
|
log.Msg.Info("Closing STOMP Client")
|
|
|
|
messaging.Disconnect(messaging.Client)
|
|
|
|
}
|
2023-07-21 12:12:16 +01:00
|
|
|
}
|
|
|
|
if dbAccess.MongoClient != nil {
|
|
|
|
log.Msg.Info("Closing MongoDB Client")
|
|
|
|
dbAccess.CloseMongoClient()
|
|
|
|
}
|
2024-03-25 14:03:19 +00:00
|
|
|
log.Msg.Info("Signalling to other goroutines")
|
|
|
|
close(stop)
|
|
|
|
|
2023-07-21 10:02:55 +01:00
|
|
|
log.Msg.Info("Program ready to exit")
|
2023-07-21 12:12:16 +01:00
|
|
|
if log.Msg != nil {
|
|
|
|
log.Msg.Sync()
|
|
|
|
}
|
2024-03-25 14:03:19 +00:00
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
os.Exit(0)
|
2023-07-21 10:02:55 +01:00
|
|
|
}
|