Skip to content

Commit 188fd2d

Browse files
authored
Add PULL_LIMIT and PUSH_LIMIT to cron.update_mirror task (#17568)
1 parent 9450410 commit 188fd2d

File tree

6 files changed

+70
-17
lines changed

6 files changed

+70
-17
lines changed

custom/conf/app.example.ini

+6
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,12 @@ PATH =
17191719
;RUN_AT_START = false
17201720
;; Notice if not success
17211721
;NO_SUCCESS_NOTICE = true
1722+
;; Limit the number of mirrors added to the queue to this number
1723+
;; (negative values mean no limit, 0 will result in no result in no mirrors being queued effectively disabling pull mirror updating.)
1724+
;PULL_LIMIT=50
1725+
;; Limit the number of mirrors added to the queue to this number
1726+
;; (negative values mean no limit, 0 will result in no mirrors being queued effectively disabling push mirror updating)
1727+
;PUSH_LIMIT=50
17221728

17231729
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17241730
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
828828

829829
- `SCHEDULE`: **@every 10m**: Cron syntax for scheduling update mirrors, e.g. `@every 3h`.
830830
- `NO_SUCCESS_NOTICE`: **true**: The cron task for update mirrors success report is not very useful - as it just means that the mirrors have been queued. Therefore this is turned off by default.
831+
- `PULL_LIMIT`: **50**: Limit the number of mirrors added to the queue to this number (negative values mean no limit, 0 will result in no mirrors being queued effectively disabling pull mirror updating).
832+
- `PUSH_LIMIT`: **50**: Limit the number of mirrors added to the queue to this number (negative values mean no limit, 0 will result in no mirrors being queued effectively disabling push mirror updating).
831833

832834
#### Cron - Repository Health Check (`cron.repo_health_check`)
833835

models/repo_mirror.go

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func MirrorsIterate(f func(idx int, bean interface{}) error) error {
119119
return db.GetEngine(db.DefaultContext).
120120
Where("next_update_unix<=?", time.Now().Unix()).
121121
And("next_update_unix!=0").
122+
OrderBy("updated_unix ASC").
122123
Iterate(new(Mirror), f)
123124
}
124125

models/repo_pushmirror.go

+1
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ func PushMirrorsIterate(f func(idx int, bean interface{}) error) error {
107107
return db.GetEngine(db.DefaultContext).
108108
Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
109109
And("`interval` != 0").
110+
OrderBy("last_update ASC").
110111
Iterate(new(PushMirror), f)
111112
}

services/cron/tasks_basic.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ import (
1818
)
1919

2020
func registerUpdateMirrorTask() {
21-
RegisterTaskFatal("update_mirrors", &BaseConfig{
22-
Enabled: true,
23-
RunAtStart: false,
24-
Schedule: "@every 10m",
25-
NoSuccessNotice: true,
26-
}, func(ctx context.Context, _ *models.User, _ Config) error {
27-
return mirror_service.Update(ctx)
21+
type UpdateMirrorTaskConfig struct {
22+
BaseConfig
23+
PullLimit int
24+
PushLimit int
25+
}
26+
27+
RegisterTaskFatal("update_mirrors", &UpdateMirrorTaskConfig{
28+
BaseConfig: BaseConfig{
29+
Enabled: true,
30+
RunAtStart: false,
31+
Schedule: "@every 10m",
32+
NoSuccessNotice: true,
33+
},
34+
PullLimit: 50,
35+
PushLimit: 50,
36+
}, func(ctx context.Context, _ *models.User, cfg Config) error {
37+
umtc := cfg.(*UpdateMirrorTaskConfig)
38+
return mirror_service.Update(ctx, umtc.PullLimit, umtc.PushLimit)
2839
})
2940
}
3041

services/mirror/mirror.go

+42-10
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,19 @@ func doMirrorSync(ctx context.Context, req *SyncRequest) {
4545
}
4646
}
4747

48+
var errLimit = fmt.Errorf("reached limit")
49+
4850
// Update checks and updates mirror repositories.
49-
func Update(ctx context.Context) error {
51+
func Update(ctx context.Context, pullLimit, pushLimit int) error {
5052
if !setting.Mirror.Enabled {
5153
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
5254
return nil
5355
}
5456
log.Trace("Doing: Update")
5557

56-
handler := func(idx int, bean interface{}) error {
58+
requested := 0
59+
60+
handler := func(idx int, bean interface{}, limit int) error {
5761
var item SyncRequest
5862
if m, ok := bean.(*models.Mirror); ok {
5963
if m.Repo == nil {
@@ -78,21 +82,49 @@ func Update(ctx context.Context) error {
7882
return nil
7983
}
8084

85+
// Check we've not been cancelled
8186
select {
8287
case <-ctx.Done():
83-
return fmt.Errorf("Aborted")
88+
return fmt.Errorf("aborted")
8489
default:
85-
return mirrorQueue.Push(&item)
8690
}
91+
92+
// Check if this request is already in the queue
93+
has, err := mirrorQueue.Has(&item)
94+
if err != nil {
95+
return err
96+
}
97+
if has {
98+
return nil
99+
}
100+
101+
// Push to the Queue
102+
if err := mirrorQueue.Push(&item); err != nil {
103+
return err
104+
}
105+
106+
requested++
107+
if limit > 0 && requested > limit {
108+
return errLimit
109+
}
110+
return nil
87111
}
88112

89-
if err := models.MirrorsIterate(handler); err != nil {
90-
log.Error("MirrorsIterate: %v", err)
91-
return err
113+
if pullLimit != 0 {
114+
if err := models.MirrorsIterate(func(idx int, bean interface{}) error {
115+
return handler(idx, bean, pullLimit)
116+
}); err != nil && err != errLimit {
117+
log.Error("MirrorsIterate: %v", err)
118+
return err
119+
}
92120
}
93-
if err := models.PushMirrorsIterate(handler); err != nil {
94-
log.Error("PushMirrorsIterate: %v", err)
95-
return err
121+
if pushLimit != 0 {
122+
if err := models.PushMirrorsIterate(func(idx int, bean interface{}) error {
123+
return handler(idx, bean, pushLimit)
124+
}); err != nil && err != errLimit {
125+
log.Error("PushMirrorsIterate: %v", err)
126+
return err
127+
}
96128
}
97129
log.Trace("Finished: Update")
98130
return nil

0 commit comments

Comments
 (0)