🐛 Fix db cleaner
This commit is contained in:
parent
07d9335180
commit
af2bc29068
@ -3,9 +3,10 @@ package services
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||||
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/eko/gocache/lib/v4/cache"
|
"github.com/eko/gocache/lib/v4/cache"
|
||||||
"github.com/eko/gocache/lib/v4/marshaler"
|
"github.com/eko/gocache/lib/v4/marshaler"
|
||||||
@ -48,7 +49,11 @@ func GetAuthContext(sessionId uint) (models.AuthTicket, error) {
|
|||||||
ctx = *val.(*models.AuthTicket)
|
ctx = *val.(*models.AuthTicket)
|
||||||
} else {
|
} else {
|
||||||
ctx, err = CacheAuthContext(sessionId)
|
ctx, err = CacheAuthContext(sessionId)
|
||||||
log.Debug().Uint("session", sessionId).Msg("Created a new auth context cache")
|
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
|
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)}),
|
store.WithTags([]string{"auth-context", fmt.Sprintf("user#%d", user.ID)}),
|
||||||
)
|
)
|
||||||
|
|
||||||
return ticket, nil
|
return ticket, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func InvalidAuthCacheWithUser(userId uint) {
|
func InvalidAuthCacheWithUser(userId uint) {
|
||||||
|
@ -1,27 +1,22 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||||
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func DoAutoDatabaseCleanup() {
|
func DoAutoDatabaseCleanup() {
|
||||||
log.Debug().Msg("Now cleaning up entire database...")
|
log.Debug().Msg("Now cleaning up entire database...")
|
||||||
|
|
||||||
var count int64
|
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)
|
deadline := time.Now().Add(-30 * 24 * time.Hour)
|
||||||
seenDeadline := time.Now().Add(-7 * 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.")
|
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user