Skip to content

Commit d3dc654

Browse files
lunnyjeffliu27
authored andcommitted
Move status table to cron package (go-gitea#7370)
1 parent 46ab683 commit d3dc654

File tree

24 files changed

+686
-500
lines changed

24 files changed

+686
-500
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require (
5858
github.com/go-xorm/core v0.6.0 // indirect
5959
github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459
6060
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561
61-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183
61+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
6262
github.com/google/go-cmp v0.3.0 // indirect
6363
github.com/google/go-github/v24 v24.0.1
6464
github.com/gorilla/context v1.1.1
@@ -112,7 +112,7 @@ require (
112112
go.etcd.io/bbolt v1.3.2 // indirect
113113
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
114114
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b
115-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759
115+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
116116
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45
117117
golang.org/x/text v0.3.2
118118
golang.org/x/tools v0.0.0-20190620154339-431033348dd0 // indirect

go.sum

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
cloud.google.com/go v0.30.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2-
cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg=
32
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
43
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
54
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
@@ -143,9 +142,9 @@ github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459 h1:JGEuhH169J7Wtm1h
143142
github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459/go.mod h1:UK1YDlWscDspd23xW9HC24749jhvwO6riZ/HUt3gbHQ=
144143
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561 h1:deE7ritpK04PgtpyVOS2TYcQEld9qLCD5b5EbVNOuLA=
145144
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561/go.mod h1:YgYOrVn3Nj9Tq0EvjmFbphRytDj7JNRoWSStJZWDJTQ=
146-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183 h1:EBTlva3AOSb80G3JSwY6ZMdILEZJ1JKuewrbqrNjWuE=
147-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183/go.mod h1:pX+V62FFmklia2fhP3P4YSY6iJdPO5jIDKFQ5fEd5QE=
148145
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
146+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14 h1:yXtpJr/LV6PFu4nTLgfjQdcMdzjbqqXMEnHfq0Or6p8=
147+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14/go.mod h1:jPoNZLWDAqA5N3G5amEoiNbhVrmM+ZQEcnQvNQ2KaZk=
149148
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
150149
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
151150
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
@@ -222,7 +221,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
222221
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
223222
github.com/lafriks/xormstore v1.0.0 h1:P/IJzNSIpjXl/Up3o2Td5ZU/x4v6DEKLMaPQJGtmJCk=
224223
github.com/lafriks/xormstore v1.0.0/go.mod h1:dD8vHNRfEp3Uy+JvX9cMi2SXcRKJ0x4pYKsZuy843Ic=
225-
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
226224
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
227225
github.com/lib/pq v1.1.0 h1:/5u4a+KGJptBRqGzPvYQL9p0d/tPR4S31+Tnzj9lEO4=
228226
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -368,15 +366,16 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
368366
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
369367
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
370368
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
369+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
371370
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
372371
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
373372
golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
374373
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b h1:lkjdUzSyJ5P1+eal9fxXX9Xg2BTfswsonKUse48C0uE=
375374
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
376375
golang.org/x/oauth2 v0.0.0-20180620175406-ef147856a6dd/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
377376
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
378-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759 h1:TMrx+Qdx7uJAeUbv15N72h5Hmyb5+VDjEiMufAEAM04=
379-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
377+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 h1:Wo7BWFiOk0QRFMLYMqJGFMd9CgUAcGx7V+qEg/h5IBI=
378+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
380379
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
381380
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
382381
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

models/branches.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,6 @@ func (deletedBranch *DeletedBranch) LoadUser() {
458458

459459
// RemoveOldDeletedBranches removes old deleted branches
460460
func RemoveOldDeletedBranches() {
461-
if !taskStatusTable.StartIfNotRunning(`deleted_branches_cleanup`) {
462-
return
463-
}
464-
defer taskStatusTable.Stop(`deleted_branches_cleanup`)
465-
466461
log.Trace("Doing: DeletedBranchesCleanup")
467462

468463
deleteBefore := time.Now().Add(-setting.Cron.DeletedBranchesCleanup.OlderThan)

models/repo.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,11 +2052,6 @@ func DeleteRepositoryArchives() error {
20522052

20532053
// DeleteOldRepositoryArchives deletes old repository archives.
20542054
func DeleteOldRepositoryArchives() {
2055-
if !taskStatusTable.StartIfNotRunning(archiveCleanup) {
2056-
return
2057-
}
2058-
defer taskStatusTable.Stop(archiveCleanup)
2059-
20602055
log.Trace("Doing: ArchiveCleanup")
20612056

20622057
if err := x.Where("id > 0").Iterate(new(Repository), deleteOldRepositoryArchives); err != nil {
@@ -2183,23 +2178,8 @@ func SyncRepositoryHooks() error {
21832178
})
21842179
}
21852180

2186-
// Prevent duplicate running tasks.
2187-
var taskStatusTable = sync.NewStatusTable()
2188-
2189-
const (
2190-
mirrorUpdate = "mirror_update"
2191-
gitFsck = "git_fsck"
2192-
checkRepos = "check_repos"
2193-
archiveCleanup = "archive_cleanup"
2194-
)
2195-
21962181
// GitFsck calls 'git fsck' to check repository health.
21972182
func GitFsck() {
2198-
if !taskStatusTable.StartIfNotRunning(gitFsck) {
2199-
return
2200-
}
2201-
defer taskStatusTable.Stop(gitFsck)
2202-
22032183
log.Trace("Doing: GitFsck")
22042184

22052185
if err := x.
@@ -2268,11 +2248,6 @@ func repoStatsCheck(checker *repoChecker) {
22682248

22692249
// CheckRepoStats checks the repository stats
22702250
func CheckRepoStats() {
2271-
if !taskStatusTable.StartIfNotRunning(checkRepos) {
2272-
return
2273-
}
2274-
defer taskStatusTable.Stop(checkRepos)
2275-
22762251
log.Trace("Doing: CheckRepoStats")
22772252

22782253
checkers := []*repoChecker{

models/repo_mirror.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,6 @@ func DeleteMirrorByRepoID(repoID int64) error {
315315

316316
// MirrorUpdate checks and updates mirror repositories.
317317
func MirrorUpdate() {
318-
if !taskStatusTable.StartIfNotRunning(mirrorUpdate) {
319-
return
320-
}
321-
defer taskStatusTable.Stop(mirrorUpdate)
322-
323318
log.Trace("Doing: MirrorUpdate")
324319

325320
if err := x.

models/user.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ const (
6060
algoPbkdf2 = "pbkdf2"
6161
)
6262

63-
const syncExternalUsers = "sync_external_users"
64-
6563
var (
6664
// ErrUserNotKeyOwner user does not own this key error
6765
ErrUserNotKeyOwner = errors.New("User does not own this public key")
@@ -1643,11 +1641,6 @@ func synchronizeLdapSSHPublicKeys(usr *User, s *LoginSource, sshPublicKeys []str
16431641

16441642
// SyncExternalUsers is used to synchronize users with external authorization source
16451643
func SyncExternalUsers() {
1646-
if !taskStatusTable.StartIfNotRunning(syncExternalUsers) {
1647-
return
1648-
}
1649-
defer taskStatusTable.Stop(syncExternalUsers)
1650-
16511644
log.Trace("Doing: SyncExternalUsers")
16521645

16531646
ls, err := LoginSources()

modules/cron/cron.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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
78
import (
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

1728
var 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
2048
func 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

Comments
 (0)