From b2719ae302567e3b0a0880ab8fd8eb5ed7c3430d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 6 Apr 2024 14:05:51 +0800 Subject: [PATCH] :sparkles: Database cleaner --- pkg/cmd/main.go | 9 +++++++++ pkg/database/migrator.go | 16 +++++++++------- pkg/services/cleaner.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 pkg/services/cleaner.go diff --git a/pkg/cmd/main.go b/pkg/cmd/main.go index 7c3bc59..d830f28 100644 --- a/pkg/cmd/main.go +++ b/pkg/cmd/main.go @@ -1,6 +1,8 @@ package main import ( + "git.solsynth.dev/hydrogen/messaging/pkg/services" + "github.com/robfig/cron/v3" "os" "os/signal" "syscall" @@ -50,6 +52,11 @@ func main() { server.NewServer() go server.Listen() + // Configure timed tasks + quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger))) + quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup) + quartz.Start() + // Messages log.Info().Msgf("Messaging v%s is started...", messaging.AppVersion) @@ -58,4 +65,6 @@ func main() { <-quit log.Info().Msgf("Messaging v%s is quitting...", messaging.AppVersion) + + quartz.Stop() } diff --git a/pkg/database/migrator.go b/pkg/database/migrator.go index 938457c..5597b65 100644 --- a/pkg/database/migrator.go +++ b/pkg/database/migrator.go @@ -5,14 +5,16 @@ import ( "gorm.io/gorm" ) +var DatabaseAutoActionRange = []any{ + &models.Account{}, + &models.Channel{}, + &models.ChannelMember{}, + &models.Message{}, + &models.Attachment{}, +} + func RunMigration(source *gorm.DB) error { - if err := source.AutoMigrate( - &models.Account{}, - &models.Channel{}, - &models.ChannelMember{}, - &models.Message{}, - &models.Attachment{}, - ); err != nil { + if err := source.AutoMigrate(DatabaseAutoActionRange...); err != nil { return err } diff --git a/pkg/services/cleaner.go b/pkg/services/cleaner.go new file mode 100644 index 0000000..1d49f0a --- /dev/null +++ b/pkg/services/cleaner.go @@ -0,0 +1,33 @@ +package services + +import ( + "git.solsynth.dev/hydrogen/messaging/pkg/database" + "git.solsynth.dev/hydrogen/messaging/pkg/models" + "github.com/rs/zerolog/log" + "time" +) + +func DoAutoDatabaseCleanup() { + deadline := time.Now().Add(60 * time.Minute) + log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...") + + // Deal soft-deletion + var count int64 + for _, model := range database.DatabaseAutoActionRange { + tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline) + if tx.Error != nil { + log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...") + } + count += tx.RowsAffected + } + + // Clean up outdated chat history + tx := database.C.Unscoped().Delete(&models.Message{}, "created_at < ?", time.Now().Add(30*24*time.Hour)) + if tx.Error != nil { + log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...") + } else { + count += tx.RowsAffected + } + + log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.") +}