2017-01-25 10:43:02 +08: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 21:29:40 +08:00
|
|
|
package code
|
2017-01-25 10:43:02 +08:00
|
|
|
|
|
|
|
import (
|
2019-12-24 15:26:34 +08:00
|
|
|
"context"
|
|
|
|
"os"
|
2019-12-23 20:31:16 +08:00
|
|
|
"time"
|
2017-09-17 04:16:21 +08:00
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2018-02-06 02:29:17 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-12-23 20:31:16 +08:00
|
|
|
)
|
2018-02-06 02:29:17 +08:00
|
|
|
|
2019-12-23 20:31:16 +08: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-17 04:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:31:16 +08: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-17 04:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
// Init initialize the repo indexer
|
|
|
|
func Init() {
|
|
|
|
if !setting.Indexer.RepoIndexerEnabled {
|
2019-12-24 15:26:34 +08:00
|
|
|
indexer.Close()
|
2019-12-23 20:31:16 +08:00
|
|
|
return
|
|
|
|
}
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-24 15:26:34 +08: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 20:31:16 +08:00
|
|
|
waitChannel := make(chan time.Duration)
|
|
|
|
go func() {
|
|
|
|
start := time.Now()
|
2019-12-24 15:26:34 +08: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 20:31:16 +08:00
|
|
|
if err != nil {
|
2019-12-24 15:26:34 +08:00
|
|
|
if bleveIndexer != nil {
|
|
|
|
bleveIndexer.Close()
|
|
|
|
}
|
|
|
|
cancel()
|
2019-12-23 20:31:16 +08:00
|
|
|
indexer.Close()
|
2019-12-24 15:26:34 +08: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 20:31:16 +08:00
|
|
|
}
|
2019-12-24 15:26:34 +08:00
|
|
|
indexer.set(bleveIndexer)
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
go processRepoIndexerOperationQueue(indexer)
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-23 20:31:16 +08:00
|
|
|
if created {
|
|
|
|
go populateRepoIndexer()
|
|
|
|
}
|
2019-12-24 15:26:34 +08:00
|
|
|
select {
|
|
|
|
case waitChannel <- time.Since(start):
|
|
|
|
case <-graceful.GetManager().IsShutdown():
|
|
|
|
}
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-24 15:26:34 +08:00
|
|
|
close(waitChannel)
|
2019-12-23 20:31:16 +08:00
|
|
|
}()
|
2017-09-25 08:08:48 +08:00
|
|
|
|
2019-12-23 20:31:16 +08: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 15:26:34 +08: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 20:31:16 +08:00
|
|
|
log.Info("Repository Indexer Initialization took %v", duration)
|
|
|
|
case <-time.After(timeout):
|
2019-12-24 15:26:34 +08:00
|
|
|
cancel()
|
|
|
|
indexer.Close()
|
2019-12-23 20:31:16 +08:00
|
|
|
log.Fatal("Repository Indexer Initialization Timed-Out after: %v", timeout)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-25 08:08:48 +08:00
|
|
|
}
|
|
|
|
}
|