From 43652746f2929aeb53ec1e575c3691050755a0b5 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Sat, 26 Aug 2023 22:24:45 -0400 Subject: [PATCH] Move `modules/mirror` to `services` (#26737) To solve the cyclic imports in a better way Closes #20261 --- models/repo/pushmirror.go | 2 +- modules/notification/notification.go | 2 -- routers/api/v1/repo/mirror.go | 3 +-- routers/web/repo/setting/setting.go | 7 +++--- services/mirror/mirror.go | 21 ++++++++-------- services/mirror/mirror_push.go | 12 ++++++++++ .../mirror.go => services/mirror/notifier.go | 24 ++++--------------- .../mirror.go => services/mirror/queue.go | 0 8 files changed, 32 insertions(+), 39 deletions(-) rename modules/notification/mirror/mirror.go => services/mirror/notifier.go (61%) rename modules/mirror/mirror.go => services/mirror/queue.go (100%) diff --git a/models/repo/pushmirror.go b/models/repo/pushmirror.go index dad9a9d388..3edbcceb9b 100644 --- a/models/repo/pushmirror.go +++ b/models/repo/pushmirror.go @@ -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) } diff --git a/modules/notification/notification.go b/modules/notification/notification.go index 99e1a06ebd..50b2694d9c 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -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 diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index 9d8497927e..60f1bfe0d3 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -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) } diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 71c1939f29..af09e240d5 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -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") diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index abce1d3c2d..0fc871b214 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -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) } diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index 2c1b00b60c..594d31df89 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -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) + } +} diff --git a/modules/notification/mirror/mirror.go b/services/mirror/notifier.go similarity index 61% rename from modules/notification/mirror/mirror.go rename to services/mirror/notifier.go index 426e36ee58..e0e1b443e0 100644 --- a/modules/notification/mirror/mirror.go +++ b/services/mirror/notifier.go @@ -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) - } -} diff --git a/modules/mirror/mirror.go b/services/mirror/queue.go similarity index 100% rename from modules/mirror/mirror.go rename to services/mirror/queue.go