2019-10-01 19:10:17 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package mirror
|
|
|
|
|
|
|
|
import (
|
2019-12-15 15:21:28 +05:30
|
|
|
"context"
|
2019-10-01 19:10:17 +05:30
|
|
|
"fmt"
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-01-14 09:08:04 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-10-01 19:10:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-17 17:13:25 +05:30
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-10-01 19:10:17 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-10-17 17:13:25 +05:30
|
|
|
var mirrorQueue queue.UniqueQueue
|
|
|
|
|
|
|
|
// SyncType type of sync request
|
|
|
|
type SyncType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PullMirrorType for pull mirrors
|
|
|
|
PullMirrorType SyncType = iota
|
|
|
|
// PushMirrorType for push mirrors
|
|
|
|
PushMirrorType
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyncRequest for the mirror queue
|
|
|
|
type SyncRequest struct {
|
2022-03-02 13:13:11 +05:30
|
|
|
Type SyncType
|
|
|
|
ReferenceID int64 // RepoID for pull mirror, MirrorID fro push mirror
|
2021-10-17 17:13:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// doMirrorSync causes this request to mirror itself
|
|
|
|
func doMirrorSync(ctx context.Context, req *SyncRequest) {
|
2022-03-02 13:13:11 +05:30
|
|
|
if req.ReferenceID == 0 {
|
|
|
|
log.Warn("Skipping mirror sync request, no mirror ID was specified")
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 17:13:25 +05:30
|
|
|
switch req.Type {
|
|
|
|
case PushMirrorType:
|
2022-03-02 13:13:11 +05:30
|
|
|
_ = SyncPushMirror(ctx, req.ReferenceID)
|
2021-10-17 17:13:25 +05:30
|
|
|
case PullMirrorType:
|
2022-03-02 13:13:11 +05:30
|
|
|
_ = SyncPullMirror(ctx, req.ReferenceID)
|
2021-10-17 17:13:25 +05:30
|
|
|
default:
|
2022-03-02 13:13:11 +05:30
|
|
|
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
|
2021-10-17 17:13:25 +05:30
|
|
|
}
|
|
|
|
}
|
2019-10-01 19:10:17 +05:30
|
|
|
|
2021-11-23 08:39:35 +05:30
|
|
|
var errLimit = fmt.Errorf("reached limit")
|
|
|
|
|
2019-10-01 19:10:17 +05:30
|
|
|
// Update checks and updates mirror repositories.
|
2021-11-23 08:39:35 +05:30
|
|
|
func Update(ctx context.Context, pullLimit, pushLimit int) error {
|
2021-09-07 21:19:36 +05:30
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-01 19:10:17 +05:30
|
|
|
log.Trace("Doing: Update")
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2022-03-01 01:11:06 +05:30
|
|
|
handler := func(idx int, bean interface{}) error {
|
2021-10-17 17:13:25 +05:30
|
|
|
var item SyncRequest
|
2022-02-08 19:32:32 +05:30
|
|
|
var repo *repo_model.Repository
|
2021-12-10 06:57:50 +05:30
|
|
|
if m, ok := bean.(*repo_model.Mirror); ok {
|
2021-06-14 22:50:43 +05:30
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 19:32:32 +05:30
|
|
|
repo = m.Repo
|
2021-10-17 17:13:25 +05:30
|
|
|
item = SyncRequest{
|
2022-03-02 13:13:11 +05:30
|
|
|
Type: PullMirrorType,
|
|
|
|
ReferenceID: m.RepoID,
|
2021-10-17 17:13:25 +05:30
|
|
|
}
|
2021-12-10 06:57:50 +05:30
|
|
|
} else if m, ok := bean.(*repo_model.PushMirror); ok {
|
2021-06-14 22:50:43 +05:30
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected push-mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 19:32:32 +05:30
|
|
|
repo = m.Repo
|
2021-10-17 17:13:25 +05:30
|
|
|
item = SyncRequest{
|
2022-03-02 13:13:11 +05:30
|
|
|
Type: PushMirrorType,
|
|
|
|
ReferenceID: m.ID,
|
2021-10-17 17:13:25 +05:30
|
|
|
}
|
2021-06-14 22:50:43 +05:30
|
|
|
} else {
|
|
|
|
log.Error("Unknown bean: %v", bean)
|
2019-10-01 19:10:17 +05:30
|
|
|
return nil
|
|
|
|
}
|
2021-06-14 22:50:43 +05:30
|
|
|
|
2021-11-23 08:39:35 +05:30
|
|
|
// Check we've not been cancelled
|
2019-12-15 15:21:28 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-23 08:39:35 +05:30
|
|
|
return fmt.Errorf("aborted")
|
2019-12-15 15:21:28 +05:30
|
|
|
default:
|
|
|
|
}
|
2021-11-23 08:39:35 +05:30
|
|
|
|
|
|
|
// Push to the Queue
|
|
|
|
if err := mirrorQueue.Push(&item); err != nil {
|
2022-02-08 19:32:32 +05:30
|
|
|
if err == queue.ErrAlreadyInQueue {
|
|
|
|
if item.Type == PushMirrorType {
|
|
|
|
log.Trace("PushMirrors for %-v already queued for sync", repo)
|
|
|
|
} else {
|
|
|
|
log.Trace("PullMirrors for %-v already queued for sync", repo)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-23 08:39:35 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-14 22:50:43 +05:30
|
|
|
}
|
|
|
|
|
2022-02-08 19:32:32 +05:30
|
|
|
pullMirrorsRequested := 0
|
2021-11-23 08:39:35 +05:30
|
|
|
if pullLimit != 0 {
|
2022-03-01 01:11:06 +05:30
|
|
|
if err := repo_model.MirrorsIterate(pullLimit, func(idx int, bean interface{}) error {
|
|
|
|
if err := handler(idx, bean); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pullMirrorsRequested++
|
|
|
|
return nil
|
2021-11-23 08:39:35 +05:30
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("MirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-14 22:50:43 +05:30
|
|
|
}
|
2022-03-01 01:11:06 +05:30
|
|
|
|
2022-02-08 19:32:32 +05:30
|
|
|
pushMirrorsRequested := 0
|
2021-11-23 08:39:35 +05:30
|
|
|
if pushLimit != 0 {
|
2022-03-01 01:11:06 +05:30
|
|
|
if err := repo_model.PushMirrorsIterate(pushLimit, func(idx int, bean interface{}) error {
|
|
|
|
if err := handler(idx, bean); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pushMirrorsRequested++
|
|
|
|
return nil
|
2021-11-23 08:39:35 +05:30
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("PushMirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
2022-02-08 19:32:32 +05:30
|
|
|
log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
|
2020-05-17 05:01:38 +05:30
|
|
|
return nil
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
|
|
|
|
2022-01-23 02:52:14 +05:30
|
|
|
func queueHandle(data ...queue.Data) []queue.Data {
|
2021-10-17 17:13:25 +05:30
|
|
|
for _, datum := range data {
|
|
|
|
req := datum.(*SyncRequest)
|
|
|
|
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
2022-01-23 02:52:14 +05:30
|
|
|
return nil
|
2020-10-25 13:02:25 +05:30
|
|
|
}
|
|
|
|
|
2019-10-01 19:10:17 +05:30
|
|
|
// InitSyncMirrors initializes a go routine to sync the mirrors
|
|
|
|
func InitSyncMirrors() {
|
2021-09-07 21:19:36 +05:30
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 17:13:25 +05:30
|
|
|
mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest))
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run)
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// StartToMirror adds repoID to mirror queue
|
|
|
|
func StartToMirror(repoID int64) {
|
2021-09-07 21:19:36 +05:30
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 17:13:25 +05:30
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
2022-03-02 13:13:11 +05:30
|
|
|
Type: PullMirrorType,
|
|
|
|
ReferenceID: repoID,
|
2021-10-17 17:13:25 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2022-03-02 13:13:11 +05:30
|
|
|
log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]: Error: %v", repoID, err)
|
|
|
|
return
|
2021-10-17 17:13:25 +05:30
|
|
|
}
|
|
|
|
}()
|
2021-06-14 22:50:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AddPushMirrorToQueue adds the push mirror to the queue
|
|
|
|
func AddPushMirrorToQueue(mirrorID int64) {
|
2021-09-07 21:19:36 +05:30
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 17:13:25 +05:30
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
2022-03-02 13:13:11 +05:30
|
|
|
Type: PushMirrorType,
|
|
|
|
ReferenceID: mirrorID,
|
2021-10-17 17:13:25 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to push sync request to the queue for pull mirror repo[%d]: Error: %v", mirrorID, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|