2019-10-01 09:40:17 -04: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 04:51:28 -05:00
|
|
|
"context"
|
2019-10-01 09:40:17 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2020-01-13 22:38:04 -05:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-10-01 09:40:17 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-17 07:43:25 -04:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-10-01 09:40:17 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-10-17 07:43:25 -04: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 {
|
|
|
|
Type SyncType
|
|
|
|
RepoID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// doMirrorSync causes this request to mirror itself
|
|
|
|
func doMirrorSync(ctx context.Context, req *SyncRequest) {
|
|
|
|
switch req.Type {
|
|
|
|
case PushMirrorType:
|
|
|
|
_ = SyncPushMirror(ctx, req.RepoID)
|
|
|
|
case PullMirrorType:
|
|
|
|
_ = SyncPullMirror(ctx, req.RepoID)
|
|
|
|
default:
|
|
|
|
log.Error("Unknown Request type in queue: %v for RepoID[%d]", req.Type, req.RepoID)
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 09:40:17 -04:00
|
|
|
|
|
|
|
// Update checks and updates mirror repositories.
|
2020-05-16 19:31:38 -04:00
|
|
|
func Update(ctx context.Context) error {
|
2021-09-07 11:49:36 -04:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-01 09:40:17 -04:00
|
|
|
log.Trace("Doing: Update")
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
handler := func(idx int, bean interface{}) error {
|
2021-10-17 07:43:25 -04:00
|
|
|
var item SyncRequest
|
2021-06-14 13:20:43 -04:00
|
|
|
if m, ok := bean.(*models.Mirror); ok {
|
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
item = SyncRequest{
|
|
|
|
Type: PullMirrorType,
|
|
|
|
RepoID: m.RepoID,
|
|
|
|
}
|
2021-06-14 13:20:43 -04:00
|
|
|
} else if m, ok := bean.(*models.PushMirror); ok {
|
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected push-mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
item = SyncRequest{
|
|
|
|
Type: PushMirrorType,
|
|
|
|
RepoID: m.RepoID,
|
|
|
|
}
|
2021-06-14 13:20:43 -04:00
|
|
|
} else {
|
|
|
|
log.Error("Unknown bean: %v", bean)
|
2019-10-01 09:40:17 -04:00
|
|
|
return nil
|
|
|
|
}
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2019-12-15 04:51:28 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-05-16 19:31:38 -04:00
|
|
|
return fmt.Errorf("Aborted")
|
2019-12-15 04:51:28 -05:00
|
|
|
default:
|
2021-10-17 07:43:25 -04:00
|
|
|
return mirrorQueue.Push(&item)
|
2019-12-15 04:51:28 -05:00
|
|
|
}
|
2021-06-14 13:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := models.MirrorsIterate(handler); err != nil {
|
|
|
|
log.Error("MirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := models.PushMirrorsIterate(handler); err != nil {
|
|
|
|
log.Error("PushMirrorsIterate: %v", err)
|
2020-05-16 19:31:38 -04:00
|
|
|
return err
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
2020-05-16 19:31:38 -04:00
|
|
|
log.Trace("Finished: Update")
|
|
|
|
return nil
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
|
|
|
|
2021-10-17 07:43:25 -04:00
|
|
|
func queueHandle(data ...queue.Data) {
|
|
|
|
for _, datum := range data {
|
|
|
|
req := datum.(*SyncRequest)
|
|
|
|
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
2020-10-25 03:32:25 -04:00
|
|
|
}
|
|
|
|
|
2019-10-01 09:40:17 -04:00
|
|
|
// InitSyncMirrors initializes a go routine to sync the mirrors
|
|
|
|
func InitSyncMirrors() {
|
2021-09-07 11:49:36 -04:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest))
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run)
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartToMirror adds repoID to mirror queue
|
|
|
|
func StartToMirror(repoID int64) {
|
2021-09-07 11:49:36 -04:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
|
|
|
Type: PullMirrorType,
|
|
|
|
RepoID: repoID,
|
|
|
|
})
|
|
|
|
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 13:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPushMirrorToQueue adds the push mirror to the queue
|
|
|
|
func AddPushMirrorToQueue(mirrorID int64) {
|
2021-09-07 11:49:36 -04:00
|
|
|
if !setting.Mirror.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
go func() {
|
|
|
|
err := mirrorQueue.Push(&SyncRequest{
|
|
|
|
Type: PushMirrorType,
|
|
|
|
RepoID: mirrorID,
|
|
|
|
})
|
|
|
|
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 09:40:17 -04:00
|
|
|
}
|