Move `modules/mirror` to `services` (#26737)

To solve the cyclic imports in a better way

Closes #20261
This commit is contained in:
Chongyi Zheng 2023-08-26 22:24:45 -04:00 committed by GitHub
parent 37b3ba22a1
commit 43652746f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 39 deletions

View File

@ -128,7 +128,7 @@ func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.Li
func GetPushMirrorsSyncedOnCommit(ctx context.Context, repoID int64) ([]*PushMirror, error) {
mirrors := make([]*PushMirror, 0, 10)
return mirrors, db.GetEngine(ctx).
Where("repo_id=? AND sync_on_commit=?", repoID, true).
Where("repo_id = ? AND sync_on_commit = ?", repoID, true).
Find(&mirrors)
}

View File

@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/notification/indexer"
"code.gitea.io/gitea/modules/notification/mail"
"code.gitea.io/gitea/modules/notification/mirror"
"code.gitea.io/gitea/modules/notification/ui"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
@ -38,7 +37,6 @@ func NewContext() {
}
RegisterNotifier(indexer.NewNotifier())
RegisterNotifier(action.NewNotifier())
RegisterNotifier(mirror.NewNotifier())
}
// NotifyNewWikiPage notifies creating new wiki pages to notifiers

View File

@ -14,7 +14,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/context"
mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
@ -70,7 +69,7 @@ func MirrorSync(ctx *context.APIContext) {
return
}
mirror_module.AddPullMirrorToQueue(repo.ID)
mirror_service.AddPullMirrorToQueue(repo.ID)
ctx.Status(http.StatusOK)
}

View File

@ -24,7 +24,6 @@ import (
"code.gitea.io/gitea/modules/indexer/stats"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
mirror_module "code.gitea.io/gitea/modules/mirror"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
@ -277,7 +276,7 @@ func SettingsPost(ctx *context.Context) {
return
}
mirror_module.AddPullMirrorToQueue(repo.ID)
mirror_service.AddPullMirrorToQueue(repo.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings")
@ -294,7 +293,7 @@ func SettingsPost(ctx *context.Context) {
return
}
mirror_module.AddPushMirrorToQueue(m.ID)
mirror_service.AddPushMirrorToQueue(m.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings")
@ -332,7 +331,7 @@ func SettingsPost(ctx *context.Context) {
// If we observed its implementation in the context of `push-mirror-sync` where it
// is evident that pushing to the queue is necessary for updates.
// So, there are updates within the given interval, it is necessary to update the queue accordingly.
mirror_module.AddPushMirrorToQueue(m.ID)
mirror_service.AddPushMirrorToQueue(m.ID)
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
ctx.Redirect(repo.Link() + "/settings")

View File

@ -10,21 +10,20 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
)
// doMirrorSync causes this request to mirror itself
func doMirrorSync(ctx context.Context, req *mirror_module.SyncRequest) {
func doMirrorSync(ctx context.Context, req *SyncRequest) {
if req.ReferenceID == 0 {
log.Warn("Skipping mirror sync request, no mirror ID was specified")
return
}
switch req.Type {
case mirror_module.PushMirrorType:
case PushMirrorType:
_ = SyncPushMirror(ctx, req.ReferenceID)
case mirror_module.PullMirrorType:
case PullMirrorType:
_ = SyncPullMirror(ctx, req.ReferenceID)
default:
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
@ -43,7 +42,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
handler := func(idx int, bean any) error {
var repo *repo_model.Repository
var mirrorType mirror_module.SyncType
var mirrorType SyncType
var referenceID int64
if m, ok := bean.(*repo_model.Mirror); ok {
@ -52,7 +51,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
repo = m.Repo
mirrorType = mirror_module.PullMirrorType
mirrorType = PullMirrorType
referenceID = m.RepoID
} else if m, ok := bean.(*repo_model.PushMirror); ok {
if m.GetRepository() == nil {
@ -60,7 +59,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
repo = m.Repo
mirrorType = mirror_module.PushMirrorType
mirrorType = PushMirrorType
referenceID = m.ID
} else {
log.Error("Unknown bean: %v", bean)
@ -75,9 +74,9 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
}
// Push to the Queue
if err := mirror_module.PushToQueue(mirrorType, referenceID); err != nil {
if err := PushToQueue(mirrorType, referenceID); err != nil {
if err == queue.ErrAlreadyInQueue {
if mirrorType == mirror_module.PushMirrorType {
if mirrorType == PushMirrorType {
log.Trace("PushMirrors for %-v already queued for sync", repo)
} else {
log.Trace("PullMirrors for %-v already queued for sync", repo)
@ -120,7 +119,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil
}
func queueHandler(items ...*mirror_module.SyncRequest) []*mirror_module.SyncRequest {
func queueHandler(items ...*SyncRequest) []*SyncRequest {
for _, req := range items {
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
}
@ -129,5 +128,5 @@ func queueHandler(items ...*mirror_module.SyncRequest) []*mirror_module.SyncRequ
// InitSyncMirrors initializes a go routine to sync the mirrors
func InitSyncMirrors() {
mirror_module.StartSyncMirrors(queueHandler)
StartSyncMirrors(queueHandler)
}

View File

@ -253,3 +253,15 @@ func pushAllLFSObjects(ctx context.Context, gitRepo *git.Repository, lfsClient l
return nil
}
func syncPushMirrorWithSyncOnCommit(ctx context.Context, repoID int64) {
pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(ctx, repoID)
if err != nil {
log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
return
}
for _, mirror := range pushMirrors {
AddPushMirrorToQueue(mirror.ID)
}
}

View File

@ -8,23 +8,21 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/repository"
)
func init() {
notification.RegisterNotifier(&mirrorNotifier{})
}
type mirrorNotifier struct {
base.NullNotifier
}
var _ base.Notifier = &mirrorNotifier{}
// NewNotifier create a new mirrorNotifier notifier
func NewNotifier() base.Notifier {
return &mirrorNotifier{}
}
func (m *mirrorNotifier) NotifyPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
}
@ -32,15 +30,3 @@ func (m *mirrorNotifier) NotifyPushCommits(ctx context.Context, _ *user_model.Us
func (m *mirrorNotifier) NotifySyncPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
}
func syncPushMirrorWithSyncOnCommit(ctx context.Context, repoID int64) {
pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(ctx, repoID)
if err != nil {
log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
return
}
for _, mirror := range pushMirrors {
mirror_module.AddPushMirrorToQueue(mirror.ID)
}
}