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