@@ -12,80 +12,107 @@ import (
12
12
"code.gitea.io/gitea/models"
13
13
"code.gitea.io/gitea/modules/log"
14
14
"code.gitea.io/gitea/modules/setting"
15
+ "code.gitea.io/gitea/modules/sync"
16
+ )
17
+
18
+ const (
19
+ mirrorUpdate = "mirror_update"
20
+ gitFsck = "git_fsck"
21
+ checkRepos = "check_repos"
22
+ archiveCleanup = "archive_cleanup"
23
+ syncExternalUsers = "sync_external_users"
24
+ deletedBranchesCleanup = "deleted_branches_cleanup"
15
25
)
16
26
17
27
var c = cron .New ()
18
28
29
+ // Prevent duplicate running tasks.
30
+ var taskStatusTable = sync .NewStatusTable ()
31
+
32
+ // CronFunc defines a cron function
33
+ type CronFunc func ()
34
+
35
+ // WithUnique wrap a cron func with an unique running check
36
+ func WithUnique (name string , body CronFunc ) CronFunc {
37
+ return func () {
38
+ if ! taskStatusTable .StartIfNotRunning (name ) {
39
+ return
40
+ }
41
+ defer taskStatusTable .Stop (name )
42
+ body ()
43
+ }
44
+ }
45
+
19
46
// NewContext begins cron tasks
20
47
func NewContext () {
21
48
var (
22
49
entry * cron.Entry
23
50
err error
24
51
)
25
52
if setting .Cron .UpdateMirror .Enabled {
26
- entry , err = c .AddFunc ("Update mirrors" , setting .Cron .UpdateMirror .Schedule , models .MirrorUpdate )
53
+ entry , err = c .AddFunc ("Update mirrors" , setting .Cron .UpdateMirror .Schedule , WithUnique ( mirrorUpdate , models .MirrorUpdate ) )
27
54
if err != nil {
28
55
log .Fatal ("Cron[Update mirrors]: %v" , err )
29
56
}
30
57
if setting .Cron .UpdateMirror .RunAtStart {
31
58
entry .Prev = time .Now ()
32
59
entry .ExecTimes ++
33
- go models .MirrorUpdate ()
60
+ go WithUnique ( mirrorUpdate , models .MirrorUpdate ) ()
34
61
}
35
62
}
36
63
if setting .Cron .RepoHealthCheck .Enabled {
37
- entry , err = c .AddFunc ("Repository health check" , setting .Cron .RepoHealthCheck .Schedule , models .GitFsck )
64
+ entry , err = c .AddFunc ("Repository health check" , setting .Cron .RepoHealthCheck .Schedule , WithUnique ( gitFsck , models .GitFsck ) )
38
65
if err != nil {
39
66
log .Fatal ("Cron[Repository health check]: %v" , err )
40
67
}
41
68
if setting .Cron .RepoHealthCheck .RunAtStart {
42
69
entry .Prev = time .Now ()
43
70
entry .ExecTimes ++
44
- go models .GitFsck ()
71
+ go WithUnique ( gitFsck , models .GitFsck ) ()
45
72
}
46
73
}
47
74
if setting .Cron .CheckRepoStats .Enabled {
48
- entry , err = c .AddFunc ("Check repository statistics" , setting .Cron .CheckRepoStats .Schedule , models .CheckRepoStats )
75
+ entry , err = c .AddFunc ("Check repository statistics" , setting .Cron .CheckRepoStats .Schedule , WithUnique ( checkRepos , models .CheckRepoStats ) )
49
76
if err != nil {
50
77
log .Fatal ("Cron[Check repository statistics]: %v" , err )
51
78
}
52
79
if setting .Cron .CheckRepoStats .RunAtStart {
53
80
entry .Prev = time .Now ()
54
81
entry .ExecTimes ++
55
- go models .CheckRepoStats ()
82
+ go WithUnique ( checkRepos , models .CheckRepoStats ) ()
56
83
}
57
84
}
58
85
if setting .Cron .ArchiveCleanup .Enabled {
59
- entry , err = c .AddFunc ("Clean up old repository archives" , setting .Cron .ArchiveCleanup .Schedule , models .DeleteOldRepositoryArchives )
86
+ entry , err = c .AddFunc ("Clean up old repository archives" , setting .Cron .ArchiveCleanup .Schedule , WithUnique ( archiveCleanup , models .DeleteOldRepositoryArchives ) )
60
87
if err != nil {
61
88
log .Fatal ("Cron[Clean up old repository archives]: %v" , err )
62
89
}
63
90
if setting .Cron .ArchiveCleanup .RunAtStart {
64
91
entry .Prev = time .Now ()
65
92
entry .ExecTimes ++
66
- go models .DeleteOldRepositoryArchives ()
93
+ go WithUnique ( archiveCleanup , models .DeleteOldRepositoryArchives ) ()
67
94
}
68
95
}
69
96
if setting .Cron .SyncExternalUsers .Enabled {
70
- entry , err = c .AddFunc ("Synchronize external users" , setting .Cron .SyncExternalUsers .Schedule , models .SyncExternalUsers )
97
+ entry , err = c .AddFunc ("Synchronize external users" , setting .Cron .SyncExternalUsers .Schedule , WithUnique ( syncExternalUsers , models .SyncExternalUsers ) )
71
98
if err != nil {
72
99
log .Fatal ("Cron[Synchronize external users]: %v" , err )
73
100
}
74
101
if setting .Cron .SyncExternalUsers .RunAtStart {
75
102
entry .Prev = time .Now ()
76
103
entry .ExecTimes ++
77
- go models .SyncExternalUsers ()
104
+ go WithUnique ( syncExternalUsers , models .SyncExternalUsers ) ()
78
105
}
79
106
}
80
107
if setting .Cron .DeletedBranchesCleanup .Enabled {
81
- entry , err = c .AddFunc ("Remove old deleted branches" , setting .Cron .DeletedBranchesCleanup .Schedule , models .RemoveOldDeletedBranches )
108
+ entry , err = c .AddFunc ("Remove old deleted branches" , setting .Cron .DeletedBranchesCleanup .Schedule , WithUnique ( deletedBranchesCleanup , models .RemoveOldDeletedBranches ) )
82
109
if err != nil {
83
110
log .Fatal ("Cron[Remove old deleted branches]: %v" , err )
84
111
}
85
112
if setting .Cron .DeletedBranchesCleanup .RunAtStart {
86
113
entry .Prev = time .Now ()
87
114
entry .ExecTimes ++
88
- go models .RemoveOldDeletedBranches ()
115
+ go WithUnique ( deletedBranchesCleanup , models .RemoveOldDeletedBranches ) ()
89
116
}
90
117
}
91
118
c .Start ()
0 commit comments