2024-10-07 16:26:41 +05:30
|
|
|
package elastic
|
2024-10-03 12:40:46 +05:30
|
|
|
|
|
|
|
import (
|
2024-10-07 09:50:00 +05:30
|
|
|
"log"
|
|
|
|
"time"
|
2024-10-03 12:40:46 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type Worker struct {
|
2024-10-03 14:04:17 +05:30
|
|
|
client *Client
|
2024-10-07 09:50:00 +05:30
|
|
|
index string
|
|
|
|
offset int
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
2024-10-07 16:26:41 +05:30
|
|
|
func NewWorker(client *Client, index string, offset int) {
|
2024-10-03 12:40:46 +05:30
|
|
|
w := &Worker{
|
|
|
|
client: client,
|
|
|
|
index: index,
|
2024-10-07 09:50:00 +05:30
|
|
|
offset: offset,
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
go w.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Worker) start() {
|
|
|
|
|
2024-10-07 09:50:00 +05:30
|
|
|
for {
|
2024-10-07 16:26:41 +05:30
|
|
|
log.Printf("worker index=%s offset=%d size=%d\n", w.index, w.offset, w.client.cfg.BatchSize)
|
2024-10-07 09:50:00 +05:30
|
|
|
|
2024-10-07 16:26:41 +05:30
|
|
|
dataArray, err := search(w.client.es, w.index, &w.offset, &w.client.cfg.BatchSize)
|
2024-10-07 09:50:00 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Println("search() err:", err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, data := range dataArray {
|
|
|
|
w.client.ch <- data
|
|
|
|
w.offset++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 12:40:46 +05:30
|
|
|
}
|