11// Copyright 2014 The Gogs Authors. All rights reserved.
2+ // Copyright 2019 The Gitea Authors. All rights reserved.
23// Use of this source code is governed by a MIT-style
34// license that can be found in the LICENSE file.
45
@@ -7,85 +8,112 @@ package cron
78import (
89 "time"
910
10- "github.com/gogits/cron"
11-
1211 "code.gitea.io/gitea/models"
1312 "code.gitea.io/gitea/modules/log"
1413 "code.gitea.io/gitea/modules/setting"
14+ "code.gitea.io/gitea/modules/sync"
15+
16+ "github.com/gogs/cron"
17+ )
18+
19+ const (
20+ mirrorUpdate = "mirror_update"
21+ gitFsck = "git_fsck"
22+ checkRepos = "check_repos"
23+ archiveCleanup = "archive_cleanup"
24+ syncExternalUsers = "sync_external_users"
25+ deletedBranchesCleanup = "deleted_branches_cleanup"
1526)
1627
1728var c = cron .New ()
1829
30+ // Prevent duplicate running tasks.
31+ var taskStatusTable = sync .NewStatusTable ()
32+
33+ // Func defines a cron function body
34+ type Func func ()
35+
36+ // WithUnique wrap a cron func with an unique running check
37+ func WithUnique (name string , body Func ) Func {
38+ return func () {
39+ if ! taskStatusTable .StartIfNotRunning (name ) {
40+ return
41+ }
42+ defer taskStatusTable .Stop (name )
43+ body ()
44+ }
45+ }
46+
1947// NewContext begins cron tasks
2048func NewContext () {
2149 var (
2250 entry * cron.Entry
2351 err error
2452 )
2553 if setting .Cron .UpdateMirror .Enabled {
26- entry , err = c .AddFunc ("Update mirrors" , setting .Cron .UpdateMirror .Schedule , models .MirrorUpdate )
54+ entry , err = c .AddFunc ("Update mirrors" , setting .Cron .UpdateMirror .Schedule , WithUnique ( mirrorUpdate , models .MirrorUpdate ) )
2755 if err != nil {
2856 log .Fatal ("Cron[Update mirrors]: %v" , err )
2957 }
3058 if setting .Cron .UpdateMirror .RunAtStart {
3159 entry .Prev = time .Now ()
3260 entry .ExecTimes ++
33- go models .MirrorUpdate ()
61+ go WithUnique ( mirrorUpdate , models .MirrorUpdate ) ()
3462 }
3563 }
3664 if setting .Cron .RepoHealthCheck .Enabled {
37- entry , err = c .AddFunc ("Repository health check" , setting .Cron .RepoHealthCheck .Schedule , models .GitFsck )
65+ entry , err = c .AddFunc ("Repository health check" , setting .Cron .RepoHealthCheck .Schedule , WithUnique ( gitFsck , models .GitFsck ) )
3866 if err != nil {
3967 log .Fatal ("Cron[Repository health check]: %v" , err )
4068 }
4169 if setting .Cron .RepoHealthCheck .RunAtStart {
4270 entry .Prev = time .Now ()
4371 entry .ExecTimes ++
44- go models .GitFsck ()
72+ go WithUnique ( gitFsck , models .GitFsck ) ()
4573 }
4674 }
4775 if setting .Cron .CheckRepoStats .Enabled {
48- entry , err = c .AddFunc ("Check repository statistics" , setting .Cron .CheckRepoStats .Schedule , models .CheckRepoStats )
76+ entry , err = c .AddFunc ("Check repository statistics" , setting .Cron .CheckRepoStats .Schedule , WithUnique ( checkRepos , models .CheckRepoStats ) )
4977 if err != nil {
5078 log .Fatal ("Cron[Check repository statistics]: %v" , err )
5179 }
5280 if setting .Cron .CheckRepoStats .RunAtStart {
5381 entry .Prev = time .Now ()
5482 entry .ExecTimes ++
55- go models .CheckRepoStats ()
83+ go WithUnique ( checkRepos , models .CheckRepoStats ) ()
5684 }
5785 }
5886 if setting .Cron .ArchiveCleanup .Enabled {
59- entry , err = c .AddFunc ("Clean up old repository archives" , setting .Cron .ArchiveCleanup .Schedule , models .DeleteOldRepositoryArchives )
87+ entry , err = c .AddFunc ("Clean up old repository archives" , setting .Cron .ArchiveCleanup .Schedule , WithUnique ( archiveCleanup , models .DeleteOldRepositoryArchives ) )
6088 if err != nil {
6189 log .Fatal ("Cron[Clean up old repository archives]: %v" , err )
6290 }
6391 if setting .Cron .ArchiveCleanup .RunAtStart {
6492 entry .Prev = time .Now ()
6593 entry .ExecTimes ++
66- go models .DeleteOldRepositoryArchives ()
94+ go WithUnique ( archiveCleanup , models .DeleteOldRepositoryArchives ) ()
6795 }
6896 }
6997 if setting .Cron .SyncExternalUsers .Enabled {
70- entry , err = c .AddFunc ("Synchronize external users" , setting .Cron .SyncExternalUsers .Schedule , models .SyncExternalUsers )
98+ entry , err = c .AddFunc ("Synchronize external users" , setting .Cron .SyncExternalUsers .Schedule , WithUnique ( syncExternalUsers , models .SyncExternalUsers ) )
7199 if err != nil {
72100 log .Fatal ("Cron[Synchronize external users]: %v" , err )
73101 }
74102 if setting .Cron .SyncExternalUsers .RunAtStart {
75103 entry .Prev = time .Now ()
76104 entry .ExecTimes ++
77- go models .SyncExternalUsers ()
105+ go WithUnique ( syncExternalUsers , models .SyncExternalUsers ) ()
78106 }
79107 }
80108 if setting .Cron .DeletedBranchesCleanup .Enabled {
81- entry , err = c .AddFunc ("Remove old deleted branches" , setting .Cron .DeletedBranchesCleanup .Schedule , models .RemoveOldDeletedBranches )
109+ entry , err = c .AddFunc ("Remove old deleted branches" , setting .Cron .DeletedBranchesCleanup .Schedule , WithUnique ( deletedBranchesCleanup , models .RemoveOldDeletedBranches ) )
82110 if err != nil {
83111 log .Fatal ("Cron[Remove old deleted branches]: %v" , err )
84112 }
85113 if setting .Cron .DeletedBranchesCleanup .RunAtStart {
86114 entry .Prev = time .Now ()
87115 entry .ExecTimes ++
88- go models .RemoveOldDeletedBranches ()
116+ go WithUnique ( deletedBranchesCleanup , models .RemoveOldDeletedBranches ) ()
89117 }
90118 }
91119 c .Start ()
0 commit comments