@@ -180,7 +180,6 @@ func IndexBatch(elasticURL string, index string, batch string) (int, int, error)
180
180
func IndexContacts (db * sql.DB , elasticURL string , index string , lastModified time.Time ) (int , int , error ) {
181
181
batch := strings.Builder {}
182
182
createdCount , deletedCount := 0 , 0
183
- processedCount := 0
184
183
185
184
if index == "" {
186
185
return createdCount , deletedCount , fmt .Errorf ("empty index passed to IndexContacts" )
@@ -194,10 +193,11 @@ func IndexContacts(db *sql.DB, elasticURL string, index string, lastModified tim
194
193
start := time .Now ()
195
194
196
195
for {
197
- batchCount := 0
198
- batchModified := lastModified
196
+ rows , err := db .Query (contactQuery , lastModified )
199
197
200
- rows , err := db .Query (contactQuery , lastModified .Add (time .Second * - 5 ))
198
+ queryCreated := 0
199
+ queryCount := 0
200
+ queryModified := lastModified
201
201
202
202
// no more rows? return
203
203
if err == sql .ErrNoRows {
@@ -214,7 +214,8 @@ func IndexContacts(db *sql.DB, elasticURL string, index string, lastModified tim
214
214
return 0 , 0 , err
215
215
}
216
216
217
- processedCount ++
217
+ queryCount ++
218
+ lastModified = modifiedOn
218
219
219
220
if isActive {
220
221
log .WithField ("id" , id ).WithField ("modifiedOn" , modifiedOn ).WithField ("contact" , contactJSON ).Debug ("modified contact" )
@@ -228,40 +229,39 @@ func IndexContacts(db *sql.DB, elasticURL string, index string, lastModified tim
228
229
batch .WriteString ("\n " )
229
230
}
230
231
231
- // write to elastic search in batches of 500
232
- if processedCount % batchSize == 0 {
232
+ // write to elastic search in batches
233
+ if queryCount % batchSize == 0 {
233
234
created , deleted , err := IndexBatch (elasticURL , index , batch .String ())
234
235
if err != nil {
235
236
return 0 , 0 , err
236
237
}
237
238
batch .Reset ()
238
239
240
+ queryCreated += created
239
241
createdCount += created
240
242
deletedCount += deleted
241
- batchCount += created
242
243
}
243
-
244
- lastModified = modifiedOn
245
244
}
246
245
247
246
if batch .Len () > 0 {
248
247
created , deleted , err := IndexBatch (elasticURL , index , batch .String ())
249
248
if err != nil {
250
249
return 0 , 0 , err
251
250
}
251
+
252
+ queryCreated += created
252
253
createdCount += created
253
254
deletedCount += deleted
254
- batchCount += created
255
255
batch .Reset ()
256
256
}
257
257
258
- // didn't add anything in this batch and our last modified stayed the same , seen it all, break out
259
- if batchCount == 0 && lastModified .Equal (batchModified ) {
258
+ // last modified stayed the same and we didn't add anything , seen it all, break out
259
+ if lastModified .Equal (queryModified ) && queryCreated == 0 {
260
260
break
261
261
}
262
262
263
263
elapsed := time .Now ().Sub (start )
264
- rate := float32 (processedCount ) / (float32 (elapsed ) / float32 (time .Second ))
264
+ rate := float32 (queryCount ) / (float32 (elapsed ) / float32 (time .Second ))
265
265
log .WithFields (map [string ]interface {}{
266
266
"rate" : int (rate ),
267
267
"added" : createdCount ,
0 commit comments