forked from forgejo/forgejo
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:
parent
8bea92c3dc
commit
e3c3b33ea7
37 changed files with 628 additions and 287 deletions
|
@ -4,6 +4,12 @@
|
|||
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
// RepoIndexerStatus status of a repo's entry in the repo indexer
|
||||
// For now, implicitly refers to default branch
|
||||
type RepoIndexerStatus struct {
|
||||
|
@ -12,6 +18,31 @@ type RepoIndexerStatus struct {
|
|||
CommitSha string `xorm:"VARCHAR(40)"`
|
||||
}
|
||||
|
||||
// GetUnindexedRepos returns repos which do not have an indexer status
|
||||
func GetUnindexedRepos(maxRepoID int64, page, pageSize int) ([]int64, error) {
|
||||
ids := make([]int64, 0, 50)
|
||||
cond := builder.Cond(builder.IsNull{
|
||||
"repo_indexer_status.id",
|
||||
})
|
||||
sess := x.Table("repository").Join("LEFT OUTER", "repo_indexer_status", "repository.id = repo_indexer_status.repo_id")
|
||||
if maxRepoID > 0 {
|
||||
cond = builder.And(cond, builder.Lte{
|
||||
"repository.id": maxRepoID,
|
||||
})
|
||||
}
|
||||
if page >= 0 && pageSize > 0 {
|
||||
start := 0
|
||||
if page > 0 {
|
||||
start = (page - 1) * pageSize
|
||||
}
|
||||
sess.Limit(pageSize, start)
|
||||
}
|
||||
|
||||
sess.Where(cond).Cols("repository.id").Desc("repository.id")
|
||||
err := sess.Find(&ids)
|
||||
return ids, err
|
||||
}
|
||||
|
||||
// GetIndexerStatus loads repo codes indxer status
|
||||
func (repo *Repository) GetIndexerStatus() error {
|
||||
if repo.IndexerStatus != nil {
|
||||
|
@ -31,15 +62,21 @@ func (repo *Repository) GetIndexerStatus() error {
|
|||
// UpdateIndexerStatus updates indexer status
|
||||
func (repo *Repository) UpdateIndexerStatus(sha string) error {
|
||||
if err := repo.GetIndexerStatus(); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("UpdateIndexerStatus: Unable to getIndexerStatus for repo: %s/%s Error: %v", repo.MustOwnerName(), repo.Name, err)
|
||||
}
|
||||
if len(repo.IndexerStatus.CommitSha) == 0 {
|
||||
repo.IndexerStatus.CommitSha = sha
|
||||
_, err := x.Insert(repo.IndexerStatus)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("UpdateIndexerStatus: Unable to insert repoIndexerStatus for repo: %s/%s Sha: %s Error: %v", repo.MustOwnerName(), repo.Name, sha, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
repo.IndexerStatus.CommitSha = sha
|
||||
_, err := x.ID(repo.IndexerStatus.ID).Cols("commit_sha").
|
||||
Update(repo.IndexerStatus)
|
||||
return err
|
||||
if err != nil {
|
||||
return fmt.Errorf("UpdateIndexerStatus: Unable to update repoIndexerStatus for repo: %s/%s Sha: %s Error: %v", repo.MustOwnerName(), repo.Name, sha, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue