Skip to content

Commit e1fa778

Browse files
committed
Use cron only for deleting the files need to be cleanup
1 parent 9671f2e commit e1fa778

File tree

18 files changed

+98
-241
lines changed

18 files changed

+98
-241
lines changed

models/repo/attachment.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,37 +166,34 @@ func GetAttachmentByReleaseIDFileName(ctx context.Context, releaseID int64, file
166166
}
167167

168168
// DeleteAttachments delete the given attachments and add disk files to pending deletion
169-
func DeleteAttachments(ctx context.Context, attachments []*Attachment) ([]int64, error) {
169+
func DeleteAttachments(ctx context.Context, attachments []*Attachment) error {
170170
if len(attachments) == 0 {
171-
return nil, nil
171+
return nil
172172
}
173173

174174
ids := make([]int64, 0, len(attachments))
175175
for _, a := range attachments {
176176
ids = append(ids, a.ID)
177177
}
178178

179-
return db.WithTx2(ctx, func(ctx context.Context) ([]int64, error) {
179+
return db.WithTx(ctx, func(ctx context.Context) error {
180180
// delete attachments from database
181181
if _, err := db.GetEngine(ctx).Table("attachment").In("id", ids).Delete(); err != nil {
182-
return nil, err
182+
return err
183183
}
184184

185185
// add disk files to pending deletion table as well
186-
var deletionIDs []int64
187186
for _, a := range attachments {
188187
pendingDeletion := &system_model.StoragePathDeletion{
189188
StorageName: storage.AttachmentStorageName,
190189
PathType: system_model.PathFile,
191190
RelativePath: a.RelativePath(),
192191
}
193192
if err := db.Insert(ctx, pendingDeletion); err != nil {
194-
return nil, fmt.Errorf("insert pending deletion: %w", err)
193+
return fmt.Errorf("insert pending deletion: %w", err)
195194
}
196-
197-
deletionIDs = append(deletionIDs, pendingDeletion.ID) // Collect pending deletions
198195
}
199-
return deletionIDs, nil
196+
return nil
200197
})
201198
}
202199

models/user/main_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/unittest"
10-
"code.gitea.io/gitea/modules/setting"
11-
"code.gitea.io/gitea/services/storagecleanup"
1210

1311
_ "code.gitea.io/gitea/models"
1412
_ "code.gitea.io/gitea/models/actions"
@@ -17,10 +15,5 @@ import (
1715
)
1816

1917
func TestMain(m *testing.M) {
20-
unittest.MainTest(m, &unittest.TestOptions{
21-
SetUp: func() error {
22-
setting.LoadQueueSettings()
23-
return storagecleanup.Init()
24-
},
25-
})
18+
unittest.MainTest(m)
2619
}

routers/init.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import (
5252
release_service "code.gitea.io/gitea/services/release"
5353
repo_service "code.gitea.io/gitea/services/repository"
5454
"code.gitea.io/gitea/services/repository/archiver"
55-
"code.gitea.io/gitea/services/storagecleanup"
5655
"code.gitea.io/gitea/services/task"
5756
"code.gitea.io/gitea/services/uinotification"
5857
"code.gitea.io/gitea/services/webhook"
@@ -175,7 +174,6 @@ func InitWebInstalled(ctx context.Context) {
175174
mustInitCtx(ctx, actions_service.Init)
176175

177176
mustInit(repo_service.InitLicenseClassifier)
178-
mustInit(storagecleanup.Init)
179177

180178
// Finally start up the cron
181179
cron.NewContext(ctx)

services/attachment/attachment.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"code.gitea.io/gitea/modules/storage"
1515
"code.gitea.io/gitea/modules/util"
1616
"code.gitea.io/gitea/services/context/upload"
17-
"code.gitea.io/gitea/services/storagecleanup"
1817

1918
"github.com/google/uuid"
2019
)
@@ -69,12 +68,10 @@ func DeleteAttachment(ctx context.Context, a *repo_model.Attachment) error {
6968

7069
// DeleteAttachments deletes the given attachments and optionally the associated files.
7170
func DeleteAttachments(ctx context.Context, attachments []*repo_model.Attachment) (int, error) {
72-
deletions, err := repo_model.DeleteAttachments(ctx, attachments)
71+
err := repo_model.DeleteAttachments(ctx, attachments)
7372
if err != nil {
7473
return 0, err
7574
}
7675

77-
storagecleanup.AddDeletionsToCleanQueue(ctx, deletions)
78-
79-
return len(deletions), nil
76+
return len(attachments), nil
8077
}

services/attachment/attachment_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@ import (
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
"code.gitea.io/gitea/models/unittest"
1616
user_model "code.gitea.io/gitea/models/user"
17-
"code.gitea.io/gitea/modules/setting"
1817
"code.gitea.io/gitea/modules/storage"
19-
"code.gitea.io/gitea/services/storagecleanup"
2018

2119
_ "code.gitea.io/gitea/models/actions"
2220

2321
"github.com/stretchr/testify/assert"
2422
)
2523

2624
func TestMain(m *testing.M) {
27-
unittest.MainTest(m, &unittest.TestOptions{
28-
SetUp: func() error {
29-
setting.LoadQueueSettings()
30-
return storagecleanup.Init()
31-
},
32-
})
25+
unittest.MainTest(m)
3326
}
3427

3528
func TestUploadAttachment(t *testing.T) {

services/cron/tasks_extended.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
asymkey_service "code.gitea.io/gitea/services/asymkey"
1818
repo_service "code.gitea.io/gitea/services/repository"
1919
archiver_service "code.gitea.io/gitea/services/repository/archiver"
20-
"code.gitea.io/gitea/services/storagecleanup"
20+
storage_service "code.gitea.io/gitea/services/storage"
2121
user_service "code.gitea.io/gitea/services/user"
2222
)
2323

@@ -228,9 +228,9 @@ func registerCleanStorage() {
228228
RegisterTaskFatal("cleanup_storage", &BaseConfig{
229229
Enabled: false,
230230
RunAtStart: false,
231-
Schedule: "@every 24h",
231+
Schedule: "@every 1m", // Run every minute to check for deletions
232232
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
233-
return storagecleanup.ScanToBeDeletedFilesOrDir(ctx)
233+
return storage_service.ScanToBeDeletedFilesOrDir(ctx)
234234
})
235235
}
236236

services/issue/comments.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/timeutil"
1919
git_service "code.gitea.io/gitea/services/git"
2020
notify_service "code.gitea.io/gitea/services/notify"
21-
"code.gitea.io/gitea/services/storagecleanup"
2221
)
2322

2423
// CreateRefComment creates a commit reference comment to issue.
@@ -132,34 +131,31 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion
132131
}
133132

134133
// deleteComment deletes the comment
135-
func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAttachments bool) ([]int64, error) {
136-
return db.WithTx2(ctx, func(ctx context.Context) ([]int64, error) {
134+
func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAttachments bool) error {
135+
return db.WithTx(ctx, func(ctx context.Context) error {
137136
if removeAttachments {
138137
// load attachments before deleting the comment
139138
if err := comment.LoadAttachments(ctx); err != nil {
140-
return nil, err
139+
return err
141140
}
142141
}
143142

144143
if err := issues_model.DeleteComment(ctx, comment); err != nil {
145-
return nil, err
144+
return err
146145
}
147146

148147
if removeAttachments {
149148
return repo_model.DeleteAttachments(ctx, comment.Attachments)
150149
}
151-
return nil, nil
150+
return nil
152151
})
153152
}
154153

155154
func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) error {
156-
deletions, err := deleteComment(ctx, comment, true)
157-
if err != nil {
155+
if err := deleteComment(ctx, comment, true); err != nil {
158156
return err
159157
}
160158

161-
storagecleanup.AddDeletionsToCleanQueue(ctx, deletions)
162-
163159
notify_service.DeleteComment(ctx, doer, comment)
164160

165161
return nil

services/issue/issue.go

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/log"
2020
notify_service "code.gitea.io/gitea/services/notify"
21-
"code.gitea.io/gitea/services/storagecleanup"
2221
)
2322

2423
// NewIssue creates new issue with labels for repository.
@@ -189,13 +188,10 @@ func DeleteIssue(ctx context.Context, doer *user_model.User, gitRepo *git.Reposi
189188
}
190189

191190
// delete entries in database
192-
toBeCleanedDeletions, err := deleteIssue(ctx, issue, true)
193-
if err != nil {
191+
if err := deleteIssue(ctx, issue, true); err != nil {
194192
return err
195193
}
196194

197-
storagecleanup.AddDeletionsToCleanQueue(ctx, toBeCleanedDeletions)
198-
199195
// delete pull request related git data
200196
if issue.IsPull && gitRepo != nil {
201197
if err := gitRepo.RemoveReference(issue.PullRequest.GetGitHeadRefName()); err != nil {
@@ -258,37 +254,36 @@ func GetRefEndNamesAndURLs(issues []*issues_model.Issue, repoLink string) (map[i
258254
}
259255

260256
// deleteIssue deletes the issue
261-
func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) ([]int64, error) {
262-
return db.WithTx2(ctx, func(ctx context.Context) ([]int64, error) {
263-
toBeCleanedDeletions := make([]int64, 0)
257+
func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachments bool) error {
258+
return db.WithTx(ctx, func(ctx context.Context) error {
264259
if _, err := db.GetEngine(ctx).ID(issue.ID).NoAutoCondition().Delete(issue); err != nil {
265-
return nil, err
260+
return err
266261
}
267262

268263
// update the total issue numbers
269264
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, false); err != nil {
270-
return nil, err
265+
return err
271266
}
272267
// if the issue is closed, update the closed issue numbers
273268
if issue.IsClosed {
274269
if err := repo_model.UpdateRepoIssueNumbers(ctx, issue.RepoID, issue.IsPull, true); err != nil {
275-
return nil, err
270+
return err
276271
}
277272
}
278273

279274
if err := issues_model.UpdateMilestoneCounters(ctx, issue.MilestoneID); err != nil {
280-
return nil, fmt.Errorf("error updating counters for milestone id %d: %w",
275+
return fmt.Errorf("error updating counters for milestone id %d: %w",
281276
issue.MilestoneID, err)
282277
}
283278

284279
if err := activities_model.DeleteIssueActions(ctx, issue.RepoID, issue.ID, issue.Index); err != nil {
285-
return nil, err
280+
return err
286281
}
287282

288283
if deleteAttachments {
289284
// find attachments related to this issue and remove them
290285
if err := issue.LoadAttachments(ctx); err != nil {
291-
return nil, err
286+
return err
292287
}
293288
}
294289

@@ -311,82 +306,66 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue, deleteAttachmen
311306
&issues_model.Comment{DependentIssueID: issue.ID},
312307
&issues_model.IssuePin{IssueID: issue.ID},
313308
); err != nil {
314-
return nil, err
309+
return err
315310
}
316311

317312
for _, comment := range issue.Comments {
318-
deletions, err := deleteComment(ctx, comment, deleteAttachments)
319-
if err != nil {
320-
return nil, fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err)
321-
}
322-
if deleteAttachments {
323-
toBeCleanedDeletions = append(toBeCleanedDeletions, deletions...)
313+
if err := deleteComment(ctx, comment, deleteAttachments); err != nil {
314+
return fmt.Errorf("deleteComment [comment_id: %d]: %w", comment.ID, err)
324315
}
325316
}
326317

327318
if deleteAttachments {
328319
// delete issue attachments
329320
if err := issue.LoadAttachments(ctx); err != nil {
330-
return nil, err
321+
return err
331322
}
332-
deletions, err := repo_model.DeleteAttachments(ctx, issue.Attachments)
333-
if err != nil {
334-
return nil, err
323+
if err := repo_model.DeleteAttachments(ctx, issue.Attachments); err != nil {
324+
return err
335325
}
336-
toBeCleanedDeletions = append(toBeCleanedDeletions, deletions...)
337326
}
338-
return toBeCleanedDeletions, nil
327+
return nil
339328
})
340329
}
341330

342331
// DeleteOrphanedIssues delete issues without a repo
343332
func DeleteOrphanedIssues(ctx context.Context) error {
344-
toBeCleanedDeletions := make([]int64, 0)
345-
if err := db.WithTx(ctx, func(ctx context.Context) error {
333+
return db.WithTx(ctx, func(ctx context.Context) error {
346334
repoIDs, err := issues_model.GetOrphanedIssueRepoIDs(ctx)
347335
if err != nil {
348336
return err
349337
}
350338
for i := range repoIDs {
351-
deletions, err := DeleteIssuesByRepoID(ctx, repoIDs[i], true)
352-
if err != nil {
339+
if err := DeleteIssuesByRepoID(ctx, repoIDs[i], true); err != nil {
353340
return err
354341
}
355-
toBeCleanedDeletions = append(toBeCleanedDeletions, deletions...)
356342
}
357343
return nil
358-
}); err != nil {
359-
return err
360-
}
361-
storagecleanup.AddDeletionsToCleanQueue(ctx, toBeCleanedDeletions)
362-
return nil
344+
})
363345
}
364346

365347
// DeleteIssuesByRepoID deletes issues by repositories id
366-
func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) ([]int64, error) {
367-
toBeCleanedDeletions := make([]int64, 0)
348+
func DeleteIssuesByRepoID(ctx context.Context, repoID int64, deleteAttachments bool) error {
368349
for {
369350
issues := make([]*issues_model.Issue, 0, db.DefaultMaxInSize)
370351
if err := db.GetEngine(ctx).
371352
Where("repo_id = ?", repoID).
372353
OrderBy("id").
373354
Limit(db.DefaultMaxInSize).
374355
Find(&issues); err != nil {
375-
return nil, err
356+
return err
376357
}
377358

378359
if len(issues) == 0 {
379360
break
380361
}
381362

382363
for _, issue := range issues {
383-
deletions, err := deleteIssue(ctx, issue, deleteAttachments)
384-
if err != nil {
385-
return nil, fmt.Errorf("deleteIssue [issue_id: %d]: %w", issue.ID, err)
364+
if err := deleteIssue(ctx, issue, deleteAttachments); err != nil {
365+
return fmt.Errorf("deleteIssue [issue_id: %d]: %w", issue.ID, err)
386366
}
387-
toBeCleanedDeletions = append(toBeCleanedDeletions, deletions...)
388367
}
389368
}
390369

391-
return toBeCleanedDeletions, nil
370+
return nil
392371
}

0 commit comments

Comments
 (0)