|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package release |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + repo_model "code.gitea.io/gitea/models/repo" |
| 13 | + "code.gitea.io/gitea/modules/graceful" |
| 14 | + "code.gitea.io/gitea/modules/log" |
| 15 | + "code.gitea.io/gitea/modules/queue" |
| 16 | + repo_module "code.gitea.io/gitea/modules/repository" |
| 17 | + |
| 18 | + "xorm.io/builder" |
| 19 | +) |
| 20 | + |
| 21 | +type TagSyncOptions struct { |
| 22 | + RepoID int64 |
| 23 | +} |
| 24 | + |
| 25 | +// tagSyncQueue represents a queue to handle tag sync jobs. |
| 26 | +var tagSyncQueue *queue.WorkerPoolQueue[*TagSyncOptions] |
| 27 | + |
| 28 | +func handlerTagSync(items ...*TagSyncOptions) []*TagSyncOptions { |
| 29 | + for _, opts := range items { |
| 30 | + err := repo_module.SyncRepoTags(graceful.GetManager().ShutdownContext(), opts.RepoID) |
| 31 | + if err != nil { |
| 32 | + log.Error("syncRepoTags [%d] failed: %v", opts.RepoID, err) |
| 33 | + } |
| 34 | + } |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func addRepoToTagSyncQueue(repoID int64) error { |
| 39 | + return tagSyncQueue.Push(&TagSyncOptions{ |
| 40 | + RepoID: repoID, |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +func initTagSyncQueue(ctx context.Context) error { |
| 45 | + tagSyncQueue = queue.CreateUniqueQueue(ctx, "tag_sync", handlerTagSync) |
| 46 | + if tagSyncQueue == nil { |
| 47 | + return errors.New("unable to create tag_sync queue") |
| 48 | + } |
| 49 | + go graceful.GetManager().RunWithCancel(tagSyncQueue) |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func AddAllRepoTagsToSyncQueue(ctx context.Context) error { |
| 55 | + if err := db.Iterate(ctx, builder.Eq{"is_empty": false}, func(ctx context.Context, repo *repo_model.Repository) error { |
| 56 | + return addRepoToTagSyncQueue(repo.ID) |
| 57 | + }); err != nil { |
| 58 | + return fmt.Errorf("run sync all tags failed: %v", err) |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
0 commit comments