Add RemoveOutdatedServices

This commit is contained in:
Fred Boniface 2024-04-14 21:21:35 +01:00
parent f97bea78eb
commit c9f894bbe6
2 changed files with 28 additions and 0 deletions

View File

@ -47,7 +47,19 @@ func runCifFullDownload(cfg *helpers.Configuration) error {
log.Warn("CIF Data updated, but metadata write failed") log.Warn("CIF Data updated, but metadata write failed")
} }
// Set parsed to nil to encourage garbage collection
parsed = nil parsed = nil
// Clear out of date schedules
cutoff := time.Now().Add(-time.Hour * 24 * 7)
log.Debug("Attempting to remove outdated services", zap.Time("scheduleEnd before", cutoff))
count, err := dbAccess.RemoveOutdatedServices(cutoff)
if err != nil {
log.Warn("Out of date services not removed", zap.Error(err))
} else {
log.Info("Out of date services removed", zap.Int64("removal count", count))
}
return nil return nil
} }

View File

@ -138,3 +138,19 @@ func CreateCifEntries(schedules []database.Service) error {
return nil return nil
} }
// Removes any schedules which ended before 'cutoff'
func RemoveOutdatedServices(cutoff time.Time) (count int64, err error) {
// Define filter
filter := bson.M{"scheduleEndDate": bson.M{"$lt": cutoff}}
collection := MongoClient.Database(databaseName).Collection(timetableCollection)
res, err := collection.DeleteMany(context.Background(), filter)
if err != nil {
return // Automatically returns named values
}
count = res.DeletedCount
return // Automatically returns names values
}