diff options
Diffstat (limited to 'services/mirror/mirror.go')
-rw-r--r-- | services/mirror/mirror.go | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index 1ad9448b6b..7fc6e97b46 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -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 |