mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Graceful: Xorm, RepoIndexer, Cron and Others (#9282)
* Change graceful to use a singleton obtained through GetManager instead of a global. * Graceful: Make TestPullRequests shutdownable * Graceful: Make the cron tasks graceful * Graceful: AddTestPullRequest run in graceful ctx * Graceful: SyncMirrors shutdown * Graceful: SetDefaultContext for Xorm to be HammerContext * Avoid starting graceful for migrate commands and checkout * Graceful: DeliverHooks now can be shutdown * Fix multiple syncing errors in modules/sync/UniqueQueue & Make UniqueQueue closable * Begin the process of making the repo indexer shutdown gracefully
This commit is contained in:
		@@ -5,11 +5,14 @@
 | 
			
		||||
package mirror
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/graceful"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/cache"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
@@ -294,29 +297,38 @@ func Password(m *models.Mirror) string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Update checks and updates mirror repositories.
 | 
			
		||||
func Update() {
 | 
			
		||||
func Update(ctx context.Context) {
 | 
			
		||||
	log.Trace("Doing: Update")
 | 
			
		||||
 | 
			
		||||
	if err := models.MirrorsIterate(func(idx int, bean interface{}) error {
 | 
			
		||||
		m := bean.(*models.Mirror)
 | 
			
		||||
		if m.Repo == nil {
 | 
			
		||||
			log.Error("Disconnected mirror repository found: %d", m.ID)
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		mirrorQueue.Add(m.RepoID)
 | 
			
		||||
		return nil
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ctx.Done():
 | 
			
		||||
			return fmt.Errorf("Aborted due to shutdown")
 | 
			
		||||
		default:
 | 
			
		||||
			mirrorQueue.Add(m.RepoID)
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		log.Error("Update: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SyncMirrors checks and syncs mirrors.
 | 
			
		||||
// TODO: sync more mirrors at same time.
 | 
			
		||||
func SyncMirrors() {
 | 
			
		||||
// FIXME: graceful: this should be a persistable queue
 | 
			
		||||
func SyncMirrors(ctx context.Context) {
 | 
			
		||||
	// Start listening on new sync requests.
 | 
			
		||||
	for repoID := range mirrorQueue.Queue() {
 | 
			
		||||
		syncMirror(repoID)
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ctx.Done():
 | 
			
		||||
			mirrorQueue.Close()
 | 
			
		||||
			return
 | 
			
		||||
		case repoID := <-mirrorQueue.Queue():
 | 
			
		||||
			syncMirror(repoID)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -416,7 +428,7 @@ func syncMirror(repoID string) {
 | 
			
		||||
 | 
			
		||||
// InitSyncMirrors initializes a go routine to sync the mirrors
 | 
			
		||||
func InitSyncMirrors() {
 | 
			
		||||
	go SyncMirrors()
 | 
			
		||||
	go graceful.GetManager().RunWithShutdownContext(SyncMirrors)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StartToMirror adds repoID to mirror queue
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user