🐛 Bug fixes on cron jobs

This commit is contained in:
LittleSheep 2024-07-12 11:25:41 +08:00
parent e049b2c566
commit d405b34747
3 changed files with 9 additions and 12 deletions

View File

@ -3,18 +3,16 @@ package services
import ( import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"time"
) )
func DoAutoDatabaseCleanup() { func DoAutoDatabaseCleanup() {
deadline := time.Now().Add(60 * time.Minute) log.Debug().Msg("Now cleaning up entire database...")
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
var count int64 var count int64
for _, model := range database.AutoMaintainRange { for _, model := range database.AutoMaintainRange {
tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline) tx := database.C.Unscoped().Delete(model, "deleted_at IS NOT NULL")
if tx.Error != nil { if tx.Error != nil {
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...") log.Error().Err(tx.Error).Msg("An error occurred when running cleaning up entire database...")
} }
count += tx.RowsAffected count += tx.RowsAffected
} }

View File

@ -1,21 +1,21 @@
package services package services
import ( import (
"time"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database" "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models" "git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper"
"time"
) )
func DoAutoSignoff() { func DoAutoSignoff() {
duration := time.Duration(viper.GetInt64("security.auto_signoff_duration")) * time.Second duration := 7 * 24 * time.Hour
divider := time.Now().Add(-duration) deadline := time.Now().Add(-duration)
log.Debug().Time("before", divider).Msg("Now signing off tickets...") log.Debug().Time("before", deadline).Msg("Now signing off tickets...")
if tx := database.C. if tx := database.C.
Where("last_grant_at < ?", divider). Where("last_grant_at < ?", deadline).
Delete(&models.AuthTicket{}); tx.Error != nil { Delete(&models.AuthTicket{}); tx.Error != nil {
log.Error().Err(tx.Error).Msg("An error occurred when running auto sign off...") log.Error().Err(tx.Error).Msg("An error occurred when running auto sign off...")
} else { } else {

View File

@ -35,7 +35,6 @@ password = "gz937Zxxzfcd9SeH"
[security] [security]
cookie_domain = "localhost" cookie_domain = "localhost"
cookie_samesite = "Lax" cookie_samesite = "Lax"
auto_signoff_duration = 86400
access_token_duration = 300 access_token_duration = 300
refresh_token_duration = 2592000 refresh_token_duration = 2592000