gitea源码

queue.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package mirror
  4. import (
  5. "code.gitea.io/gitea/modules/graceful"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/queue"
  8. "code.gitea.io/gitea/modules/setting"
  9. )
  10. var mirrorQueue *queue.WorkerPoolQueue[*SyncRequest]
  11. // SyncType type of sync request
  12. type SyncType int
  13. const (
  14. // PullMirrorType for pull mirrors
  15. PullMirrorType SyncType = iota
  16. // PushMirrorType for push mirrors
  17. PushMirrorType
  18. )
  19. // SyncRequest for the mirror queue
  20. type SyncRequest struct {
  21. Type SyncType
  22. ReferenceID int64 // RepoID for pull mirror, MirrorID for push mirror
  23. }
  24. func queueHandler(items ...*SyncRequest) []*SyncRequest {
  25. for _, req := range items {
  26. doMirrorSync(graceful.GetManager().ShutdownContext(), req)
  27. }
  28. return nil
  29. }
  30. // StartSyncMirrors starts a go routine to sync the mirrors
  31. func StartSyncMirrors() {
  32. if !setting.Mirror.Enabled {
  33. return
  34. }
  35. mirrorQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "mirror", queueHandler)
  36. if mirrorQueue == nil {
  37. log.Fatal("Unable to create mirror queue")
  38. }
  39. go graceful.GetManager().RunWithCancel(mirrorQueue)
  40. }
  41. // AddPullMirrorToQueue adds repoID to mirror queue
  42. func AddPullMirrorToQueue(repoID int64) {
  43. addMirrorToQueue(PullMirrorType, repoID)
  44. }
  45. // AddPushMirrorToQueue adds the push mirror to the queue
  46. func AddPushMirrorToQueue(mirrorID int64) {
  47. addMirrorToQueue(PushMirrorType, mirrorID)
  48. }
  49. func addMirrorToQueue(syncType SyncType, referenceID int64) {
  50. if !setting.Mirror.Enabled {
  51. return
  52. }
  53. go func() {
  54. if err := PushToQueue(syncType, referenceID); err != nil {
  55. log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]. Error: %v", referenceID, err)
  56. }
  57. }()
  58. }
  59. // PushToQueue adds the sync request to the queue
  60. func PushToQueue(mirrorType SyncType, referenceID int64) error {
  61. return mirrorQueue.Push(&SyncRequest{
  62. Type: mirrorType,
  63. ReferenceID: referenceID,
  64. })
  65. }