-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_consumer.go
182 lines (143 loc) · 4.14 KB
/
pull_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package ergonats
import (
"context"
"fmt"
"log/slog"
"strings"
"github.com/ergo-services/ergo/etf"
"github.com/ergo-services/ergo/gen"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
type PullConsumerBehavior interface {
gen.ServerBehavior
InitPullConsumer(process *PullConsumerProcess, args ...etf.Term) (*PullConsumerOptions, error)
HandleMessage(process *PullConsumerProcess, msg jetstream.Msg) error
}
type PullConsumer struct {
gen.Server
}
type PullConsumerOptions struct {
Logger *slog.Logger
Connection *nats.Conn
JsDomain string
StreamName string
NatsConsumerConfig jetstream.ConsumerConfig
}
type PullConsumerProcess struct {
gen.ServerProcess
options PullConsumerOptions
behavior PullConsumerBehavior
}
func (pcp *PullConsumerProcess) Options() *PullConsumerOptions {
return &pcp.options
}
// gen.Server callbacks
func (c *PullConsumer) Init(
process *gen.ServerProcess,
args ...etf.Term) error {
consumerProcess := &PullConsumerProcess{
ServerProcess: *process,
}
consumerProcess.State = nil
behavior, ok := process.Behavior().(PullConsumerBehavior)
if !ok {
return fmt.Errorf("consumer: not a ConsumerBehavior")
}
consumerProcess.behavior = behavior
consumerOpts, err := behavior.InitPullConsumer(consumerProcess, args...)
if err != nil {
return err
}
if err := consumerOpts.validate(); err != nil {
return err
}
if consumerOpts.Logger == nil {
consumerOpts.Logger = slog.Default()
}
consumerOpts.Logger.Info("Initializing pull consumer", slog.Any("pid", process.Info().PID),
slog.String("process_name", process.Name()))
consumerProcess.options = *consumerOpts
process.State = consumerProcess
// Initialize the Nats consumer based on consumerOpts
go consumerProcess.startPulling()
return nil
}
func (c *PullConsumer) HandleCall(
process *gen.ServerProcess,
from gen.ServerFrom,
message etf.Term) (etf.Term, gen.ServerStatus) {
return etf.Atom("ok"), gen.ServerStatusOK
}
func (c *PullConsumer) HandleDirect(
process *gen.ServerProcess,
ref etf.Ref, message interface{}) (interface{}, gen.DirectStatus) {
return nil, fmt.Errorf("unsupported request")
}
func (c *PullConsumer) HandleCast(
process *gen.ServerProcess,
message etf.Term) gen.ServerStatus {
behavior := process.Behavior().(PullConsumerBehavior)
p := process.State.(*PullConsumerProcess)
err := behavior.HandleMessage(p, message.(jetstream.Msg))
if err != nil {
p.options.Logger.Error("Failed to handle cast", slog.Any("error", err))
// dispatch / log error
}
return gen.ServerStatusOK
}
func (c *PullConsumer) HandleInfo(
process *gen.ServerProcess,
message etf.Term) gen.ServerStatus {
return gen.ServerStatusOK
}
func (c *PullConsumer) Terminate(
process *gen.ServerProcess,
reason string) {
}
func (process *PullConsumerProcess) startPulling() {
//ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx := context.Background()
streamName := process.options.StreamName
js, err := GetJetStream(process)
if err != nil {
process.options.Logger.Error("Failed to attach to JetStream",
slog.String("stream", streamName),
slog.Any("error", err),
)
return
}
stream, err := js.Stream(ctx, streamName)
if err != nil {
process.options.Logger.Error("Failed to attach to stream",
slog.String("stream", streamName),
slog.Any("error", err),
)
return
}
cons, err := stream.CreateOrUpdateConsumer(ctx, process.options.NatsConsumerConfig)
if err != nil {
process.options.Logger.Error("Failed to create or locate consumer",
slog.String("consumer", process.options.NatsConsumerConfig.Name),
slog.Any("error", err),
)
return
}
_, _ = cons.Consume(func(msg jetstream.Msg) {
process.Cast(process.Self(), msg)
})
}
func (opts PullConsumerOptions) validate() error {
return nil
}
func GetJetStream(process *PullConsumerProcess) (jetstream.JetStream, error) {
var js jetstream.JetStream
var err error
domain := strings.TrimSpace(process.options.JsDomain)
if len(domain) == 0 {
js, err = jetstream.New(process.options.Connection)
} else {
js, err = jetstream.NewWithDomain(process.options.Connection, domain)
}
return js, err
}