diff --git a/modules/gitrepo/cleanup.go b/modules/gitrepo/cleanup.go index 3b8c62dcaf..90fd61091e 100644 --- a/modules/gitrepo/cleanup.go +++ b/modules/gitrepo/cleanup.go @@ -17,7 +17,7 @@ var lockFiles = []string{ // CleanupRepo cleans up the repository by removing unnecessary lock files. func CleanupRepo(ctx context.Context, repo Repository) error { - return CleanFixedFileLocks(ctx, repo, time.Now()) + return CleanFixedFileLocks(ctx, repo, time.Now().Add(-24*time.Hour)) } // CleanFixedFileLocks removes lock files that haven't been modified since the last update. diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go index 3747111984..a2cdc1b4c6 100644 --- a/services/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -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() } diff --git a/services/repository/cleanup.go b/services/repository/cleanup.go new file mode 100644 index 0000000000..26abbee408 --- /dev/null +++ b/services/repository/cleanup.go @@ -0,0 +1,36 @@ +// 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" +) + +func CleanupRepo(ctx context.Context) error { + log.Trace("Doing: CleanupRepo") + + if err := db.Iterate( + ctx, + nil, + 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 +}