Skip to content

Commit d2958d9

Browse files
lunnytechknowlogick
authored andcommitted
Move status table to cron package (#7370)
1 parent 760c473 commit d2958d9

File tree

24 files changed

+686
-500
lines changed

24 files changed

+686
-500
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ require (
5757
github.com/go-xorm/core v0.6.0 // indirect
5858
github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459
5959
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561
60-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183
60+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
6161
github.com/google/go-cmp v0.3.0 // indirect
6262
github.com/google/go-github/v24 v24.0.1
6363
github.com/gorilla/context v1.1.1
@@ -111,7 +111,7 @@ require (
111111
go.etcd.io/bbolt v1.3.2 // indirect
112112
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443
113113
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b
114-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759
114+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
115115
golang.org/x/sys v0.0.0-20190620070143-6f217b454f45
116116
golang.org/x/text v0.3.2
117117
golang.org/x/tools v0.0.0-20190620154339-431033348dd0 // indirect

go.sum

+5-6
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=
@@ -141,9 +140,9 @@ github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459 h1:JGEuhH169J7Wtm1h
141140
github.com/go-xorm/xorm v0.7.3-0.20190620151208-f1b4f8368459/go.mod h1:UK1YDlWscDspd23xW9HC24749jhvwO6riZ/HUt3gbHQ=
142141
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561 h1:deE7ritpK04PgtpyVOS2TYcQEld9qLCD5b5EbVNOuLA=
143142
github.com/gogits/chardet v0.0.0-20150115103509-2404f7772561/go.mod h1:YgYOrVn3Nj9Tq0EvjmFbphRytDj7JNRoWSStJZWDJTQ=
144-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183 h1:EBTlva3AOSb80G3JSwY6ZMdILEZJ1JKuewrbqrNjWuE=
145-
github.com/gogits/cron v0.0.0-20160810035002-7f3990acf183/go.mod h1:pX+V62FFmklia2fhP3P4YSY6iJdPO5jIDKFQ5fEd5QE=
146143
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
144+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14 h1:yXtpJr/LV6PFu4nTLgfjQdcMdzjbqqXMEnHfq0Or6p8=
145+
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14/go.mod h1:jPoNZLWDAqA5N3G5amEoiNbhVrmM+ZQEcnQvNQ2KaZk=
147146
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
148147
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
149148
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
@@ -220,7 +219,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
220219
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
221220
github.com/lafriks/xormstore v1.0.0 h1:P/IJzNSIpjXl/Up3o2Td5ZU/x4v6DEKLMaPQJGtmJCk=
222221
github.com/lafriks/xormstore v1.0.0/go.mod h1:dD8vHNRfEp3Uy+JvX9cMi2SXcRKJ0x4pYKsZuy843Ic=
223-
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
224222
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
225223
github.com/lib/pq v1.1.0 h1:/5u4a+KGJptBRqGzPvYQL9p0d/tPR4S31+Tnzj9lEO4=
226224
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -366,15 +364,16 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
366364
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
367365
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
368366
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
367+
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
369368
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
370369
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
371370
golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
372371
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b h1:lkjdUzSyJ5P1+eal9fxXX9Xg2BTfswsonKUse48C0uE=
373372
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
374373
golang.org/x/oauth2 v0.0.0-20180620175406-ef147856a6dd/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
375374
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
376-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759 h1:TMrx+Qdx7uJAeUbv15N72h5Hmyb5+VDjEiMufAEAM04=
377-
golang.org/x/oauth2 v0.0.0-20181101160152-c453e0c75759/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
375+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421 h1:Wo7BWFiOk0QRFMLYMqJGFMd9CgUAcGx7V+qEg/h5IBI=
376+
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
378377
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
379378
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
380379
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

models/branches.go

-5
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

-25
Original file line numberDiff line numberDiff line change
@@ -2056,11 +2056,6 @@ func DeleteRepositoryArchives() error {
20562056

20572057
// DeleteOldRepositoryArchives deletes old repository archives.
20582058
func DeleteOldRepositoryArchives() {
2059-
if !taskStatusTable.StartIfNotRunning(archiveCleanup) {
2060-
return
2061-
}
2062-
defer taskStatusTable.Stop(archiveCleanup)
2063-
20642059
log.Trace("Doing: ArchiveCleanup")
20652060

20662061
if err := x.Where("id > 0").Iterate(new(Repository), deleteOldRepositoryArchives); err != nil {
@@ -2187,23 +2182,8 @@ func SyncRepositoryHooks() error {
21872182
})
21882183
}
21892184

2190-
// Prevent duplicate running tasks.
2191-
var taskStatusTable = sync.NewStatusTable()
2192-
2193-
const (
2194-
mirrorUpdate = "mirror_update"
2195-
gitFsck = "git_fsck"
2196-
checkRepos = "check_repos"
2197-
archiveCleanup = "archive_cleanup"
2198-
)
2199-
22002185
// GitFsck calls 'git fsck' to check repository health.
22012186
func GitFsck() {
2202-
if !taskStatusTable.StartIfNotRunning(gitFsck) {
2203-
return
2204-
}
2205-
defer taskStatusTable.Stop(gitFsck)
2206-
22072187
log.Trace("Doing: GitFsck")
22082188

22092189
if err := x.
@@ -2272,11 +2252,6 @@ func repoStatsCheck(checker *repoChecker) {
22722252

22732253
// CheckRepoStats checks the repository stats
22742254
func CheckRepoStats() {
2275-
if !taskStatusTable.StartIfNotRunning(checkRepos) {
2276-
return
2277-
}
2278-
defer taskStatusTable.Stop(checkRepos)
2279-
22802255
log.Trace("Doing: CheckRepoStats")
22812256

22822257
checkers := []*repoChecker{

models/repo_mirror.go

-5
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

-7
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

+42-14
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)