Skip to content

Commit 8106d95

Browse files
authored
Use global lock instead of status pool for cron lock (#35507)
1 parent 1f32170 commit 8106d95

File tree

5 files changed

+18
-99
lines changed

5 files changed

+18
-99
lines changed

modules/sync/status_pool.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

modules/sync/status_pool_test.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

routers/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func InitWebInstalled(ctx context.Context) {
177177
mustInit(repo_service.InitLicenseClassifier)
178178

179179
// Finally start up the cron
180-
cron.NewContext(ctx)
180+
cron.Init(ctx)
181181
}
182182

183183
// NormalRoutes represents non install routes

services/cron/cron.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@ import (
1111

1212
"code.gitea.io/gitea/modules/graceful"
1313
"code.gitea.io/gitea/modules/process"
14-
"code.gitea.io/gitea/modules/sync"
1514
"code.gitea.io/gitea/modules/translation"
1615

1716
"github.com/go-co-op/gocron"
1817
)
1918

2019
var scheduler = gocron.NewScheduler(time.Local)
2120

22-
// Prevent duplicate running tasks.
23-
var taskStatusTable = sync.NewStatusTable()
24-
25-
// NewContext begins cron tasks
21+
// Init begins cron tasks
2622
// Each cron task is run within the shutdown context as a running server
2723
// AtShutdown the cron server is stopped
28-
func NewContext(original context.Context) {
24+
func Init(original context.Context) {
2925
defer pprof.SetGoroutineLabels(original)
3026
_, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().ShutdownContext(), "Service: Cron", process.SystemProcessType, true)
3127
initBasicTasks()

services/cron/tasks.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/models/db"
1515
system_model "code.gitea.io/gitea/models/system"
1616
user_model "code.gitea.io/gitea/models/user"
17+
"code.gitea.io/gitea/modules/globallock"
1718
"code.gitea.io/gitea/modules/graceful"
1819
"code.gitea.io/gitea/modules/log"
1920
"code.gitea.io/gitea/modules/process"
@@ -71,20 +72,30 @@ func (t *Task) Run() {
7172
}, t.config)
7273
}
7374

75+
func getCronTaskLockKey(name string) string {
76+
return "cron_task:" + name
77+
}
78+
7479
// RunWithUser will run the task incrementing the cron counter at the time with User
7580
func (t *Task) RunWithUser(doer *user_model.User, config Config) {
76-
if !taskStatusTable.StartIfNotRunning(t.Name) {
81+
locked, releaser, err := globallock.TryLock(graceful.GetManager().ShutdownContext(), getCronTaskLockKey(t.Name))
82+
if err != nil {
83+
log.Error("Failed to acquire lock for cron task %q: %v", t.Name, err)
84+
return
85+
}
86+
if !locked {
87+
log.Trace("a cron task %q is already running", t.Name)
7788
return
7889
}
90+
defer releaser()
91+
7992
t.lock.Lock()
8093
if config == nil {
8194
config = t.config
8295
}
8396
t.ExecTimes++
8497
t.lock.Unlock()
85-
defer func() {
86-
taskStatusTable.Stop(t.Name)
87-
}()
98+
8899
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
89100
defer func() {
90101
if err := recover(); err != nil {

0 commit comments

Comments
 (0)