Skip to content

Commit

Permalink
chore: Clean up old authentication events from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Font committed Aug 22, 2024
1 parent 51feaf0 commit b57d4da
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
const (
GitlabVersionManifestPath = "/opt/gitlab/version-manifest.txt"
PreloadEventsPeriodDays = 30

CleanAuthLogEventsPeriod = 60 * time.Hour * 24 // time to keep the auth events in the DB
)

type Config struct {
Expand Down Expand Up @@ -82,6 +84,8 @@ func NewGitLabLogStreamer(config Config) (*GitLabLogStreamer, error) {
return nil, err
}

go streamer.cleanOldEvents()

return streamer, nil
}

Expand Down Expand Up @@ -174,3 +178,15 @@ func (s *GitLabLogStreamer) handleFileEvent(event fsnotify.Event) {
}
}
}

func (s *GitLabLogStreamer) cleanOldEvents() {
// remove events older than CleanAuthLogEventsPeriod from s.db

for {
err := s.db.Where("time < ?", time.Now().Add(-CleanAuthLogEventsPeriod)).Delete(&AuthEvent{}).Error
if err != nil {
log.Error().Caller().Err(err).Msg("Error while deleting old auth events")
}
time.Sleep(1 * time.Hour)
}
}

0 comments on commit b57d4da

Please sign in to comment.