43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
|
|
"github.com/jasonlvhit/gocron"
|
|
"github.com/rs/zerolog"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func CleanupLoginsCronJob(db *bun.DB, logger *zerolog.Logger) {
|
|
err := gocron.Every(5).Minutes().Do(CleanupLogins, db, logger)
|
|
if err != nil {
|
|
return
|
|
}
|
|
<-gocron.Start()
|
|
}
|
|
|
|
func CleanupLogins(db *bun.DB, logger *zerolog.Logger) {
|
|
res, err := db.NewDelete().Table("logins").Where("expires < NOW()").Exec(context.Background())
|
|
if err != nil {
|
|
log.Printf("[CleanupLogins] Error deleting logins: %v\n", err)
|
|
}
|
|
|
|
numdeleted, _ := res.RowsAffected()
|
|
logger.Debug().Int64("logins", numdeleted).Msg("cleaned up logins from DB")
|
|
|
|
res, err = db.NewDelete().Table("logintransactions").Where("expires < NOW()").Exec(context.Background())
|
|
if err != nil {
|
|
log.Printf("[CleanupLogins] Error deleting login transactions: %v\n", err)
|
|
}
|
|
numdeleted, _ = res.RowsAffected()
|
|
logger.Debug().Int64("transactions", numdeleted).Msg("cleaned up transactions from DB")
|
|
|
|
res, err = db.NewDelete().Table("multifactor").Where("expires_at < NOW()").Where("active = false").Exec(context.Background())
|
|
if err != nil {
|
|
log.Printf("[CleanupLogins] Error deleting 2fa transactions: %v\n", err)
|
|
}
|
|
|
|
numdeleted, _ = res.RowsAffected()
|
|
logger.Debug().Int64("mfatransactions", numdeleted).Msg("cleaned up 2fa transactions from DB")
|
|
}
|