gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mirror
  4. import (
  5. "context"
  6. "errors"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/queue"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // doMirrorSync causes this request to mirror itself
  13. func doMirrorSync(ctx context.Context, req *SyncRequest) {
  14. if req.ReferenceID == 0 {
  15. log.Warn("Skipping mirror sync request, no mirror ID was specified")
  16. return
  17. }
  18. switch req.Type {
  19. case PushMirrorType:
  20. _ = SyncPushMirror(ctx, req.ReferenceID)
  21. case PullMirrorType:
  22. _ = SyncPullMirror(ctx, req.ReferenceID)
  23. default:
  24. log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
  25. }
  26. }
  27. var errLimit = errors.New("reached limit")
  28. // Update checks and updates mirror repositories.
  29. func Update(ctx context.Context, pullLimit, pushLimit int) error {
  30. if !setting.Mirror.Enabled {
  31. log.Warn("Mirror feature disabled, but cron job enabled: skip update")
  32. return nil
  33. }
  34. log.Trace("Doing: Update")
  35. handler := func(bean any) error {
  36. var repo *repo_model.Repository
  37. var mirrorType SyncType
  38. var referenceID int64
  39. if m, ok := bean.(*repo_model.Mirror); ok {
  40. if m.GetRepository(ctx) == nil {
  41. log.Error("Disconnected mirror found: %d", m.ID)
  42. return nil
  43. }
  44. repo = m.Repo
  45. mirrorType = PullMirrorType
  46. referenceID = m.RepoID
  47. } else if m, ok := bean.(*repo_model.PushMirror); ok {
  48. if m.GetRepository(ctx) == nil {
  49. log.Error("Disconnected push-mirror found: %d", m.ID)
  50. return nil
  51. }
  52. repo = m.Repo
  53. mirrorType = PushMirrorType
  54. referenceID = m.ID
  55. } else {
  56. log.Error("Unknown bean: %v", bean)
  57. return nil
  58. }
  59. // Check we've not been cancelled
  60. select {
  61. case <-ctx.Done():
  62. return errors.New("aborted")
  63. default:
  64. }
  65. // Push to the Queue
  66. if err := PushToQueue(mirrorType, referenceID); err != nil {
  67. if err == queue.ErrAlreadyInQueue {
  68. if mirrorType == PushMirrorType {
  69. log.Trace("PushMirrors for %-v already queued for sync", repo)
  70. } else {
  71. log.Trace("PullMirrors for %-v already queued for sync", repo)
  72. }
  73. return nil
  74. }
  75. return err
  76. }
  77. return nil
  78. }
  79. pullMirrorsRequested := 0
  80. if pullLimit != 0 {
  81. if err := repo_model.MirrorsIterate(ctx, pullLimit, func(_ int, bean any) error {
  82. if err := handler(bean); err != nil {
  83. return err
  84. }
  85. pullMirrorsRequested++
  86. return nil
  87. }); err != nil && err != errLimit {
  88. log.Error("MirrorsIterate: %v", err)
  89. return err
  90. }
  91. }
  92. pushMirrorsRequested := 0
  93. if pushLimit != 0 {
  94. if err := repo_model.PushMirrorsIterate(ctx, pushLimit, func(idx int, bean any) error {
  95. if err := handler(bean); err != nil {
  96. return err
  97. }
  98. pushMirrorsRequested++
  99. return nil
  100. }); err != nil && err != errLimit {
  101. log.Error("PushMirrorsIterate: %v", err)
  102. return err
  103. }
  104. }
  105. log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
  106. return nil
  107. }
  108. // InitSyncMirrors initializes a go routine to sync the mirrors
  109. func InitSyncMirrors() {
  110. StartSyncMirrors()
  111. }