-
Notifications
You must be signed in to change notification settings - Fork 8
/
consumer.go
47 lines (39 loc) · 1.1 KB
/
consumer.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
package job
import (
"context"
"encoding/json"
"log"
"github.com/Shopify/sarama"
"github.com/starryrbs/kfan/app/history/service/internal/biz"
)
type consumeHandler struct {
ready chan bool
hc biz.HistoryJobRepo
}
func NewConsumeHandler(hc biz.HistoryJobRepo) sarama.ConsumerGroupHandler {
return &consumeHandler{ready: make(chan bool), hc: hc}
}
func (c *consumeHandler) Setup(session sarama.ConsumerGroupSession) error {
close(c.ready)
return nil
}
func (c *consumeHandler) Cleanup(session sarama.ConsumerGroupSession) error {
return nil
}
func (c *consumeHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
log.Printf("Message claimed: value = %s, timestamp = %v, topic = %s", string(message.Value), message.Timestamp, message.Topic)
ctx := context.Background()
var history biz.History
err := json.Unmarshal(message.Value, &history)
if err != nil {
return err
}
_, err = c.hc.PersistentSaveHistory(ctx, &history)
if err != nil {
return err
}
session.MarkMessage(message, "")
}
return nil
}