1
1
// Copyright 2014 The Gogs Authors. All rights reserved.
2
+ // Copyright 2019 The Gitea Authors. All rights reserved.
2
3
// Use of this source code is governed by a MIT-style
3
4
// license that can be found in the LICENSE file.
4
5
@@ -7,85 +8,112 @@ package cron
7
8
import (
8
9
"time"
9
10
10
- "github.com/gogits/cron"
11
-
12
11
"code.gitea.io/gitea/models"
13
12
"code.gitea.io/gitea/modules/log"
14
13
"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"
15
26
)
16
27
17
28
var c = cron .New ()
18
29
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
+
19
47
// NewContext begins cron tasks
20
48
func NewContext () {
21
49
var (
22
50
entry * cron.Entry
23
51
err error
24
52
)
25
53
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 ) )
27
55
if err != nil {
28
56
log .Fatal ("Cron[Update mirrors]: %v" , err )
29
57
}
30
58
if setting .Cron .UpdateMirror .RunAtStart {
31
59
entry .Prev = time .Now ()
32
60
entry .ExecTimes ++
33
- go models .MirrorUpdate ()
61
+ go WithUnique ( mirrorUpdate , models .MirrorUpdate ) ()
34
62
}
35
63
}
36
64
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 ) )
38
66
if err != nil {
39
67
log .Fatal ("Cron[Repository health check]: %v" , err )
40
68
}
41
69
if setting .Cron .RepoHealthCheck .RunAtStart {
42
70
entry .Prev = time .Now ()
43
71
entry .ExecTimes ++
44
- go models .GitFsck ()
72
+ go WithUnique ( gitFsck , models .GitFsck ) ()
45
73
}
46
74
}
47
75
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 ) )
49
77
if err != nil {
50
78
log .Fatal ("Cron[Check repository statistics]: %v" , err )
51
79
}
52
80
if setting .Cron .CheckRepoStats .RunAtStart {
53
81
entry .Prev = time .Now ()
54
82
entry .ExecTimes ++
55
- go models .CheckRepoStats ()
83
+ go WithUnique ( checkRepos , models .CheckRepoStats ) ()
56
84
}
57
85
}
58
86
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 ) )
60
88
if err != nil {
61
89
log .Fatal ("Cron[Clean up old repository archives]: %v" , err )
62
90
}
63
91
if setting .Cron .ArchiveCleanup .RunAtStart {
64
92
entry .Prev = time .Now ()
65
93
entry .ExecTimes ++
66
- go models .DeleteOldRepositoryArchives ()
94
+ go WithUnique ( archiveCleanup , models .DeleteOldRepositoryArchives ) ()
67
95
}
68
96
}
69
97
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 ) )
71
99
if err != nil {
72
100
log .Fatal ("Cron[Synchronize external users]: %v" , err )
73
101
}
74
102
if setting .Cron .SyncExternalUsers .RunAtStart {
75
103
entry .Prev = time .Now ()
76
104
entry .ExecTimes ++
77
- go models .SyncExternalUsers ()
105
+ go WithUnique ( syncExternalUsers , models .SyncExternalUsers ) ()
78
106
}
79
107
}
80
108
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 ) )
82
110
if err != nil {
83
111
log .Fatal ("Cron[Remove old deleted branches]: %v" , err )
84
112
}
85
113
if setting .Cron .DeletedBranchesCleanup .RunAtStart {
86
114
entry .Prev = time .Now ()
87
115
entry .ExecTimes ++
88
- go models .RemoveOldDeletedBranches ()
116
+ go WithUnique ( deletedBranchesCleanup , models .RemoveOldDeletedBranches ) ()
89
117
}
90
118
}
91
119
c .Start ()
0 commit comments