diff --git a/internal/bootstrap/app_bootstrap.go b/internal/bootstrap/app_bootstrap.go index 2cb7e97..498b41c 100644 --- a/internal/bootstrap/app_bootstrap.go +++ b/internal/bootstrap/app_bootstrap.go @@ -2,6 +2,7 @@ package bootstrap import ( "bytes" + "context" "encoding/json" "fmt" "net/http" @@ -13,11 +14,13 @@ import ( "tinyauth/internal/config" "tinyauth/internal/controller" "tinyauth/internal/middleware" + "tinyauth/internal/model" "tinyauth/internal/service" "tinyauth/internal/utils" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" + "gorm.io/gorm" ) type Controller interface { @@ -277,6 +280,10 @@ func (app *BootstrapApp) Setup() error { go app.heartbeat() } + // Start DB cleanup routine + log.Debug().Msg("Starting database cleanup routine") + go app.dbCleanup(database) + // Start server address := fmt.Sprintf("%s:%d", app.config.Address, app.config.Port) log.Info().Msgf("Starting server on %s", address) @@ -338,3 +345,17 @@ func (app *BootstrapApp) heartbeat() { } } } + +func (app *BootstrapApp) dbCleanup(db *gorm.DB) { + ticker := time.NewTicker(time.Duration(30) * time.Minute) + defer ticker.Stop() + ctx := context.Background() + + for ; true; <-ticker.C { + log.Debug().Msg("Cleaning up old database sessions") + _, err := gorm.G[model.Session](db).Where("expiry < ?", time.Now().UnixMilli()).Delete(ctx) + if err != nil { + log.Error().Err(err).Msg("Failed to cleanup old sessions") + } + } +}