Skip to content

Commit e7b19e9

Browse files
committed
Add PULL_LIMIT and PUSH_LIMIT to cron.update_mirror task
Add `PULL_LIMIT` and `PUSH_LIMIT` to cron.update_mirror task to limit the number of mirrors added to the queue each time the cron task is run. Fix #16982 Signed-off-by: Andrew Thornton <[email protected]>
1 parent bd613c7 commit e7b19e9

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

custom/conf/app.example.ini

+4
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,10 @@ PATH =
17051705
;RUN_AT_START = false
17061706
;; Notice if not success
17071707
;NO_SUCCESS_NOTICE = true
1708+
;; Limit the number of mirrors added to the queue to this number (set to -1 to add all)
1709+
;PULL_LIMIT=50
1710+
;; Limit the number of mirrors added to the queue to this number (set to -1 to add all)
1711+
;PUSH_LIMIT=50
17081712

17091713
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17101714
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

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

792792
- `SCHEDULE`: **@every 10m**: Cron syntax for scheduling update mirrors, e.g. `@every 3h`.
793793
- `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.
794+
- `PULL_LIMIT`: **50**: Limit the number of mirrors added to the queue to this number (set to -1 to add all).
795+
- `PUSH_LIMIT`: **50**: Limit the number of mirrors added to the queue to this number (set to -1 to add all).
794796

795797
#### Cron - Repository Health Check (`cron.repo_health_check`)
796798

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
}

modules/cron/tasks_basic.go

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

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

services/mirror/mirror.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ func doMirrorSync(ctx context.Context, req *SyncRequest) {
4646
}
4747

4848
// Update checks and updates mirror repositories.
49-
func Update(ctx context.Context) error {
49+
func Update(ctx context.Context, pullLimit, pushLimit int) error {
5050
if !setting.Mirror.Enabled {
5151
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
5252
return nil
5353
}
5454
log.Trace("Doing: Update")
5555

56-
handler := func(idx int, bean interface{}) error {
56+
requested := 0
57+
58+
doneErr := fmt.Errorf("reached limit")
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,19 +82,50 @@ 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+
// Double check we've not been cancelled
102+
select {
103+
case <-ctx.Done():
104+
return fmt.Errorf("aborted")
105+
default:
106+
}
107+
108+
// Push to the Queue
109+
if err := mirrorQueue.Push(&item); err != nil {
110+
return err
111+
}
112+
113+
requested++
114+
if limit > 0 && requested > limit {
115+
return doneErr
116+
}
117+
return nil
87118
}
88119

89-
if err := models.MirrorsIterate(handler); err != nil {
120+
if err := models.MirrorsIterate(func(idx int, bean interface{}) error {
121+
return handler(idx, bean, pullLimit)
122+
}); err != nil && err != doneErr {
90123
log.Error("MirrorsIterate: %v", err)
91124
return err
92125
}
93-
if err := models.PushMirrorsIterate(handler); err != nil {
126+
if err := models.PushMirrorsIterate(func(idx int, bean interface{}) error {
127+
return handler(idx, bean, pushLimit)
128+
}); err != nil && err != doneErr {
94129
log.Error("PushMirrorsIterate: %v", err)
95130
return err
96131
}

0 commit comments

Comments
 (0)