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