From 5e73d9acd4aa960967feeb131039005fc59a1d75 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 1 Jan 2025 11:43:54 +0800 Subject: [PATCH] :recycle: Use update api instead of overhaul in background tasks --- pkg/internal/services/analyzer.go | 10 ++++------ pkg/internal/services/attachments.go | 18 +++++++----------- pkg/internal/services/uploader.go | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pkg/internal/services/analyzer.go b/pkg/internal/services/analyzer.go index de375c5..428f769 100644 --- a/pkg/internal/services/analyzer.go +++ b/pkg/internal/services/analyzer.go @@ -220,16 +220,14 @@ func AnalyzeAttachment(file models.Attachment) error { tx := database.C.Begin() - file.IsAnalyzed = true + if err := tx.Model(&file).Update("is_analyzed", true).Error; err != nil { + tx.Rollback() + return fmt.Errorf("unable to update file record: %v", err) + } linked, err := TryLinkAttachment(tx, file, file.HashCode) if linked && err != nil { return fmt.Errorf("unable to link file record: %v", err) - } else if !linked { - if err := tx.Save(&file).Error; err != nil { - tx.Rollback() - return fmt.Errorf("unable to save file record: %v", err) - } } tx.Commit() diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index 604c72f..e9834c5 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -166,19 +166,15 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er } } - prev.RefCount++ - og.RefID = &prev.ID - og.Uuid = prev.Uuid - og.Destination = prev.Destination - - if og.AccountID == prev.AccountID { - og.IsSelfRef = true - } - - if err := tx.Save(&og).Error; err != nil { + if err := tx.Model(&og).Updates(&models.Attachment{ + RefID: &prev.ID, + Uuid: prev.Uuid, + Destination: prev.Destination, + IsSelfRef: og.AccountID == prev.AccountID, + }).Error; err != nil { tx.Rollback() return true, err - } else if err = tx.Save(&prev).Error; err != nil { + } else if err = tx.Model(&prev).Update("ref_count", prev.RefCount+1).Error; err != nil { tx.Rollback() return true, err } diff --git a/pkg/internal/services/uploader.go b/pkg/internal/services/uploader.go index d482b7b..d0fff3c 100644 --- a/pkg/internal/services/uploader.go +++ b/pkg/internal/services/uploader.go @@ -48,7 +48,7 @@ func ReUploadFile(meta models.Attachment, dst int, doNotUpdate ...bool) error { cleanupDst := func() { if len(doNotUpdate) == 0 || !doNotUpdate[0] { - database.C.Save(&meta) + database.C.Model(&meta).Update("destination", dst) } if prevDst == models.AttachmentDstTemporary { return