-
Notifications
You must be signed in to change notification settings - Fork 0
/
saga.go
157 lines (144 loc) · 3.65 KB
/
saga.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
package opinionatedsagas
import (
"context"
"database/sql"
"errors"
"time"
events "github.com/markusylisiurunen/go-opinionatedevents"
_ "github.com/lib/pq"
)
type Saga struct {
db *sql.DB
schema string
publisher *events.Publisher
receiver *events.Receiver
queue string
steps []*Step
}
type SagaOpts struct {
ConnectionString string
DB *sql.DB
Schema string
}
func (o *SagaOpts) getSchema() string {
if o.Schema != "" {
return o.Schema
}
return "opinionatedsagas"
}
// FIXME: this entire function (and the co-existing with `opinionatedevents`) needs some love
func NewSaga(ctx context.Context, opts *SagaOpts) (*Saga, error) {
// initialise the database connection (if needed)
var db *sql.DB
if opts.DB != nil {
db = opts.DB
} else {
if opts.ConnectionString == "" {
return nil, errors.New("either a connection string or a *sql.DB must be provided")
}
_db, err := sql.Open("postgres", opts.ConnectionString)
if err != nil {
return nil, err
}
if err := _db.Ping(); err != nil {
return nil, err
}
db = _db
}
// initialise the publisher
destination, err := events.NewPostgresDestination(db,
events.PostgresDestinationWithSchema(opts.getSchema()),
)
if err != nil {
return nil, err
}
publisher, err := events.NewPublisher(events.PublisherWithSyncBridge(destination))
if err != nil {
return nil, err
}
// initialise the receiver
source, err := events.NewPostgresSource(db,
events.PostgresSourceWithIntervalTrigger(1*time.Second),
events.PostgresSourceWithMaxWorkers(16),
events.PostgresSourceWithNotifyTrigger(opts.ConnectionString),
events.PostgresSourceWithSchema(opts.getSchema()),
)
if err != nil {
return nil, err
}
if err := source.QueueDeclare(&events.PostgresSourceQueueDeclareParams{
Topic: "tasks",
Queue: "tasks",
}); err != nil {
return nil, err
}
receiver, err := events.NewReceiver(events.ReceiverWithSource(source))
if err != nil {
return nil, err
}
// FIXME: the migrations from sagas and events must somehow co-exist and not collide... (e.g. both must be able to have migrations 000001, 000002 etc.)
if err := migrate(db, opts.getSchema()); err != nil {
return nil, err
}
// construct the saga
saga := &Saga{
db: db,
publisher: publisher,
queue: "tasks",
receiver: receiver,
schema: opts.getSchema(),
steps: []*Step{},
}
return saga, nil
}
func (s *Saga) AddStep(step *Step) {
step.init(s.db, s.schema, s.publisher, s.receiver, s.queue)
s.steps = append(s.steps, step)
}
func (s *Saga) RegisterHandlers() error {
if len(s.steps) == 0 {
return nil
}
for _, step := range s.steps {
if err := step.isValid(); err != nil {
return err
}
}
for _, step := range s.steps {
if err := step.mountHandleFunc(); err != nil {
return err
}
if err := step.mountCompensateFunc(); err != nil {
return err
}
}
// FIXME: come on... not the `context.Background()`
if err := s.receiver.Start(context.Background()); err != nil {
return err
}
return nil
}
func (s *Saga) TriggerTx(ctx context.Context, tx *sql.Tx, task task) error {
if len(s.steps) == 0 || !s.steps[0].isForTask(task) {
return errors.New("the task must match to the first step's argument type")
}
msg, err := newTaskMessage(task).toOpinionatedMessage()
if err != nil {
return err
}
return s.publisher.Publish(events.WithTx(ctx, tx), msg)
}
func (s *Saga) Trigger(ctx context.Context, task task) error {
tx, err := s.db.Begin()
defer tx.Rollback() //nolint the error is not relevant
if err != nil {
return err
}
if err := s.TriggerTx(ctx, tx, task); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}