@@ -151,24 +151,31 @@ func cleanAttachments(ctx context.Context, attachmentIDs []int64) []int64 {
151151// ScanToBeDeletedAttachments scans for attachments that are marked as to be deleted and send to
152152// clean queue
153153func ScanToBeDeletedAttachments (ctx context.Context ) error {
154- attachments := make ([]* repo_model. Attachment , 0 , 10 )
154+ attachmentIDs := make ([]int64 , 0 , 100 )
155155 lastID := int64 (0 )
156156 for {
157157 if err := db .GetEngine (ctx ).
158+ Select ("id" ).
158159 // use the status and id index to speed up the query
159160 Where ("status = ? AND id > ?" , db .FileStatusToBeDeleted , lastID ).
160161 Asc ("id" ).
161162 Limit (100 ).
162- Find (& attachments ); err != nil {
163+ Find (& attachmentIDs ); err != nil {
163164 return fmt .Errorf ("scan to-be-deleted attachments: %w" , err )
164165 }
165166
166- if len (attachments ) == 0 {
167+ if len (attachmentIDs ) == 0 {
167168 log .Trace ("No more attachments to be deleted" )
168169 break
169170 }
170- AddAttachmentsToCleanQueue (ctx , attachments )
171- lastID = attachments [len (attachments )- 1 ].ID
171+ for _ , id := range attachmentIDs {
172+ if err := cleanQueue .Push (id ); err != nil {
173+ log .Error ("Failed to push attachment ID %d to clean queue: %v" , id , err )
174+ }
175+ }
176+
177+ lastID = attachmentIDs [len (attachmentIDs )- 1 ]
178+ attachmentIDs = attachmentIDs [0 :0 ]
172179 }
173180
174181 return nil
0 commit comments