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