elasticstream/worker.go

46 lines
713 B
Go
Raw Normal View History

2024-10-03 12:40:46 +05:30
package elasticstream
import (
2024-10-07 09:50:00 +05:30
"log"
"time"
// "github.com/elastic/go-elasticsearch/v8"
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
size int
2024-10-03 12:40:46 +05:30
}
2024-10-07 09:50:00 +05:30
func NewWorker(client *Client, index string, offset, size 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,
size: size,
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 13:58:20 +05:30
log.Printf("worker index=%s offset=%d size=%d\n", w.index, w.offset, w.size)
2024-10-07 09:50:00 +05:30
dataArray, err := search(w.client.es, w.index, &w.offset, &w.size)
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
}