Skip to content

Commit 54f0293

Browse files
cez81lunny
authored andcommitted
Mirror sync interval specified as duration string (#1407)
* Sync interval specifed as duration string * Changed mirror interval text * make fmt * Add MinInterval for mirror sync * Use duration internally * Changed min default to 10m * make fmt * Incorrect default * Removed defaults in MustDuration() * Add Mirror interval migration * Default values corrected * Use transaction during migration * Change http 500 to page with error message * Cleanup session.commit()
1 parent edbb9ee commit 54f0293

File tree

10 files changed

+90
-21
lines changed

10 files changed

+90
-21
lines changed

conf/app.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,10 @@ PULL = 300
461461
GC = 60
462462

463463
[mirror]
464-
; Default interval in hours between each check
465-
DEFAULT_INTERVAL = 8
464+
; Default interval as a duration between each check
465+
DEFAULT_INTERVAL = 8h
466+
; Min interval as a duration must be > 1m
467+
MIN_INTERVAL = 10m
466468

467469
[api]
468470
; Max number of items will response in a page

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ var migrations = []Migration{
102102
NewMigration("add show field in user openid table", addUserOpenIDShow),
103103
// v26 -> v27
104104
NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains),
105+
// v27 -> v28
106+
NewMigration("change mirror interval from hours to time.Duration", convertIntervalToDuration),
105107
}
106108

107109
// Migrate database to current version

models/migrations/v27.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2017 Gitea. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
"time"
10+
11+
"github.com/go-xorm/xorm"
12+
)
13+
14+
func convertIntervalToDuration(x *xorm.Engine) (err error) {
15+
type Repository struct {
16+
ID int64
17+
OwnerID int64
18+
Name string
19+
}
20+
type Mirror struct {
21+
ID int64 `xorm:"pk autoincr"`
22+
RepoID int64 `xorm:"INDEX"`
23+
Repo *Repository `xorm:"-"`
24+
Interval time.Duration
25+
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
26+
27+
Updated time.Time `xorm:"-"`
28+
UpdatedUnix int64 `xorm:"INDEX"`
29+
NextUpdate time.Time `xorm:"-"`
30+
NextUpdateUnix int64 `xorm:"INDEX"`
31+
32+
address string `xorm:"-"`
33+
}
34+
35+
sess := x.NewSession()
36+
defer sess.Close()
37+
38+
if err := sess.Begin(); err != nil {
39+
return err
40+
}
41+
42+
var mirrors []Mirror
43+
err = sess.Table("mirror").Select("*").Find(&mirrors)
44+
if err != nil {
45+
return fmt.Errorf("Query repositories: %v", err)
46+
}
47+
for _, mirror := range mirrors {
48+
mirror.Interval = mirror.Interval * time.Hour
49+
_, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
50+
if err != nil {
51+
return fmt.Errorf("update mirror interval failed: %v", err)
52+
}
53+
}
54+
55+
return sess.Commit()
56+
}

models/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
815815
RepoID: repo.ID,
816816
Interval: setting.Mirror.DefaultInterval,
817817
EnablePrune: true,
818-
NextUpdate: time.Now().Add(time.Duration(setting.Mirror.DefaultInterval) * time.Hour),
818+
NextUpdate: time.Now().Add(setting.Mirror.DefaultInterval),
819819
}); err != nil {
820820
return repo, fmt.Errorf("InsertOne: %v", err)
821821
}

