Move ticker to new module
This commit is contained in:
parent
e204fb04a4
commit
cf633eeb8f
30
src/background/ticker.go
Normal file
30
src/background/ticker.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package background
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
||||||
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||||
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
const frequency = 5 * time.Millisecond // Figure out a sensible frequency!
|
||||||
|
|
||||||
|
func InitTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||||
|
go runTicker(cfg, stop)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
|
||||||
|
log.Msg.Sugar().Infof("Starting background ticker, runs every %s", frequency)
|
||||||
|
ticker := time.NewTicker(frequency)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
cif.CifCheck(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,27 +0,0 @@
|
|||||||
package cif
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Start a background task to periodically check and update the timetable from the CIF file feed.
|
|
||||||
|
|
||||||
func InitBackgroundTask(cfg *helpers.Configuration, stop <-chan struct{}) {
|
|
||||||
go runBackgroundTask(cfg, stop)
|
|
||||||
}
|
|
||||||
|
|
||||||
func runBackgroundTask(cfg *helpers.Configuration, stop <-chan struct{}) {
|
|
||||||
ticker := time.NewTicker(10 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-stop:
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
cifCheck(cfg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,7 +8,7 @@ import (
|
|||||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func cifCheck(cfg *helpers.Configuration) {
|
func CifCheck(cfg *helpers.Configuration) {
|
||||||
log.Msg.Debug("CIF Task Started")
|
log.Msg.Debug("CIF Task Started")
|
||||||
numGoroutines := runtime.NumGoroutine()
|
numGoroutines := runtime.NumGoroutine()
|
||||||
fmt.Println("Number of goroutines running: ", numGoroutines)
|
fmt.Println("Number of goroutines running: ", numGoroutines)
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
"git.fjla.uk/owlboard/timetable-mgr/background"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
"git.fjla.uk/owlboard/timetable-mgr/helpers"
|
||||||
"git.fjla.uk/owlboard/timetable-mgr/log"
|
"git.fjla.uk/owlboard/timetable-mgr/log"
|
||||||
@ -39,7 +39,7 @@ func main() {
|
|||||||
//defer cleanup(cfg, stop)
|
//defer cleanup(cfg, stop)
|
||||||
|
|
||||||
// Start CIF Task Ticker
|
// Start CIF Task Ticker
|
||||||
cif.InitBackgroundTask(cfg, stop)
|
background.InitTicker(cfg, stop)
|
||||||
|
|
||||||
if cfg.VstpOn {
|
if cfg.VstpOn {
|
||||||
messaging.StompInit(cfg)
|
messaging.StompInit(cfg)
|
||||||
|
Loading…
Reference in New Issue
Block a user