Browse Source

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()
for-closed-social
Jonas 7 years ago
committed by Lunny Xiao
parent
commit
54f0293f0a
10 changed files with 90 additions and 21 deletions
  1. +4
    -2
      conf/app.ini
  2. +2
    -0
      models/migrations/migrations.go
  3. +56
    -0
      models/migrations/v27.go
  4. +1
    -1
      models/repo.go
  5. +3
    -3
      models/repo_mirror.go
  6. +1
    -1
      modules/auth/repo_form.go
  7. +13
    -8
      modules/setting/setting.go
  8. +2
    -1
      options/locale/locale_en-US.ini
  9. +7
    -4
      routers/repo/setting.go
  10. +1
    -1
      templates/repo/settings/options.tmpl

+ 4
- 2
conf/app.ini View File

@ -461,8 +461,10 @@ PULL = 300
GC = 60
[mirror]
; Default interval in hours between each check
DEFAULT_INTERVAL = 8
; Default interval as a duration between each check
DEFAULT_INTERVAL = 8h
; Min interval as a duration must be > 1m
MIN_INTERVAL = 10m
[api]
; Max number of items will response in a page

+ 2
- 0
models/migrations/migrations.go View File

@ -102,6 +102,8 @@ var migrations = []Migration{
NewMigration("add show field in user openid table", addUserOpenIDShow),
// v26 -> v27
NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains),
// v27 -> v28
NewMigration("change mirror interval from hours to time.Duration", convertIntervalToDuration),
}
// Migrate database to current version

+ 56
- 0
models/migrations/v27.go View File

@ -0,0 +1,56 @@
// Copyright 2017 Gitea. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"fmt"
"time"
"github.com/go-xorm/xorm"
)
func convertIntervalToDuration(x *xorm.Engine) (err error) {
type Repository struct {
ID int64
OwnerID int64
Name string
}
type Mirror struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX"`
Repo *Repository `xorm:"-"`
Interval time.Duration
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
NextUpdate time.Time `xorm:"-"`
NextUpdateUnix int64 `xorm:"INDEX"`
address string `xorm:"-"`
}
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
var mirrors []Mirror
err = sess.Table("mirror").Select("*").Find(&mirrors)
if err != nil {
return fmt.Errorf("Query repositories: %v", err)
}
for _, mirror := range mirrors {
mirror.Interval = mirror.Interval * time.Hour
_, err := sess.Id(mirror.ID).Cols("interval").Update(mirror)
if err != nil {
return fmt.Errorf("update mirror interval failed: %v", err)
}
}
return sess.Commit()
}

+ 1
- 1
models/repo.go View File

@ -815,7 +815,7 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
RepoID: repo.ID,
Interval: setting.Mirror.DefaultInterval,
EnablePrune: true,
NextUpdate: time.Now().Add(time.Duration(setting.Mirror.DefaultInterval) * time.Hour),
NextUpdate: time.Now().Add(setting.Mirror.DefaultInterval),
}); err != nil {
return repo, fmt.Errorf("InsertOne: %v", err)
}

+ 3
- 3
models/repo_mirror.go View File

@ -27,8 +27,8 @@ type Mirror struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX"`
Repo *Repository `xorm:"-"`
Interval int // Hour.
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
Interval time.Duration
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
Updated time.Time `xorm:"-"`
UpdatedUnix int64 `xorm:"INDEX"`
@ -68,7 +68,7 @@ func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
// ScheduleNextUpdate calculates and sets next update time.
func (m *Mirror) ScheduleNextUpdate() {
m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour)
m.NextUpdate = time.Now().Add(m.Interval)
}
func (m *Mirror) readAddress() {

+ 1
- 1
modules/auth/repo_form.go View File

@ -88,7 +88,7 @@ type RepoSettingForm struct {
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
Description string `binding:"MaxSize(255)"`
Website string `binding:"Url;MaxSize(255)"`
Interval int
Interval string
MirrorAddress string
Private bool
EnablePrune bool

+ 13
- 8
modules/setting/setting.go View File

@ -417,10 +417,9 @@ var (
}
// Mirror settings
Mirror = struct {
DefaultInterval int
}{
DefaultInterval: 8,
Mirror struct {
DefaultInterval time.Duration
MinInterval time.Duration
}
// API settings
@ -886,14 +885,20 @@ please consider changing to GITEA_CUSTOM`)
log.Fatal(4, "Failed to map Cron settings: %v", err)
} else if err = Cfg.Section("git").MapTo(&Git); err != nil {
log.Fatal(4, "Failed to map Git settings: %v", err)
} else if err = Cfg.Section("mirror").MapTo(&Mirror); err != nil {
log.Fatal(4, "Failed to map Mirror settings: %v", err)
} else if err = Cfg.Section("api").MapTo(&API); err != nil {
log.Fatal(4, "Failed to map API settings: %v", err)
}
if Mirror.DefaultInterval <= 0 {
Mirror.DefaultInterval = 24
sec = Cfg.Section("mirror")
Mirror.MinInterval = sec.Key("MIN_INTERVAL").MustDuration(10 * time.Minute)
Mirror.DefaultInterval = sec.Key("DEFAULT_INTERVAL").MustDuration(8 * time.Hour)
if Mirror.MinInterval.Minutes() < 1 {
log.Warn("Mirror.MinInterval is too low")
Mirror.MinInterval = 1 * time.Minute
}
if Mirror.DefaultInterval < Mirror.MinInterval {
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval")
Mirror.DefaultInterval = time.Hour * 8
}
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")

+ 2
- 1
options/locale/locale_en-US.ini View File

@ -440,7 +440,8 @@ create_repo = Create Repository
default_branch = Default Branch
mirror_prune = Prune
mirror_prune_desc = Remove any remote-tracking references that no longer exist on the remote
mirror_interval = Mirror Interval (hour)
mirror_interval = Mirror interval (valid time units are "h", "m", "s")
mirror_interval_invalid = Mirror interval is not valid
mirror_address = Mirror Address
mirror_address_desc = Please include necessary user credentials in the address.
mirror_last_synced = Last Synced

+ 7
- 4
routers/repo/setting.go View File

@ -111,12 +111,15 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
return
}
if form.Interval > 0 {
interval, err := time.ParseDuration(form.Interval)
if err != nil || interval < setting.Mirror.MinInterval {
ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
} else {
ctx.Repo.Mirror.EnablePrune = form.EnablePrune
ctx.Repo.Mirror.Interval = form.Interval
ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
ctx.Repo.Mirror.Interval = interval
ctx.Repo.Mirror.NextUpdate = time.Now().Add(interval)
if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
ctx.Handle(500, "UpdateMirror", err)
ctx.RenderWithErr(ctx.Tr("repo.mirror_interval_invalid"), tplSettingsOptions, &form)
return
}
}

+ 1
- 1
templates/repo/settings/options.tmpl View File

@ -56,7 +56,7 @@
</div>
<div class="inline field {{if .Err_Interval}}error{{end}}">
<label for="interval">{{.i18n.Tr "repo.mirror_interval"}}</label>
<input id="interval" name="interval" type="number" value="{{.MirrorInterval}}">
<input id="interval" name="interval" value="{{.MirrorInterval}}">
</div>
<div class="field">
<label for="mirror_address">{{.i18n.Tr "repo.mirror_address"}}</label>

Loading…
Cancel
Save