Algorithmus zum löscheb alter Datenbankeinträge hinzugefügt
This commit is contained in:
parent
1990ae440d
commit
4c409224b0
2 changed files with 17 additions and 0 deletions
|
@ -15,6 +15,7 @@ services:
|
|||
- API_BASE_URL=http://db-rest:3000
|
||||
- MAX_RESULTS=200
|
||||
- DURATION=240
|
||||
- DELETE_AFTER_MINUTES=30
|
||||
- BUS=false
|
||||
- FERRY=false
|
||||
- TRAM=false
|
||||
|
|
16
main.go
16
main.go
|
@ -63,6 +63,10 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatalf("Ungültiger Wert für TAXI: %v", err)
|
||||
}
|
||||
deleteAfter, err := strconv.Atoi(os.Getenv("DELETE_AFTER_MINUTES"))
|
||||
if err != nil {
|
||||
log.Fatalf("Ungültiger Wert für DELETE_AFTER_MINUTES: %v", err)
|
||||
}
|
||||
stationIDs := strings.Split(os.Getenv("STATION_IDS"), ",")
|
||||
|
||||
db, err := sql.Open("mysql", dbDSN)
|
||||
|
@ -78,6 +82,7 @@ func main() {
|
|||
savePosition(db, dep)
|
||||
}
|
||||
}
|
||||
deleteOldEntries(db, deleteAfter)
|
||||
time.Sleep(1 * time.Minute)
|
||||
}
|
||||
}
|
||||
|
@ -146,3 +151,14 @@ func savePosition(db *sql.DB, dep Departure) {
|
|||
log.Printf("Fehler bei der Überprüfung des existierenden Eintrags: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteOldEntries(db *sql.DB, deleteAfterMinutes int) {
|
||||
deleteTime := time.Now().Add(time.Duration(-deleteAfterMinutes) * time.Minute)
|
||||
result, err := db.Exec("DELETE FROM trips WHERE timestamp < ?", deleteTime)
|
||||
if err != nil {
|
||||
log.Printf("Fehler beim Löschen alter Einträge: %v\n", err)
|
||||
return
|
||||
}
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
log.Printf("%d alte Einträge gelöscht\n", rowsAffected)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue