2019-10-01 07:40:17 -06:00
|
|
|
// 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 02:51:28 -07:00
|
|
|
"context"
|
2019-10-01 07:40:17 -06:00
|
|
|
"fmt"
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-01-13 20:38:04 -07:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-10-01 07:40:17 -06:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-17 05:43:25 -06:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-10-01 07:40:17 -06:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-10-17 05:43:25 -06:00
|
|
|
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-14 06:04:41 -06:00
|
|
|
Type SyncType
|
|
|
|
ReferenceID int64 // RepoID for pull mirror, MirrorID fro push mirror
|
2021-10-17 05:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// doMirrorSync causes this request to mirror itself
|
|
|
|
func doMirrorSync(ctx context.Context, req *SyncRequest) {
|
2022-03-14 06:04:41 -06:00
|
|
|
if req.ReferenceID == 0 {
|
|
|
|
log.Warn("Skipping mirror sync request, no reference ID was specified")
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 05:43:25 -06:00
|
|
|
switch req.Type {
|
|
|
|
case PushMirrorType:
|
2022-03-14 06:04:41 -06:00
|
|
|
_ = SyncPushMirror(ctx, req.ReferenceID)
|
2021-10-17 05:43:25 -06:00
|
|
|
case PullMirrorType:
|
2022-03-14 06:04:41 -06:00
|
|
|
_ = SyncPullMirror(ctx, req.ReferenceID)
|
2021-10-17 05:43:25 -06:00
|
|
|
default:
|
2022-03-14 06:04:41 -06:00
|
|
|
log.Error("Unknown Request type in queue: %v for ReferenceID[%d]", req.Type, req.ReferenceID)
|
2021-10-17 05:43:25 -06:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 07:40:17 -06:00
|
|
|
|
2021-11-22 20:09:35 -07:00
|
|
|
var errLimit = fmt.Errorf("reached limit")
|
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
// Update checks and updates mirror repositories.
|
2021-11-22 20:09:35 -07:00
|
|
|
func Update(ctx context.Context, pullLimit, pushLimit int) error {
|
2021-09-07 09:49:36 -06:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-01 07:40:17 -06:00
|
|
|
log.Trace("Doing: Update")
|
2021-06-14 11:20:43 -06:00
|
|
|
|
2021-11-22 20:09:35 -07:00
|
|
|
requested := 0
|
|
|
|
|
|
|
|
handler := func(idx int, bean interface{}, limit int) error {
|
2021-10-17 05:43:25 -06:00
|
|
|
var item SyncRequest
|
2022-02-08 14:28:21 -07:00
|
|
|
var repo *repo_model.Repository
|
2021-12-09 18:27:50 -07:00
|
|
|
if m, ok := bean.(*repo_model.Mirror); ok {
|
2021-06-14 11:20:43 -06:00
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
repo = m.Repo
|
2021-10-17 05:43:25 -06:00
|
|
|
item = SyncRequest{
|
2022-03-14 06:04:41 -06:00
|
|
|
Type: PullMirrorType,
|
|
|
|
ReferenceID: m.RepoID,
|
2021-10-17 05:43:25 -06:00
|
|
|
}
|
2021-12-09 18:27:50 -07:00
|
|
|
} else if m, ok := bean.(*repo_model.PushMirror); ok {
|
2021-06-14 11:20:43 -06:00
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected push-mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
repo = m.Repo
|
2021-10-17 05:43:25 -06:00
|
|
|
item = SyncRequest{
|
2022-03-14 06:04:41 -06:00
|
|
|
Type: PushMirrorType,
|
|
|
|
ReferenceID: m.ID,
|
2021-10-17 05:43:25 -06:00
|
|
|
}
|
2021-06-14 11:20:43 -06:00
|
|
|
} else {
|
|
|
|
log.Error("Unknown bean: %v", bean)
|
2019-10-01 07:40:17 -06:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-14 11:20:43 -06:00
|
|
|
|
2021-11-22 20:09:35 -07:00
|
|
|
// Check we've not been cancelled
|
2019-12-15 02:51:28 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-22 20:09:35 -07:00
|
|
|
return fmt.Errorf("aborted")
|
2019-12-15 02:51:28 -07:00
|
|
|
default:
|
|
|
|
}
|
2021-11-22 20:09:35 -07:00
|
|
|
|
|
|
|
// Push to the Queue
|
|
|
|
if err := mirrorQueue.Push(&item); err != nil {
|
2022-02-08 14:28:21 -07:00
|
|
|
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-22 20:09:35 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
requested++
|
|
|
|
if limit > 0 && requested > limit {
|
|
|
|
return errLimit
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-14 11:20:43 -06:00
|
|
|
}
|
|
|
|
|
2022-02-08 14:28:21 -07:00
|
|
|
pullMirrorsRequested := 0
|
2021-11-22 20:09:35 -07:00
|
|
|
if pullLimit != 0 {
|
2022-02-08 14:28:21 -07:00
|
|
|
requested = 0
|
2021-12-09 18:27:50 -07:00
|
|
|
if err := repo_model.MirrorsIterate(func(idx int, bean interface{}) error {
|
2021-11-22 20:09:35 -07:00
|
|
|
return handler(idx, bean, pullLimit)
|
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("MirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
pullMirrorsRequested, requested = requested, 0
|
2021-06-14 11:20:43 -06:00
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
pushMirrorsRequested := 0
|
2021-11-22 20:09:35 -07:00
|
|
|
if pushLimit != 0 {
|
2022-02-08 14:28:21 -07:00
|
|
|
requested = 0
|
2021-12-09 18:27:50 -07:00
|
|
|
if err := repo_model.PushMirrorsIterate(func(idx int, bean interface{}) error {
|
2021-11-22 20:09:35 -07:00
|
|
|
return handler(idx, bean, pushLimit)
|
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("PushMirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
pushMirrorsRequested, requested = requested, 0
|
2019-10-01 07:40:17 -06:00
|
|
|
}
|
2022-02-08 14:28:21 -07:00
|
|
|
log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
|
2020-05-16 17:31:38 -06:00
|
|
|
return nil
|
2019-10-01 07:40:17 -06:00
|
|
|
}
|
|
|
|
|
2021-10-17 05:43:25 -06:00
|
|
|
func queueHandle(data ...queue.Data) {
|
|
|
|
for _, datum := range data {
|
|
|
|
req := datum.(*SyncRequest)
|
|
|
|
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
|
2019-10-01 07:40:17 -06:00
|
|
|
}
|
2020-10-25 01:32:25 -06:00
|
|
|
}
|
|
|
|
|
2019-10-01 07:40:17 -06:00
|
|
|
// InitSyncMirrors initializes a go routine to sync the mirrors
|
|
|
|
func InitSyncMirrors() {
|
2021-09-07 09:49:36 -06:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 05:43:25 -06:00
|
|
|
mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest))
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run)
|
2019-10-01 07:40:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartToMirror adds repoID to mirror queue
|
|
|
|
func StartToMirror(repoID int64) {
|
2021-09-07 09:49:36 -06:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 05:43:25 -06:00
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
2022-03-14 06:04:41 -06:00
|
|
|
Type: PullMirrorType,
|
|
|
|
ReferenceID: repoID,
|
2021-10-17 05:43:25 -06:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to push sync request for to the queue for push mirror repo[%d]: Error: %v", repoID, err)
|
|
|
|
}
|
|
|
|
}()
|
2021-06-14 11:20:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPushMirrorToQueue adds the push mirror to the queue
|
|
|
|
func AddPushMirrorToQueue(mirrorID int64) {
|
2021-09-07 09:49:36 -06:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 05:43:25 -06:00
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
2022-03-14 06:04:41 -06:00
|
|
|
Type: PushMirrorType,
|
|
|
|
ReferenceID: mirrorID,
|
2021-10-17 05:43:25 -06:00
|
|
|
})
|
|
|
|
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 07:40:17 -06:00
|
|
|
}
|