🐛 Fix db cleaner
This commit is contained in:
parent
07d9335180
commit
af2bc29068
@ -3,9 +3,10 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||
"time"
|
||||
|
||||
"github.com/eko/gocache/lib/v4/cache"
|
||||
"github.com/eko/gocache/lib/v4/marshaler"
|
||||
@ -48,8 +49,12 @@ func GetAuthContext(sessionId uint) (models.AuthTicket, error) {
|
||||
ctx = *val.(*models.AuthTicket)
|
||||
} else {
|
||||
ctx, err = CacheAuthContext(sessionId)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Unable to cache auth context")
|
||||
} else {
|
||||
log.Debug().Uint("session", sessionId).Msg("Created a new auth context cache")
|
||||
}
|
||||
}
|
||||
|
||||
return ctx, err
|
||||
}
|
||||
@ -97,7 +102,7 @@ func CacheAuthContext(sessionId uint) (models.AuthTicket, error) {
|
||||
store.WithTags([]string{"auth-context", fmt.Sprintf("user#%d", user.ID)}),
|
||||
)
|
||||
|
||||
return ticket, nil
|
||||
return ticket, err
|
||||
}
|
||||
|
||||
func InvalidAuthCacheWithUser(userId uint) {
|
||||
|
@ -1,27 +1,22 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func DoAutoDatabaseCleanup() {
|
||||
log.Debug().Msg("Now cleaning up entire database...")
|
||||
|
||||
var count int64
|
||||
for _, model := range database.AutoMaintainRange {
|
||||
tx := database.C.Unscoped().Delete(model, "deleted_at IS NOT NULL")
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("An error occurred when running cleaning up entire database...")
|
||||
}
|
||||
count += tx.RowsAffected
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(-30 * 24 * time.Hour)
|
||||
seenDeadline := time.Now().Add(-7 * 24 * time.Hour)
|
||||
database.C.Unscoped().Where("created_at <= ? OR read_at <= ?", deadline, seenDeadline).Delete(&models.Notification{})
|
||||
tx := database.C.Unscoped().Where("created_at <= ? OR read_at <= ?", deadline, seenDeadline).Delete(&models.Notification{})
|
||||
count += tx.RowsAffected
|
||||
|
||||
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user