2024-03-26 11:44:19 +00:00
|
|
|
package background
|
|
|
|
|
|
|
|
import (
|
2024-04-13 21:45:05 +01:00
|
|
|
"math"
|
2024-04-14 20:07:56 +01:00
|
|
|
"os"
|
2024-04-08 21:22:56 +01:00
|
|
|
"runtime"
|
2024-03-26 11:44:19 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/cif"
|
2024-03-26 22:33:11 +00:00
|
|
|
"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"
|
2024-10-22 20:50:08 +01:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/pis"
|
2024-06-30 19:29:41 +01:00
|
|
|
"git.fjla.uk/owlboard/timetable-mgr/stations"
|
2024-04-13 21:45:05 +01:00
|
|
|
"go.uber.org/zap"
|
2024-03-26 11:44:19 +00:00
|
|
|
)
|
|
|
|
|
2024-04-08 21:22:56 +01:00
|
|
|
const frequency = 2 * time.Hour // Figure out a sensible frequency!
|
2024-03-26 11:44:19 +00:00
|
|
|
|
2024-03-29 14:01:57 +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)
|
2024-04-08 21:22:56 +01:00
|
|
|
|
2024-04-14 20:07:56 +01:00
|
|
|
// Run goroutine logging ticker if env "perflog" is set to "on"
|
|
|
|
if os.Getenv("perflog") == "on" {
|
2024-04-08 21:22:56 +01:00
|
|
|
go goroutineTicker(stop)
|
|
|
|
}
|
2024-03-26 11:44:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:01:57 +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{}) {
|
2024-04-14 20:00:34 +01:00
|
|
|
log.Info("Starting background task ticker", zap.Duration("frequency", frequency))
|
2024-03-26 11:44:19 +00:00
|
|
|
ticker := time.NewTicker(frequency)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
2024-04-14 20:07:56 +01:00
|
|
|
log.Debug("Stopping background task ticker")
|
2024-03-26 11:44:19 +00:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2024-04-14 20:07:56 +01:00
|
|
|
log.Debug("Running background tasks")
|
2024-04-03 22:25:27 +01:00
|
|
|
go cif.CheckCif(cfg)
|
2024-03-26 22:33:11 +00:00
|
|
|
go corpus.CheckCorpus(cfg)
|
2024-06-30 19:29:41 +01:00
|
|
|
go stations.Check()
|
2024-10-22 20:50:08 +01:00
|
|
|
go pis.Check()
|
2024-03-26 11:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 21:22:56 +01:00
|
|
|
|
|
|
|
// Starts a ticker that logs how many goroutines are running every two seconds
|
|
|
|
func goroutineTicker(stop <-chan struct{}) {
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Debug("Starting goroutine resource logging ticker")
|
2024-04-08 21:22:56 +01:00
|
|
|
ticker := time.NewTicker(1000 * time.Millisecond)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2024-04-08 21:39:52 +01:00
|
|
|
debugLog()
|
2024-04-08 21:22:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-08 21:39:52 +01:00
|
|
|
|
|
|
|
func debugLog() {
|
|
|
|
var memStats runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&memStats)
|
|
|
|
|
|
|
|
goroutines := runtime.NumGoroutine()
|
2024-04-13 21:45:05 +01:00
|
|
|
heapMem := float64(memStats.HeapAlloc) / (1024 * 1024)
|
|
|
|
heapMemRound := math.Round(heapMem*100) / 100
|
2024-04-08 21:39:52 +01:00
|
|
|
|
2024-04-14 19:03:13 +01:00
|
|
|
log.Debug("Performance", zap.Int("goroutine-count", goroutines), zap.Float64("heap-mem (MB)", heapMemRound))
|
2024-04-08 21:39:52 +01:00
|
|
|
}
|