@@ -47,22 +47,28 @@ func main() {
47
47
log .Fatal (err )
48
48
}
49
49
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
-
58
50
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
+
59
63
// doesn't exist or we are rebuilding, create it
60
64
if physicalIndex == "" || config .Rebuild {
61
65
physicalIndex , err = indexer .CreateNewIndex (config .ElasticURL , config .Index )
62
66
if err != nil {
63
- log .WithError (err ).Fatal ("error creating new index" )
67
+ logError (config .Rebuild , err , "error creating new index" )
68
+ continue
64
69
}
65
70
log .WithField ("index" , config .Index ).WithField ("physicalIndex" , physicalIndex ).Info ("created new physical index" )
71
+ remapAlias = true
66
72
}
67
73
68
74
lastModified , err := indexer .GetLastModified (config .ElasticURL , physicalIndex )
@@ -83,28 +89,21 @@ func main() {
83
89
log .WithField ("added" , indexed ).WithField ("deleted" , deleted ).WithField ("index" , physicalIndex ).WithField ("elapsed" , time .Now ().Sub (start )).Info ("completed indexing" )
84
90
85
91
// if the index didn't previously exist or we are rebuilding, remap to our alias
86
- if oldIndex == "" || config . Rebuild {
92
+ if remapAlias {
87
93
err := indexer .MapIndexAlias (config .ElasticURL , config .Index , physicalIndex )
88
94
if err != nil {
89
- logError (config .Rebuild , err , "error mapping alias" )
95
+ logError (config .Rebuild , err , "error remapping alias" )
90
96
continue
91
97
}
92
- oldIndex = physicalIndex
98
+ remapAlias = false
93
99
}
94
100
95
101
if config .Rebuild {
96
102
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
- }
107
103
}
104
+
105
+ // sleep a bit before starting again
106
+ time .Sleep (time .Second * 5 )
108
107
}
109
108
}
110
109
0 commit comments