forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
45 lines (37 loc) · 1.11 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
package main
import (
"context"
"github.com/lovoo/goka"
storage "github.com/lovoo/goka/storage/redis"
redis "gopkg.in/redis.v5"
)
// Publisher defines an interface to Publish the event somewhere.
type Publisher interface {
Publish(ctx context.Context, key string, event *Event) error
Close() error
}
// Consume starts goka events consumer.
func Consume(pub Publisher, brokers []string, group string, stream string, store string, namespace string) error {
codec := new(Codec)
input := goka.Input(goka.Stream(stream), codec, func(ctx goka.Context, msg interface{}) {
event, ok := msg.(*Event)
if ok {
pub.Publish(context.Background(), ctx.Key(), event)
}
})
graph := goka.DefineGroup(goka.Group(group), input, goka.Persist(codec))
opts := []goka.ProcessorOption{}
switch {
case store != "":
client := redis.NewClient(&redis.Options{
Addr: store,
})
opts = append(opts, goka.WithStorageBuilder(storage.RedisBuilder(client, namespace)))
defer client.Close()
}
processor, err := goka.NewProcessor(brokers, graph, opts...)
if err != nil {
return err
}
return processor.Run(context.Background())
}