timetable-extension #1
@ -8,15 +8,20 @@ import (
|
|||||||
|
|
||||||
// Start a background task to periodically check and update the timetable from the CIF file feed.
|
// Start a background task to periodically check and update the timetable from the CIF file feed.
|
||||||
|
|
||||||
func InitBackgroundTask(cfg *helpers.Configuration) {
|
func InitBackgroundTask(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||||
go runBackgroundTask(cfg)
|
go runBackgroundTask(cfg, stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runBackgroundTask(cfg *helpers.Configuration) {
|
func runBackgroundTask(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||||
ticker := time.NewTicker(10 * time.Second)
|
ticker := time.NewTicker(10 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
cifCheck(cfg)
|
cifCheck(cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
29
src/main.go
29
src/main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||||
@ -28,35 +29,38 @@ func main() {
|
|||||||
dbAccess.InitDataAccess(cfg)
|
dbAccess.InitDataAccess(cfg)
|
||||||
dbAccess.PushVersionToDb()
|
dbAccess.PushVersionToDb()
|
||||||
|
|
||||||
// Handle signals from the OS
|
// Initialise a `stop` channel to signal goroutines to cleanup
|
||||||
go handleSignals(cfg)
|
stop := make(chan struct{})
|
||||||
|
|
||||||
defer cleanup(cfg)
|
// Handle signals from the OS
|
||||||
|
go handleSignals(cfg, stop)
|
||||||
|
|
||||||
|
// Defer cleanup task
|
||||||
|
//defer cleanup(cfg, stop)
|
||||||
|
|
||||||
// Start CIF Task Ticker
|
// Start CIF Task Ticker
|
||||||
cif.InitBackgroundTask(cfg)
|
cif.InitBackgroundTask(cfg, stop)
|
||||||
|
|
||||||
if cfg.VstpOn {
|
if cfg.VstpOn {
|
||||||
messaging.StompInit(cfg)
|
messaging.StompInit(cfg)
|
||||||
vstp.Subscribe()
|
vstp.Subscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
select {}
|
<-stop
|
||||||
}
|
}
|
||||||
|
|
||||||
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
// Traps SIGINT and SIGTERM signals and ensures cleanup() is run
|
||||||
func handleSignals(cfg *helpers.Configuration) {
|
func handleSignals(cfg *helpers.Configuration, stop chan<- struct{}) {
|
||||||
sigChan := make(chan os.Signal, 1)
|
sigChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
sig := <-sigChan
|
sig := <-sigChan
|
||||||
log.Msg.Warn("Signal received: " + sig.String())
|
log.Msg.Warn("Signal received: " + sig.String())
|
||||||
cleanup(cfg)
|
cleanup(cfg, stop)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleans up open connections ready for a clean exit of the program
|
// Cleans up open connections ready for a clean exit of the program
|
||||||
func cleanup(cfg *helpers.Configuration) {
|
func cleanup(cfg *helpers.Configuration, stop chan<- struct{}) {
|
||||||
log.Msg.Debug("Cleaning up open connections")
|
log.Msg.Debug("Cleaning up open connections")
|
||||||
if cfg.VstpOn {
|
if cfg.VstpOn {
|
||||||
if messaging.Client != nil {
|
if messaging.Client != nil {
|
||||||
@ -68,8 +72,15 @@ func cleanup(cfg *helpers.Configuration) {
|
|||||||
log.Msg.Info("Closing MongoDB Client")
|
log.Msg.Info("Closing MongoDB Client")
|
||||||
dbAccess.CloseMongoClient()
|
dbAccess.CloseMongoClient()
|
||||||
}
|
}
|
||||||
|
log.Msg.Info("Signalling to other goroutines")
|
||||||
|
close(stop)
|
||||||
|
|
||||||
log.Msg.Info("Program ready to exit")
|
log.Msg.Info("Program ready to exit")
|
||||||
if log.Msg != nil {
|
if log.Msg != nil {
|
||||||
log.Msg.Sync()
|
log.Msg.Sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
BIN
src/timetable-mgr
Executable file
BIN
src/timetable-mgr
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user