timetable-mgr/src/cif/check.go

64 lines
1.7 KiB
Go
Raw Normal View History

2024-03-26 16:04:42 +00:00
package cif
import (
"time"
"git.fjla.uk/owlboard/timetable-mgr/dbAccess"
"git.fjla.uk/owlboard/timetable-mgr/helpers"
"git.fjla.uk/owlboard/timetable-mgr/log"
"go.uber.org/zap"
)
2024-03-28 22:47:08 +00:00
// Break this down in to smaller, simpler functions
func CifCheck(cfg *helpers.Configuration) error {
log.Msg.Debug("Checking age of CIF Data")
metadata, err := dbAccess.GetCifMetadata()
if err != nil {
2024-03-28 22:47:08 +00:00
log.Msg.Error("Unable to fetch CifMetadata", zap.Error(err))
return err
}
if metadata == nil {
log.Msg.Info("No metadata found for last CIF Update, recreating timetable")
newMeta, err := runUpdate("full", nil)
if err != nil {
log.Msg.Error("CIF Update failed", zap.Error(err))
return err
}
ok := dbAccess.PutCifMetadata(*newMeta)
if !ok {
log.Msg.Warn("CIF Update Successful but metadata update failed")
return nil
}
}
london, _ := time.LoadLocation("Europe/London")
2024-03-28 22:47:08 +00:00
londonTimeNow := time.Now().In(london)
day := 12 * time.Hour
updateThreshold := londonTimeNow.Add(-day)
availableHour := 6
2024-03-28 22:47:08 +00:00
if londonTimeNow.Hour() >= availableHour {
if metadata.LastUpdate.Before(updateThreshold) || metadata.LastUpdate.Equal(updateThreshold) {
newMeta, err := runUpdate("full", metadata)
if err != nil {
log.Msg.Error("CIF Update failed", zap.Error(err))
return err
}
if newMeta == nil {
log.Msg.Info("CIF Update requirements not met, will retry")
return nil
}
ok := dbAccess.PutCifMetadata(*newMeta)
if !ok {
log.Msg.Warn("CIF Update Successful but metadata update failed")
return nil
}
return nil
}
}
2024-03-28 22:47:08 +00:00
log.Msg.Info("CIF Data does not require updating at this time", zap.Time("Last Update", metadata.LastUpdate))
return nil
}