-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
129 lines (112 loc) · 2.95 KB
/
Copy pathmain.go
File metadata and controls
129 lines (112 loc) · 2.95 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
// Projection demonstrates Watch: a model kept continuously up to date as events
// are appended. Because events are applied from a background goroutine, the
// model must be thread-safe, so it is wrapped with NewModel and read via View.
//
// Run with: go run ./examples/projection
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/nats-io/nats.go"
"github.com/synadia-labs/rita"
"github.com/synadia-labs/rita/testutil"
"github.com/synadia-labs/rita/types"
)
type OrderPlaced struct{ Amount int }
type OrderShipped struct{ Carrier string }
// Stats is a read model aggregating across all orders.
type Stats struct {
Placed int
Shipped int
}
func (s *Stats) Evolve(_ context.Context, e *rita.Event) error {
switch e.Data.(type) {
case *OrderPlaced:
s.Placed++
case *OrderShipped:
s.Shipped++
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
dir, err := os.MkdirTemp("", "rita-example-*")
if err != nil {
return err
}
defer func() { _ = os.RemoveAll(dir) }()
srv := testutil.NewNatsServerWithDir(dir)
defer testutil.ShutdownNatsServer(srv)
nc, err := nats.Connect(srv.ClientURL())
if err != nil {
return err
}
defer nc.Close()
registry, err := types.NewRegistry(map[string]*types.Type{
"order-placed": {Init: func() any { return &OrderPlaced{} }},
"order-shipped": {Init: func() any { return &OrderShipped{} }},
})
if err != nil {
return err
}
mgr, err := rita.New(nc, rita.WithRegistry(registry))
if err != nil {
return err
}
ctx := context.Background()
es, err := mgr.CreateEventStore(ctx, rita.EventStoreConfig{Name: "orders"})
if err != nil {
return err
}
// Some events already exist before the projection starts.
if _, err := es.Append(ctx, []*rita.Event{
{Entity: "order.1", Data: &OrderPlaced{Amount: 50}},
{Entity: "order.2", Data: &OrderPlaced{Amount: 75}},
}); err != nil {
return err
}
// Watch catches up on existing events before returning, then keeps applying
// new ones in the background until Stop.
model := rita.NewModel(&Stats{})
w, err := es.Watch(ctx, model)
if err != nil {
return err
}
defer w.Stop()
// Append more events after the watch is live.
if _, err := es.Append(ctx, []*rita.Event{
{Entity: "order.1", Data: &OrderShipped{Carrier: "UPS"}},
{Entity: "order.2", Data: &OrderShipped{Carrier: "FedEx"}},
}); err != nil {
return err
}
// Delivery is asynchronous, so poll the view until it reflects all events.
deadline := time.Now().Add(5 * time.Second)
for {
var done bool
if err := model.View(ctx, func(s *Stats) error {
done = s.Placed == 2 && s.Shipped == 2
return nil
}); err != nil {
return err
}
if done {
break
}
if time.Now().After(deadline) {
return fmt.Errorf("projection did not catch up within deadline")
}
time.Sleep(10 * time.Millisecond)
}
return model.View(ctx, func(s *Stats) error {
fmt.Printf("placed=%d shipped=%d\n", s.Placed, s.Shipped)
return nil
})
}