Skip to content

Commit

Permalink
♻️ Split mark and delete file
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleSheep2Code committed Aug 18, 2024
1 parent 922a76e commit 98cf753
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
6 changes: 4 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 25 additions & 20 deletions pkg/internal/services/recycler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"github.com/samber/lo"
"gorm.io/gorm/clause"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -36,7 +35,7 @@ func StartConsumeDeletionTask() {
}
}

func RunScheduleDeletionTask() {
func RunMarkDeletionTask() {
var pools []models.AttachmentPool
if err := database.C.Find(&pools).Error; err != nil {
return
Expand All @@ -51,28 +50,34 @@ func RunScheduleDeletionTask() {

for _, pool := range pendingPools {
lifecycle := fmt.Sprintf("%d seconds", *pool.Config.Data().ExistLifecycle)
var attachments []models.Attachment
if err := database.C.Where("pool_id = ? AND created_at < NOW() - INTERVAL ?", pool.ID, lifecycle).Find(&attachments).Error; err != nil {
continue
}
tx := database.C.
Where("pool_id = ? AND created_at < NOW() - INTERVAL ?", pool.ID, lifecycle).
Updates(&models.Attachment{CleanedAt: lo.ToPtr(time.Now())})
log.Info().
Str("pool", pool.Alias).
Int("count", len(attachments)).
Msg("Deleting attachments due to pool's lifecycle configuration...")
for idx, attachment := range attachments {
if err := DeleteFile(attachment); err != nil {
log.Error().
Str("pool", pool.Alias).
Uint("id", attachment.ID).
Msg("An error occurred when deleting attachment due to pool's lifecycle configuration...")
} else {
attachments[idx].CleanedAt = lo.ToPtr(time.Now())
}
Int64("count", tx.RowsAffected).
Err(tx.Error).
Msg("Marking attachments as clean needed due to pool's lifecycle configuration...")
}
}

func RunScheduleDeletionTask() {
var attachments []models.Attachment
if err := database.C.Where("cleaned_at IS NOT NULL").Find(&attachments).Error; err != nil {
return
}

for idx, attachment := range attachments {
if err := DeleteFile(attachment); err != nil {
log.Error().
Uint("id", attachment.ID).
Msg("An error occurred when deleting marked clean up attachments...")
} else {
attachments[idx].CleanedAt = lo.ToPtr(time.Now())
}
database.C.Clauses(clause.OnConflict{
UpdateAll: true,
}).CreateInBatches(attachments, 1000)
}

database.C.Where("cleaned_at IS NOT NULL").Delete(&models.Attachment{})
}

func DeleteFile(meta models.Attachment) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
log.Error().Err(err).Msg("An error occurred when registering service to dealer...")
}

// Setup some workers
// Set up some workers
for idx := 0; idx < viper.GetInt("workers.files_deletion"); idx++ {
go services.StartConsumeDeletionTask()
}
Expand All @@ -59,6 +59,7 @@ func main() {
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
quartz.AddFunc("@every 60m", services.RunMarkDeletionTask)
quartz.AddFunc("@midnight", services.RunScheduleDeletionTask)
quartz.Start()

Expand All @@ -74,7 +75,7 @@ func main() {
log.Info().Msgf("Paperclip v%s is started...", pkg.AppVersion)

services.ScanUnanalyzedFileFromDatabase()
services.RunScheduleDeletionTask()
services.RunMarkDeletionTask()

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
Expand Down

0 comments on commit 98cf753

Please sign in to comment.