models/repo_mirror.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ type Mirror struct {
2727
ID int64 `xorm:"pk autoincr"`
2828
RepoID int64 `xorm:"INDEX"`
2929
Repo *Repository `xorm:"-"`
30-
Interval int // Hour.
31-
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
30+
Interval time.Duration
31+
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
3232

3333
Updated time.Time `xorm:"-"`
3434
UpdatedUnix int64 `xorm:"INDEX"`
@@ -68,7 +68,7 @@ func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
6868

6969
// ScheduleNextUpdate calculates and sets next update time.
7070
func (m *Mirror) ScheduleNextUpdate() {
71-
m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
71+
m.NextUpdate = time.Now().Add(m.Interval)
7272
}
7373

7474
func (m *Mirror) readAddress() {

modules/auth/repo_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type RepoSettingForm struct {
8888
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
8989
Description string `binding:"MaxSize(255)"`
9090
Website string `binding:"Url;MaxSize(255)"`
91-
Interval int
91+
Interval string
9292
MirrorAddress string
9393
Private bool
9494
EnablePrune bool

modules/setting/setting.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,9 @@ var (
417417
}
418418

419419
// Mirror settings
420-
Mirror = struct {
421-
DefaultInterval int
422-
}{
423-
DefaultInterval: 8,
420+
Mirror struct {
421+
DefaultInterval time.Duration
422+
MinInterval time.Duration
424423
}
425424

426425
// API settings
@@ -886,14 +885,20 @@ please consider changing to GITEA_CUSTOM`)
886885
log.Fatal(4, "Failed to map Cron settings: %v", err)
887886
} else if err = Cfg.Section("git").MapTo(&Git); err != nil {
888887
log.Fatal(4, "Failed to map Git settings: %v", err)
889-
} else if err = Cfg.Section("mirror").MapTo(&Mirror); err != nil {
890-
log.Fatal(4, "Failed to map Mirror settings: %v", err)
891888
} else if err = Cfg.Section("api").MapTo(&API); err != nil {
892889
log.Fatal(4, "Failed to map API settings: %v", err)
893890
}
894891

895-
if Mirror.DefaultInterval <= 0 {
896-
Mirror.DefaultInterval = 24
892+
sec = Cfg.Section("mirror")
893+
Mirror.MinInterval = sec.Key("MIN_INTERVAL").MustDuration(10 * time.Minute)
894+
Mirror.DefaultInterval = sec.Key("DEFAULT_INTERVAL").MustDuration(8 * time.Hour)
895+
if Mirror.MinInterval.Minutes() < 1 {
896+
log.Warn("Mirror.MinInterval is too low")
897+
Mirror.MinInterval = 1 * time.Minute
898+
}
899+
if Mirror.DefaultInterval < Mirror.MinInterval {
900+
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval")
901+
Mirror.DefaultInterval = time.Hour * 8
897902
}
898903

899904
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")

options/locale/locale_en-US.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ create_repo = Create Repository
440440
default_branch = Default Branch
441441
mirror_prune = Prune
442442
mirror_prune_desc = Remove any remote-tracking references that no longer exist on the remote
443-
mirror_interval = Mirror Interval (hour)
443+
mirror_interval = Mirror interval (valid time units are "h", "m", "s")
444+
mirror_interval_invalid = Mirror interval is not valid
444445
mirror_address = Mirror Address
445446
mirror_address_desc = Please include necessary user credentials in the address.
446447
mirror_last_synced = Last Synced

routers/repo/setting.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
111111
return
112112
}
113113

114-
if form.Interval > 0 {
114+
interval, err := time.ParseDuration(form.Interval)
115+
if err != nil || interval < setting.Mirror.MinInterval {
116+
ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
117+
} else {
115118
ctx.Repo.Mirror.EnablePrune = form.EnablePrune
116-
ctx.Repo.Mirror.Interval = form.Interval
117-
ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
119+
ctx.Repo.Mirror.Interval = interval
120+
ctx.Repo.Mirror.NextUpdate = time.Now().Add(interval)
118121
if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
119-
ctx.Handle(500, "UpdateMirror", err)
122+
ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
120123
return
121124
}
122125
}

templates/repo/settings/options.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
</div>
5757
<div class="inline field {{if .Err_Interval}}error{{end}}">
5858
<label for="interval">{{.i18n.Tr "repo.mirror_interval"}}</label>
59-
<input id="interval" name="interval" type="number" value="{{.MirrorInterval}}">
59+
<input id="interval" name="interval" value="{{.MirrorInterval}}">
6060
</div>
6161
<div class="field">
6262
<label for="mirror_address">{{.i18n.Tr "repo.mirror_address"}}</label>

0 commit comments

Comments
 (0)