2023-06-16 13:16:47 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2024-05-04 17:06:01 +02:00
|
|
|
|
|
|
|
"github.com/jasonlvhit/gocron"
|
2024-09-30 13:19:01 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/uptrace/bun"
|
2023-06-16 13:16:47 +02:00
|
|
|
)
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func CleanupLoginsCronJob(db *bun.DB, logger *zerolog.Logger) {
|
|
|
|
err := gocron.Every(5).Minutes().Do(CleanupLogins, db, logger)
|
2023-06-16 13:16:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
<-gocron.Start()
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func CleanupLogins(db *bun.DB, logger *zerolog.Logger) {
|
|
|
|
res, err := db.NewDelete().Table("logins").Where("expires < NOW()").Exec(context.Background())
|
2023-06-16 13:16:47 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[CleanupLogins] Error deleting logins: %v\n", err)
|
|
|
|
}
|
2024-05-04 17:06:01 +02:00
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
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())
|
2024-05-04 17:06:01 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[CleanupLogins] Error deleting login transactions: %v\n", err)
|
|
|
|
}
|
2024-09-30 13:19:01 +02:00
|
|
|
numdeleted, _ = res.RowsAffected()
|
|
|
|
logger.Debug().Int64("transactions", numdeleted).Msg("cleaned up transactions from DB")
|
2024-05-04 17:06:01 +02:00
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
res, err = db.NewDelete().Table("multifactor").Where("expires_at < NOW()").Where("active = false").Exec(context.Background())
|
2024-05-04 17:06:01 +02:00
|
|
|
if err != nil {
|
2024-09-30 13:19:01 +02:00
|
|
|
log.Printf("[CleanupLogins] Error deleting 2fa transactions: %v\n", err)
|
2024-05-04 17:06:01 +02:00
|
|
|
}
|
2024-09-30 13:19:01 +02:00
|
|
|
|
|
|
|
numdeleted, _ = res.RowsAffected()
|
|
|
|
logger.Debug().Int64("mfatransactions", numdeleted).Msg("cleaned up 2fa transactions from DB")
|
2023-06-16 13:16:47 +02:00
|
|
|
}
|