code.gitea.io/gitea@v1.22.3/modules/indexer/stats/queue.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package stats
     5  
     6  import (
     7  	"fmt"
     8  
     9  	repo_model "code.gitea.io/gitea/models/repo"
    10  	"code.gitea.io/gitea/modules/graceful"
    11  	"code.gitea.io/gitea/modules/log"
    12  	"code.gitea.io/gitea/modules/queue"
    13  	"code.gitea.io/gitea/modules/setting"
    14  )
    15  
    16  // statsQueue represents a queue to handle repository stats updates
    17  var statsQueue *queue.WorkerPoolQueue[int64]
    18  
    19  // handle passed PR IDs and test the PRs
    20  func handler(items ...int64) []int64 {
    21  	for _, opts := range items {
    22  		if err := indexer.Index(opts); err != nil {
    23  			if !setting.IsInTesting {
    24  				log.Error("stats queue indexer.Index(%d) failed: %v", opts, err)
    25  			}
    26  		}
    27  	}
    28  	return nil
    29  }
    30  
    31  func initStatsQueue() error {
    32  	statsQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "repo_stats_update", handler)
    33  	if statsQueue == nil {
    34  		return fmt.Errorf("unable to create repo_stats_update queue")
    35  	}
    36  	go graceful.GetManager().RunWithCancel(statsQueue)
    37  	return nil
    38  }
    39  
    40  // UpdateRepoIndexer update a repository's entries in the indexer
    41  func UpdateRepoIndexer(repo *repo_model.Repository) error {
    42  	if err := statsQueue.Push(repo.ID); err != nil {
    43  		if err != queue.ErrAlreadyInQueue {
    44  			return err
    45  		}
    46  		log.Debug("Repo ID: %d already queued", repo.ID)
    47  	}
    48  	return nil
    49  }