-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
49 lines (39 loc) · 982 Bytes
/
batch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"database/sql"
"fmt"
"log"
"time"
"github.com/confluentinc/confluent-kafka-go/v2/kafka"
)
func processBatches(app *App, chanBatches <-chan Batch) {
for batch := range chanBatches {
fmt.Printf("Processing batch: %d items\n", len(batch))
// store partitions for commiting
topicPartitions := []kafka.TopicPartition{}
for _, value := range batch {
topicPartitions = append(topicPartitions, value.TopicPartition)
}
// use db to deduplicate items
err := withTransaction(app.sqlConnection, func(tx *sql.Tx) error {
batch, err := deduplicateBatch(tx, batch)
if err != nil {
return err
}
parsedBatch, err := parseBatch(batch)
if err != nil {
return err
}
if err = storeBatch(app, parsedBatch); err != nil {
return err
}
app.kafkaConsumer.CommitOffsets(topicPartitions)
fmt.Println("Commiting batch")
return nil
})
if err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
}
}