Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kafka #25

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
start consumer function
jonhealy1 committed Apr 12, 2023
commit abcda816243b70c57909a03219d9c550aaa6fbc2
45 changes: 45 additions & 0 deletions es-api/database/kafka-consumer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package database

import (
"log"
"time"

"github.com/confluentinc/confluent-kafka-go/kafka"
)

func StartConsumer(topic string, handleMsg func(*kafka.Message)) {
retryInterval := 10 * time.Second
maxRetries := 5

for {
consumer, err := kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": "kafka:9092",
"group.id": "es-api-group",
"auto.offset.reset": "earliest",
})

if err != nil {
log.Printf("Failed to create consumer: %s", err)
time.Sleep(retryInterval)
continue
}

consumer.Subscribe(topic, nil)

for retries := 0; retries < maxRetries; {
msg, err := consumer.ReadMessage(-1)
if err != nil {
log.Printf("Error while reading message: %s", err)
retries++
time.Sleep(retryInterval)
continue
}

handleMsg(msg)
retries = 0
}

consumer.Close()
log.Printf("Consumer closed after %d retries, trying to restart", maxRetries)
}
}