diff --git a/app.go b/app.go index 28a6c0f..4b74cd6 100644 --- a/app.go +++ b/app.go @@ -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 { @@ -82,6 +84,8 @@ func NewGitLabLogStreamer(config Config) (*GitLabLogStreamer, error) { return nil, err } + go streamer.cleanOldEvents() + return streamer, nil } @@ -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) + } +}