2016-08-30 17:18:33 -06:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2018-09-06 20:06:09 -06:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2016-08-30 17:18:33 -06:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 08:46:21 -06:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-12-03 18:48:03 -07:00
|
|
|
|
2019-10-17 03:26:49 -06:00
|
|
|
"xorm.io/xorm"
|
2016-08-30 17:18:33 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mirror represents mirror information of a repository.
|
|
|
|
type Mirror struct {
|
2017-01-06 08:14:33 -07:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX"`
|
2016-08-30 17:18:33 -06:00
|
|
|
Repo *Repository `xorm:"-"`
|
2017-04-08 09:27:26 -06:00
|
|
|
Interval time.Duration
|
|
|
|
EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
|
2016-08-30 17:18:33 -06:00
|
|
|
|
2019-08-15 08:46:21 -06:00
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
|
|
|
|
NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
|
2016-08-30 17:18:33 -06:00
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
Address string `xorm:"-"`
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
2016-08-30 17:18:33 -06:00
|
|
|
func (m *Mirror) BeforeInsert() {
|
2017-09-12 23:18:22 -06:00
|
|
|
if m != nil {
|
2019-08-15 08:46:21 -06:00
|
|
|
m.UpdatedUnix = timeutil.TimeStampNow()
|
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow()
|
2017-09-12 23:18:22 -06:00
|
|
|
}
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-01 10:52:35 -06:00
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
|
|
|
func (m *Mirror) AfterLoad(session *xorm.Session) {
|
2017-09-12 23:18:22 -06:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:18:33 -06:00
|
|
|
var err error
|
2017-10-01 10:52:35 -06:00
|
|
|
m.Repo, err = getRepositoryByID(session, m.RepoID)
|
|
|
|
if err != nil {
|
2019-04-02 01:48:31 -06:00
|
|
|
log.Error("getRepositoryByID[%d]: %v", m.ID, err)
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleNextUpdate calculates and sets next update time.
|
|
|
|
func (m *Mirror) ScheduleNextUpdate() {
|
2018-11-08 16:58:02 -07:00
|
|
|
if m.Interval != 0 {
|
2019-08-15 08:46:21 -06:00
|
|
|
m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
|
2018-11-08 16:58:02 -07:00
|
|
|
} else {
|
|
|
|
m.NextUpdateUnix = 0
|
|
|
|
}
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
|
|
|
|
m := &Mirror{RepoID: repoID}
|
|
|
|
has, err := e.Get(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrMirrorNotExist
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMirrorByRepoID returns mirror information of a repository.
|
|
|
|
func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
|
|
|
|
return getMirrorByRepoID(x, repoID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateMirror(e Engine, m *Mirror) error {
|
2017-10-04 22:43:04 -06:00
|
|
|
_, err := e.ID(m.ID).AllCols().Update(m)
|
2016-08-30 17:18:33 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// UpdateMirror updates the mirror
|
2016-08-30 17:18:33 -06:00
|
|
|
func UpdateMirror(m *Mirror) error {
|
|
|
|
return updateMirror(x, m)
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:30:21 -07:00
|
|
|
// DeleteMirrorByRepoID deletes a mirror by repoID
|
2016-08-30 17:18:33 -06:00
|
|
|
func DeleteMirrorByRepoID(repoID int64) error {
|
|
|
|
_, err := x.Delete(&Mirror{RepoID: repoID})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
// MirrorsIterate iterates all mirror repositories.
|
|
|
|
func MirrorsIterate(f func(idx int, bean interface{}) error) error {
|
|
|
|
return x.
|
2016-11-10 08:16:32 -07:00
|
|
|
Where("next_update_unix<=?", time.Now().Unix()).
|
2018-11-08 16:58:02 -07:00
|
|
|
And("next_update_unix!=0").
|
2019-10-01 07:40:17 -06:00
|
|
|
Iterate(new(Mirror), f)
|
2016-08-30 17:18:33 -06:00
|
|
|
}
|
2019-12-14 10:30:01 -07:00
|
|
|
|
|
|
|
// InsertMirror inserts a mirror to database
|
|
|
|
func InsertMirror(mirror *Mirror) error {
|
|
|
|
_, err := x.Insert(mirror)
|
|
|
|
return err
|
|
|
|
}
|