-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer.go
53 lines (42 loc) · 1.14 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
48
49
50
51
52
53
package nsq
import (
"context"
"github.com/alexfalkowski/go-service/transport/nsq/handler"
lzap "github.com/alexfalkowski/go-service/transport/nsq/logger/zap"
"github.com/alexfalkowski/go-service/transport/nsq/meta"
"github.com/alexfalkowski/go-service/transport/nsq/trace/opentracing"
"github.com/nsqio/go-nsq"
"go.uber.org/fx"
"go.uber.org/zap"
)
// ConsumerParams for NSQ.
type ConsumerParams struct {
Config *Config
Logger *zap.Logger
Topic string
Channel string
Handler handler.Handler
}
// RegisterConsumer for NSQ.
func RegisterConsumer(lc fx.Lifecycle, params *ConsumerParams) error {
cfg := nsq.NewConfig()
consumer, err := nsq.NewConsumer(params.Topic, params.Channel, cfg)
if err != nil {
return err
}
lh := lzap.NewHandler(params.Topic, params.Channel, params.Logger, params.Handler)
oh := opentracing.NewHandler(params.Topic, params.Channel, lh)
mh := meta.NewHandler(oh)
consumer.AddHandler(handler.New(mh))
err = consumer.ConnectToNSQLookupd(params.Config.LookupHost)
if err != nil {
return err
}
lc.Append(fx.Hook{
OnStop: func(context.Context) error {
consumer.Stop()
return nil
},
})
return nil
}