code.gitea.io/gitea@v1.19.3/modules/mirror/mirror.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.UniqueQueue 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 ...queue.Data) []queue.Data) { 33 if !setting.Mirror.Enabled { 34 return 35 } 36 mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest)) 37 38 go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run) 39 } 40 41 // AddPullMirrorToQueue adds repoID to mirror queue 42 func AddPullMirrorToQueue(repoID int64) { 43 addMirrorToQueue(PullMirrorType, repoID) 44 } 45 46 // AddPushMirrorToQueue adds the push mirror to the queue 47 func AddPushMirrorToQueue(mirrorID int64) { 48 addMirrorToQueue(PushMirrorType, mirrorID) 49 } 50 51 func addMirrorToQueue(syncType SyncType, referenceID int64) { 52 if !setting.Mirror.Enabled { 53 return 54 } 55 go func() { 56 if err := PushToQueue(syncType, referenceID); err != nil { 57 log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]. Error: %v", referenceID, err) 58 } 59 }() 60 } 61 62 // PushToQueue adds the sync request to the queue 63 func PushToQueue(mirrorType SyncType, referenceID int64) error { 64 return mirrorQueue.Push(&SyncRequest{ 65 Type: mirrorType, 66 ReferenceID: referenceID, 67 }) 68 }