Skip to content

Commit 01e48fc

Browse files
committed
refactor main loop in indexer
1 parent 6e83cfd commit 01e48fc

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

Diff for: cmd/rp-indexer/main.go

+21-22
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,28 @@ func main() {
4747
log.Fatal(err)
4848
}
4949

50-
physicalIndexes := indexer.FindPhysicalIndexes(config.ElasticURL, config.Index)
51-
log.WithField("physicalIndexes", physicalIndexes).WithField("index", config.Index).Info("found physical indexes")
52-
physicalIndex := ""
53-
if len(physicalIndexes) > 0 {
54-
physicalIndex = physicalIndexes[0]
55-
}
56-
oldIndex := physicalIndex
57-
5850
for {
51+
// find our physical index
52+
physicalIndexes := indexer.FindPhysicalIndexes(config.ElasticURL, config.Index)
53+
log.WithField("physicalIndexes", physicalIndexes).WithField("index", config.Index).Debug("found physical indexes")
54+
55+
physicalIndex := ""
56+
if len(physicalIndexes) > 0 {
57+
physicalIndex = physicalIndexes[0]
58+
}
59+
60+
// whether we need to remap our alias after building
61+
remapAlias := false
62+
5963
// doesn't exist or we are rebuilding, create it
6064
if physicalIndex == "" || config.Rebuild {
6165
physicalIndex, err = indexer.CreateNewIndex(config.ElasticURL, config.Index)
6266
if err != nil {
63-
log.WithError(err).Fatal("error creating new index")
67+
logError(config.Rebuild, err, "error creating new index")
68+
continue
6469
}
6570
log.WithField("index", config.Index).WithField("physicalIndex", physicalIndex).Info("created new physical index")
71+
remapAlias = true
6672
}
6773

6874
lastModified, err := indexer.GetLastModified(config.ElasticURL, physicalIndex)
@@ -83,28 +89,21 @@ func main() {
8389
log.WithField("added", indexed).WithField("deleted", deleted).WithField("index", physicalIndex).WithField("elapsed", time.Now().Sub(start)).Info("completed indexing")
8490

8591
// if the index didn't previously exist or we are rebuilding, remap to our alias
86-
if oldIndex == "" || config.Rebuild {
92+
if remapAlias {
8793
err := indexer.MapIndexAlias(config.ElasticURL, config.Index, physicalIndex)
8894
if err != nil {
89-
logError(config.Rebuild, err, "error mapping alias")
95+
logError(config.Rebuild, err, "error remapping alias")
9096
continue
9197
}
92-
oldIndex = physicalIndex
98+
remapAlias = false
9399
}
94100

95101
if config.Rebuild {
96102
os.Exit(0)
97-
} else {
98-
time.Sleep(time.Second * 5)
99-
physicalIndex = ""
100-
physicalIndexes = indexer.FindPhysicalIndexes(config.ElasticURL, config.Index)
101-
log.WithField("physicalIndexes", physicalIndexes).WithField("index", config.Index).Debug("found physical indexes")
102-
if len(physicalIndex) > 0 {
103-
physicalIndex = physicalIndexes[0]
104-
} else {
105-
oldIndex = ""
106-
}
107103
}
104+
105+
// sleep a bit before starting again
106+
time.Sleep(time.Second * 5)
108107
}
109108
}
110109

Diff for: indexer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func MakeJSONRequest(method string, url string, body string, jsonStruct interfac
103103
req.Header.Add("Content-Type", "application/json")
104104
resp, err := http.DefaultClient.Do(req)
105105

106-
l := log.WithField("url", url).WithField("method", method).WithField("body", body)
106+
l := log.WithField("url", url).WithField("method", method).WithField("request", body)
107107
if err != nil {
108108
l.WithError(err).Error("error making ES request")
109109
return resp, err
@@ -116,7 +116,7 @@ func MakeJSONRequest(method string, url string, body string, jsonStruct interfac
116116
return resp, err
117117
}
118118

119-
l = l.WithField("body", string(jsonBody)).WithField("status", resp.StatusCode)
119+
l = l.WithField("response", string(jsonBody)).WithField("status", resp.StatusCode)
120120

121121
// error if we got a non-200
122122
if resp.StatusCode != http.StatusOK {

0 commit comments

Comments
 (0)