forked from violetpay-org/go-saga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step.go
194 lines (156 loc) · 5.02 KB
/
step.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
183
184
185
186
187
188
189
190
191
192
193
194
package saga
type Step interface {
// Name returns the name of the step.
Name() string
// IsCompensable returns true if the step is compensable.
IsCompensable() bool
// IsInvocable returns true if the step is invocable.
IsInvocable() bool
// MustBeCompleted returns true if the invocation action must be completed. So If it is true, something will be retried until it is completed.
MustBeCompleted() bool
}
func newRemoteStep[Tx TxContext](name string, endpoint Endpoint[Tx]) remoteStep[Tx] {
return remoteStep[Tx]{
name: name,
invocation: newRemoteInvocationAction(endpoint),
invokeEndpoint: endpoint,
retry: false,
}
}
func newRemoteStepWithCompensation[Tx TxContext](step remoteStep[Tx], endpoint Endpoint[Tx]) remoteStep[Tx] {
return remoteStep[Tx]{
name: step.name,
invocation: step.invocation,
invokeEndpoint: step.invokeEndpoint,
compensation: newRemoteCompensationAction(endpoint),
compEndpoint: endpoint,
retry: step.retry,
}
}
func newRemoteStepWithRetry[Tx TxContext](step remoteStep[Tx]) remoteStep[Tx] {
return remoteStep[Tx]{
name: step.name,
invocation: step.invocation,
invokeEndpoint: step.invokeEndpoint,
compensation: step.compensation,
compEndpoint: step.compEndpoint,
retry: true,
}
}
type remoteStep[Tx TxContext] struct {
name string
invocation invokeAction[Tx]
invokeEndpoint Endpoint[Tx]
compensation compensateAction[Tx]
compEndpoint Endpoint[Tx]
retry bool
}
func (s remoteStep[Tx]) Name() string {
return s.name
}
func (s remoteStep[Tx]) IsCompensable() bool {
return s.compensation != nil
}
func (s remoteStep[Tx]) IsInvocable() bool {
return s.invocation != nil
}
func (s remoteStep[Tx]) MustBeCompleted() bool {
return s.retry == true
}
func (s remoteStep[Tx]) SetRetry(retry bool) remoteStep[Tx] {
s.retry = retry
return s
}
func newRemoteCompensationAction[Tx TxContext](endpoint Endpoint[Tx]) compensateAction[Tx] {
return func(s Session) Executable[Tx] {
command := endpoint.CommandConstructor()(s)
return endpoint.CommandRepository().SaveMessage(command)
}
}
type compensateAction[Tx TxContext] func(Session) Executable[Tx]
func newRemoteInvocationAction[Tx TxContext](endpoint Endpoint[Tx]) invokeAction[Tx] {
return func(s Session) Executable[Tx] {
command := endpoint.CommandConstructor()(s)
return endpoint.CommandRepository().SaveMessage(command)
}
}
type invokeAction[Tx TxContext] func(Session) Executable[Tx]
func newLocalStep[Tx TxContext](name string, endpoint LocalEndpoint[Tx]) localStep[Tx] {
return localStep[Tx]{
name: name,
invocation: newLocalInvokeAction(endpoint),
invokeEndpoint: endpoint,
retry: false,
}
}
func newLocalStepWithCompensation[Tx TxContext](step localStep[Tx], endpoint LocalEndpoint[Tx]) localStep[Tx] {
return localStep[Tx]{
name: step.name,
invocation: step.invocation,
invokeEndpoint: step.invokeEndpoint,
compensation: newLocalCompensateAction(endpoint),
compEndpoint: endpoint,
retry: step.retry,
}
}
func newLocalStepWithRetry[Tx TxContext](step localStep[Tx]) localStep[Tx] {
return localStep[Tx]{
name: step.name,
invocation: step.invocation,
invokeEndpoint: step.invokeEndpoint,
compensation: step.compensation,
compEndpoint: step.compEndpoint,
retry: true,
}
}
type localStep[Tx TxContext] struct {
name string
invocation localInvokeAction[Tx]
invokeEndpoint LocalEndpoint[Tx]
compensation localCompensateAction[Tx]
compEndpoint LocalEndpoint[Tx]
retry bool
}
func (s localStep[Tx]) Name() string {
return s.name
}
func (s localStep[Tx]) IsCompensable() bool {
return s.compensation != nil
}
func (s localStep[Tx]) IsInvocable() bool {
return s.invocation != nil
}
func (s localStep[Tx]) MustBeCompleted() bool {
return s.retry == true
}
func (s localStep[Tx]) SetRetry(retry bool) localStep[Tx] {
s.retry = retry
return s
}
func newLocalCompensateAction[Tx TxContext](endpoint LocalEndpoint[Tx]) localCompensateAction[Tx] {
action := func(s Session) (Executable[Tx], error) {
cmd, err := endpoint.handle(s)
if err != nil {
msg := endpoint.FailureResponseConstructor()(s)
return endpoint.FailureResRepository().SaveMessage(msg), nil
}
msg := endpoint.SuccessResponseConstructor()(s)
cmd2 := endpoint.SuccessResRepository().SaveMessage(msg)
return CombineExecutables(cmd, cmd2), nil
}
return action
}
type localCompensateAction[Tx TxContext] func(Session) (Executable[Tx], error)
func newLocalInvokeAction[Tx TxContext](endpoint LocalEndpoint[Tx]) localInvokeAction[Tx] {
return func(s Session) (Executable[Tx], error) {
cmd, err := endpoint.handle(s)
if err != nil {
msg := endpoint.FailureResponseConstructor()(s)
return endpoint.FailureResRepository().SaveMessage(msg), nil
}
msg := endpoint.SuccessResponseConstructor()(s)
cmd2 := endpoint.SuccessResRepository().SaveMessage(msg)
return CombineExecutables(cmd, cmd2), nil
}
}
type localInvokeAction[Tx TxContext] func(Session) (Executable[Tx], error)