Skip to content

Commit

Permalink
Implement database cleanup function to remove entries older than 56 days
Browse files Browse the repository at this point in the history
  • Loading branch information
abaldeweg authored Feb 10, 2025
1 parent d10bd07 commit 1274682
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions logs_import/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"encoding/json"
"time"

"github.com/abaldeweg/warehouse-server/logs_import/entity"
"github.com/spf13/viper"
Expand Down Expand Up @@ -59,3 +60,14 @@ func (handler *DBHandler) Add(data entity.LogEntry) error {
_, err = handler.collection.InsertOne(context.TODO(), bsonData)
return err
}

// Cleanup removes all database entries older than 56 days.
func (handler *DBHandler) Cleanup() error {
filter := bson.M{
"time": bson.M{
"$lt": time.Now().AddDate(0, 0, -56).UTC().Format(time.RFC3339),
},
}
_, err := handler.collection.DeleteMany(context.TODO(), filter)
return err
}
12 changes: 12 additions & 0 deletions logs_import/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"log"
"time"

"github.com/abaldeweg/warehouse-server/framework/config"
"github.com/abaldeweg/warehouse-server/logs_import/db"
"github.com/abaldeweg/warehouse-server/logs_import/importer"
"github.com/spf13/viper"
)
Expand All @@ -14,6 +16,16 @@ func main() {
viper.SetDefault("MONGODB_URI", "mongodb://localhost:27017")
viper.SetDefault("blocklist", []string{})

dbHandler, err := db.NewDBHandler()
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer dbHandler.Close()

if err := dbHandler.Cleanup(); err != nil {
log.Fatalf("Failed to cleanup old entries: %v", err)
}

go importer.Import()

for {
Expand Down

0 comments on commit 1274682

Please sign in to comment.