0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-12-09 00:21:31 +01:00

Merge 66b49d4930e7c437c99ed0ccb464068e19a5f4a4 into c287a8cdb589172bbba8969357a671dabc6596bd

This commit is contained in:
Lunny Xiao 2025-12-06 14:39:30 -05:00 committed by GitHub
commit aa693a4f96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 94 additions and 2 deletions

View File

@ -0,0 +1,42 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package gitrepo
import (
"context"
"os"
"path/filepath"
"time"
)
var lockFiles = []string{
"config.lock",
"objects/info/commit-graphs/commit-graph-chain.lock",
}
// CleanupRepo cleans up the repository by removing unnecessary lock files.
func CleanupRepo(ctx context.Context, repo Repository) error {
return CleanFixedFileLocks(ctx, repo, time.Now().Add(-24*time.Hour))
}
// CleanFixedFileLocks removes lock files that haven't been modified since the last update.
func CleanFixedFileLocks(ctx context.Context, repo Repository, lastUpdated time.Time) error {
for _, lockFile := range lockFiles {
p := filepath.Join(repoPath(repo), lockFile)
fInfo, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
if fInfo.ModTime().Before(lastUpdated) {
if err := os.Remove(p); err != nil {
return err
}
}
}
return nil
}

View File

@ -3091,6 +3091,7 @@ dashboard.sync_branch.started = Branches Sync started
dashboard.sync_tag.started = Tags Sync started
dashboard.rebuild_issue_indexer = Rebuild issue indexer
dashboard.sync_repo_licenses = Sync repo licenses
dashboard.cleanup_repo_lock_files = Clean up repository lock files
users.user_manage_panel = User Account Management
users.new_account = Create User Account

View File

@ -224,6 +224,16 @@ func registerRebuildIssueIndexer() {
})
}
func registerCleanupRepoLockFiles() {
RegisterTaskFatal("cleanup_repo_lock_files", &BaseConfig{
Enabled: false,
RunAtStart: false,
Schedule: "@every 24h",
}, func(ctx context.Context, _ *user_model.User, config Config) error {
return repo_service.CleanupRepo(ctx)
})
}
func initExtendedTasks() {
registerDeleteInactiveUsers()
registerDeleteRepositoryArchives()
@ -239,4 +249,5 @@ func initExtendedTasks() {
registerDeleteOldSystemNotices()
registerGCLFS()
registerRebuildIssueIndexer()
registerCleanupRepoLockFiles()
}

View File

@ -0,0 +1,38 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"context"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log"
"xorm.io/builder"
)
func CleanupRepo(ctx context.Context) error {
log.Trace("Doing: CleanupRepo")
if err := db.Iterate(
ctx,
builder.Eq{"is_empty": false},
func(ctx context.Context, repo *repo_model.Repository) error {
select {
case <-ctx.Done():
return db.ErrCancelledf("before cleanup repo lock files for %s", repo.FullName())
default:
}
return gitrepo.CleanupRepo(ctx, repo)
},
); err != nil {
log.Trace("Error: CleanupRepo: %v", err)
return err
}
log.Trace("Finished: CleanupRepo")
return nil
}

View File

@ -304,11 +304,11 @@ func TestAPICron(t *testing.T) {
AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
assert.Equal(t, "29", resp.Header().Get("X-Total-Count"))
assert.Equal(t, "30", resp.Header().Get("X-Total-Count"))
var crons []api.Cron
DecodeJSON(t, resp, &crons)
assert.Len(t, crons, 29)
assert.Len(t, crons, 30)
})
t.Run("Execute", func(t *testing.T) {