本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.4KB

  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "xorm.io/xorm"
  11. )
  12. // Mirror represents mirror information of a repository.
  13. type Mirror struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. RepoID int64 `xorm:"INDEX"`
  16. Repo *Repository `xorm:"-"`
  17. Interval time.Duration
  18. EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
  19. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX"`
  20. NextUpdateUnix timeutil.TimeStamp `xorm:"INDEX"`
  21. Address string `xorm:"-"`
  22. }
  23. // BeforeInsert will be invoked by XORM before inserting a record
  24. func (m *Mirror) BeforeInsert() {
  25. if m != nil {
  26. m.UpdatedUnix = timeutil.TimeStampNow()
  27. m.NextUpdateUnix = timeutil.TimeStampNow()
  28. }
  29. }
  30. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  31. func (m *Mirror) AfterLoad(session *xorm.Session) {
  32. if m == nil {
  33. return
  34. }
  35. var err error
  36. m.Repo, err = getRepositoryByID(session, m.RepoID)
  37. if err != nil {
  38. log.Error("getRepositoryByID[%d]: %v", m.ID, err)
  39. }
  40. }
  41. // ScheduleNextUpdate calculates and sets next update time.
  42. func (m *Mirror) ScheduleNextUpdate() {
  43. if m.Interval != 0 {
  44. m.NextUpdateUnix = timeutil.TimeStampNow().AddDuration(m.Interval)
  45. } else {
  46. m.NextUpdateUnix = 0
  47. }
  48. }
  49. func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
  50. m := &Mirror{RepoID: repoID}
  51. has, err := e.Get(m)
  52. if err != nil {
  53. return nil, err
  54. } else if !has {
  55. return nil, ErrMirrorNotExist
  56. }
  57. return m, nil
  58. }
  59. // GetMirrorByRepoID returns mirror information of a repository.
  60. func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
  61. return getMirrorByRepoID(x, repoID)
  62. }
  63. func updateMirror(e Engine, m *Mirror) error {
  64. _, err := e.ID(m.ID).AllCols().Update(m)
  65. return err
  66. }
  67. // UpdateMirror updates the mirror
  68. func UpdateMirror(m *Mirror) error {
  69. return updateMirror(x, m)
  70. }
  71. // DeleteMirrorByRepoID deletes a mirror by repoID
  72. func DeleteMirrorByRepoID(repoID int64) error {
  73. _, err := x.Delete(&Mirror{RepoID: repoID})
  74. return err
  75. }
  76. // MirrorsIterate iterates all mirror repositories.
  77. func MirrorsIterate(f func(idx int, bean interface{}) error) error {
  78. return x.
  79. Where("next_update_unix<=?", time.Now().Unix()).
  80. And("next_update_unix!=0").
  81. Iterate(new(Mirror), f)
  82. }
上海开阖软件有限公司 沪ICP备12045867号-1