From cf633eeb8f3babcc621fa7663fba1b20b35f5ad4 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 26 Mar 2024 11:44:19 +0000 Subject: [PATCH] Move ticker to new module --- src/background/ticker.go | 30 ++++++++++++++++++++++++++++++ src/cif/initialise.go | 27 --------------------------- src/cif/runner.go | 2 +- src/main.go | 4 ++-- 4 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 src/background/ticker.go delete mode 100644 src/cif/initialise.go diff --git a/src/background/ticker.go b/src/background/ticker.go new file mode 100644 index 0000000..e69f728 --- /dev/null +++ b/src/background/ticker.go @@ -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) + } + } +} diff --git a/src/cif/initialise.go b/src/cif/initialise.go deleted file mode 100644 index 8d033ad..0000000 --- a/src/cif/initialise.go +++ /dev/null @@ -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) - } - } -} diff --git a/src/cif/runner.go b/src/cif/runner.go index 7efea7b..15c0f45 100644 --- a/src/cif/runner.go +++ b/src/cif/runner.go @@ -8,7 +8,7 @@ import ( "git.fjla.uk/owlboard/timetable-mgr/log" ) -func cifCheck(cfg *helpers.Configuration) { +func CifCheck(cfg *helpers.Configuration) { log.Msg.Debug("CIF Task Started") numGoroutines := runtime.NumGoroutine() fmt.Println("Number of goroutines running: ", numGoroutines) diff --git a/src/main.go b/src/main.go index 24589aa..29ba5f6 100644 --- a/src/main.go +++ b/src/main.go @@ -7,7 +7,7 @@ import ( "syscall" "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/helpers" "git.fjla.uk/owlboard/timetable-mgr/log" @@ -39,7 +39,7 @@ func main() { //defer cleanup(cfg, stop) // Start CIF Task Ticker - cif.InitBackgroundTask(cfg, stop) + background.InitTicker(cfg, stop) if cfg.VstpOn { messaging.StompInit(cfg)