Skip to content

Commit 3acdb13

Browse files
committed
Handle keyword search error when issue indexer service is not available
1 parent 49396ba commit 3acdb13

File tree

7 files changed

+38
-12
lines changed

7 files changed

+38
-12
lines changed

models/db/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type Engine interface {
6565
Query(...interface{}) ([]map[string][]byte, error)
6666
Cols(...string) *xorm.Session
6767
Context(ctx context.Context) *xorm.Session
68+
Ping() error
6869
}
6970

7071
// TableInfo returns table's information via an object

modules/indexer/issues/db.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,44 @@
44

55
package issues
66

7-
import "code.gitea.io/gitea/models"
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/models/db"
10+
)
811

912
// DBIndexer implements Indexer interface to use database's like search
1013
type DBIndexer struct{}
1114

1215
// Init dummy function
13-
func (db *DBIndexer) Init() (bool, error) {
16+
func (i *DBIndexer) Init() (bool, error) {
1417
return false, nil
1518
}
1619

1720
// SetAvailabilityChangeCallback dummy function
18-
func (db *DBIndexer) SetAvailabilityChangeCallback(callback func(bool)) {
21+
func (i *DBIndexer) SetAvailabilityChangeCallback(callback func(bool)) {
1922
}
2023

2124
// Ping checks if database is available
22-
func (db *DBIndexer) Ping() bool {
23-
return models.Ping() != nil
25+
func (i *DBIndexer) Ping() bool {
26+
return db.GetEngine(db.DefaultContext).Ping() != nil
2427
}
2528

2629
// Index dummy function
27-
func (db *DBIndexer) Index(issue []*IndexerData) error {
30+
func (i *DBIndexer) Index(issue []*IndexerData) error {
2831
return nil
2932
}
3033

3134
// Delete dummy function
32-
func (db *DBIndexer) Delete(ids ...int64) error {
35+
func (i *DBIndexer) Delete(ids ...int64) error {
3336
return nil
3437
}
3538

3639
// Close dummy function
37-
func (db *DBIndexer) Close() {
40+
func (i *DBIndexer) Close() {
3841
}
3942

4043
// Search dummy function
41-
func (db *DBIndexer) Search(kw string, repoIDs []int64, limit, start int) (*SearchResult, error) {
44+
func (i *DBIndexer) Search(kw string, repoIDs []int64, limit, start int) (*SearchResult, error) {
4245
total, ids, err := models.SearchIssueIDsByKeyword(kw, repoIDs, limit, start)
4346
if err != nil {
4447
return nil, err

modules/indexer/issues/elastic_search.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"net"
1112
"strconv"
1213
"time"
1314

@@ -260,7 +261,8 @@ func (b *ElasticSearchIndexer) Close() {
260261
}
261262

262263
func (b *ElasticSearchIndexer) checkError(err error) error {
263-
if elastic.IsConnErr(err) && b.available {
264+
var opErr *net.OpError
265+
if b.available && (elastic.IsConnErr(err) || (errors.As(err, &opErr) && (opErr.Op == "dial" || opErr.Op == "read"))) {
264266
b.available = false
265267
if b.availabilityCallback != nil {
266268
b.availabilityCallback(b.available)

modules/indexer/issues/indexer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,14 @@ func SearchIssuesByKeyword(repoIDs []int64, keyword string) ([]int64, error) {
387387
}
388388
return issueIDs, nil
389389
}
390+
391+
// IsAvailable checks if issue indexer is available
392+
func IsAvailable() bool {
393+
indexer := holder.get()
394+
if indexer == nil {
395+
log.Error("IsAvailable(): unable to get indexer!")
396+
return false
397+
}
398+
399+
return indexer.Ping()
400+
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ issues.filter_sort.moststars = Most stars
12621262
issues.filter_sort.feweststars = Fewest stars
12631263
issues.filter_sort.mostforks = Most forks
12641264
issues.filter_sort.fewestforks = Fewest forks
1265+
issues.keyword_search_unavailable = Currently searhing by keyword is not available. Please contact your site administrator.
12651266
issues.action_open = Open
12661267
issues.action_close = Close
12671268
issues.action_label = Label

routers/web/repo/issue.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption uti
163163
if len(keyword) > 0 {
164164
issueIDs, err = issue_indexer.SearchIssuesByKeyword([]int64{repo.ID}, keyword)
165165
if err != nil {
166-
ctx.ServerError("issueIndexer.Search", err)
167-
return
166+
if issue_indexer.IsAvailable() {
167+
ctx.ServerError("issueIndexer.Search", err)
168+
return
169+
}
170+
ctx.Data["IssueIndexerUnavailable"] = true
168171
}
169172
if len(issueIDs) == 0 {
170173
forceEmpty = true

templates/shared/issuelist.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,10 @@
139139
</div>
140140
</li>
141141
{{end}}
142+
{{if .IssueIndexerUnavailable}}
143+
<div class="ui error message">
144+
<p>{{$.i18n.Tr "repo.issues.keyword_search_unavailable"}}</p>
145+
</div>
146+
{{end}}
142147
</div>
143148
{{template "base/paginate" .}}

0 commit comments

Comments
 (0)