2017-01-24 19:43:02 -07:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-12-10 06:29:40 -07:00
|
|
|
package code
|
2017-01-24 19:43:02 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-24 00:26:34 -07:00
|
|
|
"context"
|
|
|
|
"os"
|
2019-12-23 05:31:16 -07:00
|
|
|
"time"
|
2017-09-16 14:16:21 -06:00
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-05 11:29:17 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-02-20 12:53:55 -07:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-12-23 05:31:16 -07:00
|
|
|
)
|
2018-02-05 11:29:17 -07:00
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
// SearchResult result of performing a search in a repo
|
|
|
|
type SearchResult struct {
|
2020-02-20 12:53:55 -07:00
|
|
|
RepoID int64
|
|
|
|
StartIndex int
|
|
|
|
EndIndex int
|
|
|
|
Filename string
|
|
|
|
Content string
|
|
|
|
CommitID string
|
|
|
|
UpdatedUnix timeutil.TimeStamp
|
|
|
|
Language string
|
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchResultLanguages result of top languages count in search results
|
|
|
|
type SearchResultLanguages struct {
|
|
|
|
Language string
|
|
|
|
Color string
|
|
|
|
Count int
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
// Indexer defines an interface to indexer issues contents
|
|
|
|
type Indexer interface {
|
|
|
|
Index(repoID int64) error
|
|
|
|
Delete(repoID int64) error
|
2020-02-20 12:53:55 -07:00
|
|
|
Search(repoIDs []int64, language, keyword string, page, pageSize int) (int64, []*SearchResult, []*SearchResultLanguages, error)
|
2019-12-23 05:31:16 -07:00
|
|
|
Close()
|
2017-09-16 14:16:21 -06:00
|
|
|
}
|
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
2019-12-24 00:26:34 -07:00
|
|
|
indexer.Close()
|
2019-12-23 05:31:16 -07:00
|
|
|
return
|
|
|
|
}
|
2017-09-24 18:08:48 -06:00
|
|
|
|
2019-12-25 02:44:09 -07:00
|
|
|
initQueue(setting.Indexer.UpdateQueueLength)
|
|
|
|
|
2019-12-24 00:26:34 -07:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
graceful.GetManager().RunAtTerminate(ctx, func() {
|
|
|
|
log.Debug("Closing repository indexer")
|
|
|
|
indexer.Close()
|
|
|
|
log.Info("PID: %d Repository Indexer closed", os.Getpid())
|
|
|
|
})
|
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
waitChannel := make(chan time.Duration)
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
2019-12-24 00:26:34 -07:00
|
|
|
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
|
2020-02-28 15:00:09 -07:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error("PANIC whilst initializing repository indexer: %v\nStacktrace: %s", err, log.Stack(2))
|
|
|
|
log.Error("The indexer files are likely corrupted and may need to be deleted")
|
2020-04-22 14:16:58 -06:00
|
|
|
log.Error("You can completely remove the %q directory to make Gitea recreate the indexes", setting.Indexer.RepoPath)
|
2020-02-28 15:00:09 -07:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
|
|
|
close(waitChannel)
|
|
|
|
log.Fatal("PID: %d Unable to initialize the Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-12-24 00:26:34 -07:00
|
|
|
bleveIndexer, created, err := NewBleveIndexer(setting.Indexer.RepoPath)
|
2019-12-23 05:31:16 -07:00
|
|
|
if err != nil {
|
2019-12-24 00:26:34 -07:00
|
|
|
if bleveIndexer != nil {
|
|
|
|
bleveIndexer.Close()
|
|
|
|
}
|
|
|
|
cancel()
|
2019-12-23 05:31:16 -07:00
|
|
|
indexer.Close()
|
2019-12-24 00:26:34 -07:00
|
|
|
close(waitChannel)
|
|
|
|
log.Fatal("PID: %d Unable to initialize the Repository Indexer at path: %s Error: %v", os.Getpid(), setting.Indexer.RepoPath, err)
|
2019-12-23 05:31:16 -07:00
|
|
|
}
|
2019-12-24 00:26:34 -07:00
|
|
|
indexer.set(bleveIndexer)
|
2017-09-24 18:08:48 -06:00
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
go processRepoIndexerOperationQueue(indexer)
|
2017-09-24 18:08:48 -06:00
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
if created {
|
|
|
|
go populateRepoIndexer()
|
|
|
|
}
|
2019-12-24 00:26:34 -07:00
|
|
|
select {
|
|
|
|
case waitChannel <- time.Since(start):
|
|
|
|
case <-graceful.GetManager().IsShutdown():
|
|
|
|
}
|
2017-09-24 18:08:48 -06:00
|
|
|
|
2019-12-24 00:26:34 -07:00
|
|
|
close(waitChannel)
|
2019-12-23 05:31:16 -07:00
|
|
|
}()
|
2017-09-24 18:08:48 -06:00
|
|
|
|
2019-12-23 05:31:16 -07:00
|
|
|
if setting.Indexer.StartupTimeout > 0 {
|
|
|
|
go func() {
|
|
|
|
timeout := setting.Indexer.StartupTimeout
|
|
|
|
if graceful.GetManager().IsChild() && setting.GracefulHammerTime > 0 {
|
|
|
|
timeout += setting.GracefulHammerTime
|
|
|
|
}
|
|
|
|
select {
|
2019-12-24 00:26:34 -07:00
|
|
|
case <-graceful.GetManager().IsShutdown():
|
|
|
|
log.Warn("Shutdown before Repository Indexer completed initialization")
|
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
|
|
|
case duration, ok := <-waitChannel:
|
|
|
|
if !ok {
|
|
|
|
log.Warn("Repository Indexer Initialization failed")
|
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-12-23 05:31:16 -07:00
|
|
|
log.Info("Repository Indexer Initialization took %v", duration)
|
|
|
|
case <-time.After(timeout):
|
2019-12-24 00:26:34 -07:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
2019-12-23 05:31:16 -07:00
|
|
|
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-24 18:08:48 -06:00
|
|
|
}
|
|
|
|
}
|