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