mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	Refactor mirror code & fix StartToMirror (#18904)
				
					
				
			* Use MirrorID instead of RepoID - Use the MirrorID as index(SQL uses `id` column not the `repo_id`). Passes the Mirror ID's into the Sync functions. * Check for MirrorID == 0 * Fix `StartToMirror` + refactor * Update services/mirror/mirror.go Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		@@ -30,18 +30,22 @@ const (
 | 
				
			|||||||
// SyncRequest for the mirror queue
 | 
					// SyncRequest for the mirror queue
 | 
				
			||||||
type SyncRequest struct {
 | 
					type SyncRequest struct {
 | 
				
			||||||
	Type        SyncType
 | 
						Type        SyncType
 | 
				
			||||||
	RepoID int64
 | 
						ReferenceID int64 // RepoID for pull mirror, MirrorID fro push mirror
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// doMirrorSync causes this request to mirror itself
 | 
					// doMirrorSync causes this request to mirror itself
 | 
				
			||||||
func doMirrorSync(ctx context.Context, req *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 {
 | 
						switch req.Type {
 | 
				
			||||||
	case PushMirrorType:
 | 
						case PushMirrorType:
 | 
				
			||||||
		_ = SyncPushMirror(ctx, req.RepoID)
 | 
							_ = SyncPushMirror(ctx, req.ReferenceID)
 | 
				
			||||||
	case PullMirrorType:
 | 
						case PullMirrorType:
 | 
				
			||||||
		_ = SyncPullMirror(ctx, req.RepoID)
 | 
							_ = SyncPullMirror(ctx, req.ReferenceID)
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		log.Error("Unknown Request type in queue: %v for RepoID[%d]", req.Type, req.RepoID)
 | 
							log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -66,7 +70,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
 | 
				
			|||||||
			repo = m.Repo
 | 
								repo = m.Repo
 | 
				
			||||||
			item = SyncRequest{
 | 
								item = SyncRequest{
 | 
				
			||||||
				Type:        PullMirrorType,
 | 
									Type:        PullMirrorType,
 | 
				
			||||||
				RepoID: 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.Repo == nil {
 | 
								if m.Repo == nil {
 | 
				
			||||||
@@ -76,7 +80,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
 | 
				
			|||||||
			repo = m.Repo
 | 
								repo = m.Repo
 | 
				
			||||||
			item = SyncRequest{
 | 
								item = SyncRequest{
 | 
				
			||||||
				Type:        PushMirrorType,
 | 
									Type:        PushMirrorType,
 | 
				
			||||||
				RepoID: m.RepoID,
 | 
									ReferenceID: m.ID,
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			log.Error("Unknown bean: %v", bean)
 | 
								log.Error("Unknown bean: %v", bean)
 | 
				
			||||||
@@ -162,10 +166,11 @@ func StartToMirror(repoID int64) {
 | 
				
			|||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		err := mirrorQueue.Push(&SyncRequest{
 | 
							err := mirrorQueue.Push(&SyncRequest{
 | 
				
			||||||
			Type:        PullMirrorType,
 | 
								Type:        PullMirrorType,
 | 
				
			||||||
			RepoID: repoID,
 | 
								ReferenceID: repoID,
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Unable to push sync request for to the queue for push mirror repo[%d]: Error: %v", repoID, err)
 | 
								log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]: Error: %v", repoID, err)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -178,7 +183,7 @@ func AddPushMirrorToQueue(mirrorID int64) {
 | 
				
			|||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		err := mirrorQueue.Push(&SyncRequest{
 | 
							err := mirrorQueue.Push(&SyncRequest{
 | 
				
			||||||
			Type:        PushMirrorType,
 | 
								Type:        PushMirrorType,
 | 
				
			||||||
			RepoID: mirrorID,
 | 
								ReferenceID: mirrorID,
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Error("Unable to push sync request to the queue for pull mirror repo[%d]: Error: %v", mirrorID, err)
 | 
								log.Error("Unable to push sync request to the queue for pull mirror repo[%d]: Error: %v", mirrorID, err)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user