-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.go
More file actions
161 lines (139 loc) · 4.21 KB
/
command.go
File metadata and controls
161 lines (139 loc) · 4.21 KB
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
package bee
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/blinkinglight/bee/co"
"github.com/blinkinglight/bee/gen"
"github.com/nats-io/nats.go"
"google.golang.org/protobuf/proto"
)
const commandsStream = "COMMANDS"
type CommandHandler interface {
Handle(m *gen.CommandEnvelope) ([]*gen.EventEnvelope, error)
}
type CommandProcessor struct {
js nats.JetStreamContext
nc *nats.Conn
aggregate string
subject string
durable string
handler CommandHandler
cfg *co.Config
}
// Command is the main entry point for processing commands.
// accepts co.Options to configure the command processor.
// co.WithSubject - use custom subject instead of default "cmds.aggregate"
// co.WithAggregate - use custom aggregate name
func Command(ctx context.Context, handler CommandHandler, opts ...co.Options) {
cfg := &co.Config{}
for _, opt := range opts {
opt(cfg)
}
if cfg.Aggregate == "" {
panic("Aggregate name is required for command processor")
}
subject := fmt.Sprintf(CommandsPrefix+".%s", cfg.Aggregate)
if cfg.Subject != "" {
subject = cfg.Subject
}
nc, _ := Nats(ctx)
js, _ := JetStream(ctx)
cp := &CommandProcessor{js: js, nc: nc, subject: subject, aggregate: cfg.Aggregate, durable: "default", handler: handler, cfg: cfg}
c, cancel := context.WithCancel(ctx)
go func() {
for {
err := cp.init(c, cancel)
if err != nil {
log.Printf("Error initializing command processor: %v", err)
continue
}
}
}()
<-c.Done()
}
func (cp *CommandProcessor) init(ctx context.Context, cancel context.CancelFunc) error {
_, err := cp.js.AddStream(&nats.StreamConfig{
Name: commandsStream,
Subjects: []string{CommandsPrefix + ".>"},
Retention: nats.WorkQueuePolicy,
Storage: nats.FileStorage,
Replicas: 1,
Duplicates: 5 * time.Minute,
})
if err != nil {
log.Printf("Error adding stream: %v", err)
}
_, err = cp.js.AddConsumer(commandsStream, &nats.ConsumerConfig{
Name: cp.aggregate + "_" + cp.durable + "_cmd",
Durable: cp.aggregate + "_" + cp.durable + "_cmd",
FilterSubject: cp.subject,
AckPolicy: nats.AckExplicitPolicy,
})
if err != nil {
return err
}
sub, err := cp.js.PullSubscribe(cp.subject, cp.aggregate+"_"+cp.durable+"_cmd", nats.BindStream(commandsStream), nats.ManualAck(), nats.AckExplicit(), nats.DeliverAll())
if err != nil {
log.Printf("Error subscribing to commands: %v", err)
cancel()
}
defer sub.Unsubscribe()
for {
if ctx.Err() != nil {
cancel()
return ctx.Err()
}
msg, _ := sub.Fetch(1, nats.MaxWait(60*time.Second))
if len(msg) == 0 {
time.Sleep(50 * time.Millisecond)
continue
}
for _, msg := range msg {
var cmd gen.CommandEnvelope
err := proto.Unmarshal(msg.Data, &cmd)
if err != nil {
log.Printf("Error unmarshalling message: %v", err)
msg.Ack()
continue
}
errorNotificationSubject := fmt.Sprintf("notifications.%s.error", cmd.CorrelationId)
successNotificationSubject := fmt.Sprintf("notifications.%s.success", cmd.CorrelationId)
events, err := cp.handler.Handle(&cmd)
if err != nil {
log.Printf("Error handling command: %v", err)
if cmd.CorrelationId != "" {
cp.nc.Publish(errorNotificationSubject, []byte(`{"message":"`+err.Error()+`"}`))
}
msg.Ack()
continue
}
for _, event := range events {
if event.AggregateType == "" {
event.AggregateType = cmd.Aggregate
}
if event.AggregateId == "" {
event.AggregateId = cmd.AggregateId
}
eventSubject := fmt.Sprintf("events.%s.%s.%s", event.AggregateType, event.AggregateId, event.EventType)
if len(event.Parents) > 0 {
var parents []string
for _, parent := range event.Parents {
parents = append(parents, fmt.Sprintf("%s.%s", parent.AggregateType, parent.AggregateId))
}
eventSubject = fmt.Sprintf("events.%s.%s.%s.%s", strings.Join(parents, "."), event.AggregateType, event.AggregateId, event.EventType)
}
b, _ := proto.Marshal(event)
if _, err := cp.js.Publish(eventSubject, b); err != nil {
log.Printf("Error publishing event %v", err)
}
}
if cmd.CorrelationId != "" {
cp.nc.Publish(successNotificationSubject, []byte(`{"message":"`+cmd.AggregateId+`"}`))
}
_ = msg.Ack()
}
}
}