2019-10-01 09:40:17 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-01 09:40:17 -04:00
|
|
|
|
|
|
|
package mirror
|
|
|
|
|
|
|
|
import (
|
2019-12-15 04:51:28 -05:00
|
|
|
"context"
|
2019-10-01 09:40:17 -04:00
|
|
|
"fmt"
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
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"
|
2022-07-08 15:45:12 -04:00
|
|
|
mirror_module "code.gitea.io/gitea/modules/mirror"
|
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
|
|
|
// doMirrorSync causes this request to mirror itself
|
2022-07-08 15:45:12 -04:00
|
|
|
func doMirrorSync(ctx context.Context, req *mirror_module.SyncRequest) {
|
2022-03-02 02:43:11 -05:00
|
|
|
if req.ReferenceID == 0 {
|
|
|
|
log.Warn("Skipping mirror sync request, no mirror ID was specified")
|
|
|
|
return
|
|
|
|
}
|
2021-10-17 07:43:25 -04:00
|
|
|
switch req.Type {
|
2022-07-08 15:45:12 -04:00
|
|
|
case mirror_module.PushMirrorType:
|
2022-03-02 02:43:11 -05:00
|
|
|
_ = SyncPushMirror(ctx, req.ReferenceID)
|
2022-07-08 15:45:12 -04:00
|
|
|
case mirror_module.PullMirrorType:
|
2022-03-02 02:43:11 -05:00
|
|
|
_ = SyncPullMirror(ctx, req.ReferenceID)
|
2021-10-17 07:43:25 -04:00
|
|
|
default:
|
2022-03-02 02:43:11 -05:00
|
|
|
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
|
2021-10-17 07:43:25 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-01 09:40:17 -04:00
|
|
|
|
2021-11-22 22:09:35 -05:00
|
|
|
var errLimit = fmt.Errorf("reached limit")
|
|
|
|
|
2019-10-01 09:40:17 -04:00
|
|
|
// Update checks and updates mirror repositories.
|
2021-11-22 22:09:35 -05:00
|
|
|
func Update(ctx context.Context, pullLimit, pushLimit int) 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
|
|
|
|
2022-02-28 14:41:06 -05:00
|
|
|
handler := func(idx int, bean interface{}) error {
|
2022-02-08 09:02:32 -05:00
|
|
|
var repo *repo_model.Repository
|
2022-07-08 15:45:12 -04:00
|
|
|
var mirrorType mirror_module.SyncType
|
|
|
|
var referenceID int64
|
|
|
|
|
2021-12-09 20:27:50 -05:00
|
|
|
if m, ok := bean.(*repo_model.Mirror); ok {
|
2022-06-15 11:58:44 -04:00
|
|
|
if m.GetRepository() == nil {
|
2021-06-14 13:20:43 -04:00
|
|
|
log.Error("Disconnected mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 09:02:32 -05:00
|
|
|
repo = m.Repo
|
2022-07-08 15:45:12 -04:00
|
|
|
mirrorType = mirror_module.PullMirrorType
|
|
|
|
referenceID = m.RepoID
|
2021-12-09 20:27:50 -05:00
|
|
|
} else if m, ok := bean.(*repo_model.PushMirror); ok {
|
2022-06-15 11:58:44 -04:00
|
|
|
if m.GetRepository() == nil {
|
2021-06-14 13:20:43 -04:00
|
|
|
log.Error("Disconnected push-mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 09:02:32 -05:00
|
|
|
repo = m.Repo
|
2022-07-08 15:45:12 -04:00
|
|
|
mirrorType = mirror_module.PushMirrorType
|
|
|
|
referenceID = m.ID
|
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
|
|
|
|
2021-11-22 22:09:35 -05:00
|
|
|
// Check we've not been cancelled
|
2019-12-15 04:51:28 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-22 22:09:35 -05:00
|
|
|
return fmt.Errorf("aborted")
|
2019-12-15 04:51:28 -05:00
|
|
|
default:
|
|
|
|
}
|
2021-11-22 22:09:35 -05:00
|
|
|
|
|
|
|
// Push to the Queue
|
2022-07-08 15:45:12 -04:00
|
|
|
if err := mirror_module.PushToQueue(mirrorType, referenceID); err != nil {
|
2022-02-08 09:02:32 -05:00
|
|
|
if err == queue.ErrAlreadyInQueue {
|
2022-07-08 15:45:12 -04:00
|
|
|
if mirrorType == mirror_module.PushMirrorType {
|
2022-02-08 09:02:32 -05:00
|
|
|
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 22:09:35 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-14 13:20:43 -04:00
|
|
|
}
|
|
|
|
|
2022-02-08 09:02:32 -05:00
|
|
|
pullMirrorsRequested := 0
|
2021-11-22 22:09:35 -05:00
|
|
|
if pullLimit != 0 {
|
2022-02-28 14:41:06 -05:00
|
|
|
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-22 22:09:35 -05:00
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("MirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-14 13:20:43 -04:00
|
|
|
}
|
2022-02-28 14:41:06 -05:00
|
|
|
|
2022-02-08 09:02:32 -05:00
|
|
|
pushMirrorsRequested := 0
|
2021-11-22 22:09:35 -05:00
|
|
|
if pushLimit != 0 {
|
2022-07-30 12:45:59 -04:00
|
|
|
if err := repo_model.PushMirrorsIterate(ctx, pushLimit, func(idx int, bean interface{}) error {
|
2022-02-28 14:41:06 -05:00
|
|
|
if err := handler(idx, bean); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pushMirrorsRequested++
|
|
|
|
return nil
|
2021-11-22 22:09:35 -05:00
|
|
|
}); err != nil && err != errLimit {
|
|
|
|
log.Error("PushMirrorsIterate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
2022-02-08 09:02:32 -05:00
|
|
|
log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
|
2020-05-16 19:31:38 -04:00
|
|
|
return nil
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:22:14 -05:00
|
|
|
func queueHandle(data ...queue.Data) []queue.Data {
|
2021-10-17 07:43:25 -04:00
|
|
|
for _, datum := range data {
|
2022-07-08 15:45:12 -04:00
|
|
|
req := datum.(*mirror_module.SyncRequest)
|
2021-10-17 07:43:25 -04:00
|
|
|
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|
2022-01-22 16:22:14 -05:00
|
|
|
return nil
|
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() {
|
2022-07-08 15:45:12 -04:00
|
|
|
mirror_module.StartSyncMirrors(queueHandle)
|
2019-10-01 09:40:17 -04:00
|
|
|
}
|