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"
|
2020-12-25 15:29:32 +05:30
|
|
|
"strconv"
|
2019-10-01 19:10:17 +05:30
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
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"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// mirrorQueue holds an UniqueQueue object of the mirror
|
|
|
|
var mirrorQueue = sync.NewUniqueQueue(setting.Repository.MirrorQueueLength)
|
|
|
|
|
|
|
|
// Update checks and updates mirror repositories.
|
2020-05-17 05:01:38 +05:30
|
|
|
func Update(ctx context.Context) 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
|
|
|
|
|
|
|
handler := func(idx int, bean interface{}) error {
|
|
|
|
var item string
|
|
|
|
if m, ok := bean.(*models.Mirror); ok {
|
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
item = fmt.Sprintf("pull %d", m.RepoID)
|
|
|
|
} else if m, ok := bean.(*models.PushMirror); ok {
|
|
|
|
if m.Repo == nil {
|
|
|
|
log.Error("Disconnected push-mirror found: %d", m.ID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
item = fmt.Sprintf("push %d", m.ID)
|
|
|
|
} 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
|
|
|
|
2019-12-15 15:21:28 +05:30
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-05-17 05:01:38 +05:30
|
|
|
return fmt.Errorf("Aborted")
|
2019-12-15 15:21:28 +05:30
|
|
|
default:
|
2021-06-14 22:50:43 +05:30
|
|
|
mirrorQueue.Add(item)
|
2019-12-15 15:21:28 +05:30
|
|
|
return nil
|
|
|
|
}
|
2021-06-14 22:50:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-17 05:01:38 +05:30
|
|
|
return err
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
2020-05-17 05:01:38 +05:30
|
|
|
log.Trace("Finished: Update")
|
|
|
|
return nil
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
|
|
|
|
2021-06-14 22:50:43 +05:30
|
|
|
// syncMirrors checks and syncs mirrors.
|
2019-12-15 15:21:28 +05:30
|
|
|
// FIXME: graceful: this should be a persistable queue
|
2021-06-14 22:50:43 +05:30
|
|
|
func syncMirrors(ctx context.Context) {
|
2019-10-01 19:10:17 +05:30
|
|
|
// Start listening on new sync requests.
|
2019-12-15 15:21:28 +05:30
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
mirrorQueue.Close()
|
|
|
|
return
|
2021-06-14 22:50:43 +05:30
|
|
|
case item := <-mirrorQueue.Queue():
|
|
|
|
id, _ := strconv.ParseInt(item[5:], 10, 64)
|
|
|
|
if strings.HasPrefix(item, "pull") {
|
|
|
|
_ = SyncPullMirror(ctx, id)
|
|
|
|
} else if strings.HasPrefix(item, "push") {
|
|
|
|
_ = SyncPushMirror(ctx, id)
|
|
|
|
} else {
|
|
|
|
log.Error("Unknown item in queue: %v", item)
|
2020-12-05 20:43:11 +05:30
|
|
|
}
|
2021-06-14 22:50:43 +05:30
|
|
|
mirrorQueue.Remove(item)
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|
|
|
|
}
|
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-06-14 22:50:43 +05:30
|
|
|
go graceful.GetManager().RunWithShutdownContext(syncMirrors)
|
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-06-14 22:50:43 +05:30
|
|
|
go mirrorQueue.Add(fmt.Sprintf("pull %d", repoID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-06-14 22:50:43 +05:30
|
|
|
go mirrorQueue.Add(fmt.Sprintf("push %d", mirrorID))
|
2019-10-01 19:10:17 +05:30
|
|
|
}
|