timetable-mgr/background/ticker.go

57 lines
1.4 KiB
Go
Raw Normal View History

2024-03-26 11:44:19 +00:00
package background
import (
"fmt"
"runtime"
2024-03-26 11:44:19 +00:00
"time"
"git.fjla.uk/owlboard/timetable-mgr/cif"
"git.fjla.uk/owlboard/timetable-mgr/corpus"
2024-03-26 11:44:19 +00:00
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
)
const frequency = 2 * time.Hour // Figure out a sensible frequency!
2024-03-26 11:44:19 +00:00
// Starts a background ticker to run background tasks. Uses the frequency configured in the background/ticker.go file
2024-03-26 11:44:19 +00:00
func InitTicker(cfg *helpers.Configuration, stop <-chan struct{}) {
go runTicker(cfg, stop)
// Run goroutine logging ticker if runtime set to debug
if helpers.Runtime == "debug" {
go goroutineTicker(stop)
}
2024-03-26 11:44:19 +00:00
}
// Runs the ticker and handles tick events
2024-03-26 11:44:19 +00:00
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:
2024-04-03 22:25:27 +01:00
go cif.CheckCif(cfg)
go corpus.CheckCorpus(cfg)
2024-03-26 11:44:19 +00:00
}
}
}
// Starts a ticker that logs how many goroutines are running every two seconds
func goroutineTicker(stop <-chan struct{}) {
log.Msg.Warn("Starting goroutine Tracker ticker - DEBUG USE ONLY")
ticker := time.NewTicker(1000 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
fmt.Printf("Number of goroutines running: %d\n", runtime.NumGoroutine())
}
}
}