1
0
Fork 0

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

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

View file

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

View file

@ -24,7 +24,6 @@ import (
"code.gitea.io/gitea/modules/indexer/stats" "code.gitea.io/gitea/modules/indexer/stats"
"code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
mirror_module "code.gitea.io/gitea/modules/mirror"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/structs"
@ -277,7 +276,7 @@ func SettingsPost(ctx *context.Context) {
return return
} }
mirror_module.AddPullMirrorToQueue(repo.ID) mirror_service.AddPullMirrorToQueue(repo.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress")) ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings") ctx.Redirect(repo.Link() + "/settings")
@ -294,7 +293,7 @@ func SettingsPost(ctx *context.Context) {
return return
} }
mirror_module.AddPushMirrorToQueue(m.ID) mirror_service.AddPushMirrorToQueue(m.ID)
ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress")) ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
ctx.Redirect(repo.Link() + "/settings") 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 // 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. // 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. // 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.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
ctx.Redirect(repo.Link() + "/settings") ctx.Redirect(repo.Link() + "/settings")

View file

@ -10,21 +10,20 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log" "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/queue"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
) )
// doMirrorSync causes this request to mirror itself // 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 { if req.ReferenceID == 0 {
log.Warn("Skipping mirror sync request, no mirror ID was specified") log.Warn("Skipping mirror sync request, no mirror ID was specified")
return return
} }
switch req.Type { switch req.Type {
case mirror_module.PushMirrorType: case PushMirrorType:
_ = SyncPushMirror(ctx, req.ReferenceID) _ = SyncPushMirror(ctx, req.ReferenceID)
case mirror_module.PullMirrorType: case PullMirrorType:
_ = SyncPullMirror(ctx, req.ReferenceID) _ = SyncPullMirror(ctx, req.ReferenceID)
default: default:
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID) 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 { handler := func(idx int, bean any) error {
var repo *repo_model.Repository var repo *repo_model.Repository
var mirrorType mirror_module.SyncType var mirrorType SyncType
var referenceID int64 var referenceID int64
if m, ok := bean.(*repo_model.Mirror); ok { if m, ok := bean.(*repo_model.Mirror); ok {
@ -52,7 +51,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil return nil
} }
repo = m.Repo repo = m.Repo
mirrorType = mirror_module.PullMirrorType mirrorType = PullMirrorType
referenceID = m.RepoID referenceID = m.RepoID
} else if m, ok := bean.(*repo_model.PushMirror); ok { } else if m, ok := bean.(*repo_model.PushMirror); ok {
if m.GetRepository() == nil { if m.GetRepository() == nil {
@ -60,7 +59,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
return nil return nil
} }
repo = m.Repo repo = m.Repo
mirrorType = mirror_module.PushMirrorType mirrorType = PushMirrorType
referenceID = m.ID referenceID = m.ID
} else { } else {
log.Error("Unknown bean: %v", bean) log.Error("Unknown bean: %v", bean)
@ -75,9 +74,9 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
} }
// Push to the Queue // 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 err == queue.ErrAlreadyInQueue {
if mirrorType == mirror_module.PushMirrorType { if mirrorType == PushMirrorType {
log.Trace("PushMirrors for %-v already queued for sync", repo) log.Trace("PushMirrors for %-v already queued for sync", repo)
} else { } else {
log.Trace("PullMirrors for %-v already queued for sync", repo) 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 return nil
} }
func queueHandler(items ...*mirror_module.SyncRequest) []*mirror_module.SyncRequest { func queueHandler(items ...*SyncRequest) []*SyncRequest {
for _, req := range items { for _, req := range items {
doMirrorSync(graceful.GetManager().ShutdownContext(), req) 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 // InitSyncMirrors initializes a go routine to sync the mirrors
func InitSyncMirrors() { 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 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" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification"
mirror_module "code.gitea.io/gitea/modules/mirror"
"code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/notification/base"
"code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/repository"
) )
func init() {
notification.RegisterNotifier(&mirrorNotifier{})
}
type mirrorNotifier struct { type mirrorNotifier struct {
base.NullNotifier base.NullNotifier
} }
var _ base.Notifier = &mirrorNotifier{} 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) { func (m *mirrorNotifier) NotifyPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID) 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) { func (m *mirrorNotifier) NotifySyncPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
syncPushMirrorWithSyncOnCommit(ctx, repo.ID) 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)
}
}