2017-01-25 11:43:02 +09: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 22:29:40 +09:00
|
|
|
package code
|
2017-01-25 11:43:02 +09:00
|
|
|
|
|
|
|
import (
|
2019-12-24 16:26:34 +09:00
|
|
|
"context"
|
|
|
|
"os"
|
2019-12-23 21:31:16 +09:00
|
|
|
"time"
|
2017-09-17 05:16:21 +09:00
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-06 03:29:17 +09:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-02-21 04:53:55 +09:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2019-12-23 21:31:16 +09:00
|
|
|
)
|
2018-02-06 03:29:17 +09:00
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
// SearchResult result of performing a search in a repo
|
|
|
|
type SearchResult struct {
|
2020-02-21 04:53:55 +09: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-17 05:16:21 +09:00
|
|
|
}
|
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
// Indexer defines an interface to indexer issues contents
|
|
|
|
type Indexer interface {
|
|
|
|
Index(repoID int64) error
|
|
|
|
Delete(repoID int64) error
|
2020-02-21 04:53:55 +09:00
|
|
|
Search(repoIDs []int64, language, keyword string, page, pageSize int) (int64, []*SearchResult, []*SearchResultLanguages, error)
|
2019-12-23 21:31:16 +09:00
|
|
|
Close()
|
2017-09-17 05:16:21 +09:00
|
|
|
}
|
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
2019-12-24 16:26:34 +09:00
|
|
|
indexer.Close()
|
2019-12-23 21:31:16 +09:00
|
|
|
return
|
|
|
|
}
|
2017-09-25 09:08:48 +09:00
|
|
|
|
2019-12-25 18:44:09 +09:00
|
|
|
initQueue(setting.Indexer.UpdateQueueLength)
|
|
|
|
|
2019-12-24 16:26:34 +09: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 21:31:16 +09:00
|
|
|
waitChannel := make(chan time.Duration)
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
2019-12-24 16:26:34 +09:00
|
|
|
log.Info("PID: %d Initializing Repository Indexer at: %s", os.Getpid(), setting.Indexer.RepoPath)
|
2020-02-29 07:00:09 +09: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")
|
|
|
|
log.Error("You can completely remove the \"%s\" directory to make Gitea recreate the indexes", setting.Indexer.RepoPath)
|
|
|
|
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 16:26:34 +09:00
|
|
|
bleveIndexer, created, err := NewBleveIndexer(setting.Indexer.RepoPath)
|
2019-12-23 21:31:16 +09:00
|
|
|
if err != nil {
|
2019-12-24 16:26:34 +09:00
|
|
|
if bleveIndexer != nil {
|
|
|
|
bleveIndexer.Close()
|
|
|
|
}
|
|
|
|
cancel()
|
2019-12-23 21:31:16 +09:00
|
|
|
indexer.Close()
|
2019-12-24 16:26:34 +09: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 21:31:16 +09:00
|
|
|
}
|
2019-12-24 16:26:34 +09:00
|
|
|
indexer.set(bleveIndexer)
|
2017-09-25 09:08:48 +09:00
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
go processRepoIndexerOperationQueue(indexer)
|
2017-09-25 09:08:48 +09:00
|
|
|
|
2019-12-23 21:31:16 +09:00
|
|
|
if created {
|
|
|
|
go populateRepoIndexer()
|
|
|
|
}
|
2019-12-24 16:26:34 +09:00
|
|
|
select {
|
|
|
|
case waitChannel <- time.Since(start):
|
|
|
|
case <-graceful.GetManager().IsShutdown():
|
|
|
|
}
|
2017-09-25 09:08:48 +09:00
|
|
|
|
2019-12-24 16:26:34 +09:00
|
|
|
close(waitChannel)
|
2019-12-23 21:31:16 +09:00
|
|
|
}()
|
2017-09-25 09:08:48 +09:00
|
|
|
|
2019-12-23 21:31:16 +09: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 16:26:34 +09: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 21:31:16 +09:00
|
|
|
log.Info("Repository Indexer Initialization took %v", duration)
|
|
|
|
case <-time.After(timeout):
|
2019-12-24 16:26:34 +09:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
2019-12-23 21:31:16 +09:00
|
|
|
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-25 09:08:48 +09:00
|
|
|
}
|
|
|
|
}
|