-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
250 lines (205 loc) · 4.33 KB
/
engine.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package flowstate
import (
"context"
"errors"
"fmt"
"log/slog"
"sync"
"sync/atomic"
)
var ErrFlowNotFound = errors.New("flow not found")
var sessIDS = &atomic.Int64{}
type Engine interface {
Execute(stateCtx *StateCtx) error
Do(cmds ...Command) error
Shutdown(ctx context.Context) error
}
type engine struct {
d Doer
l *slog.Logger
wg *sync.WaitGroup
doneCh chan struct{}
}
func NewEngine(d Doer, l *slog.Logger) (Engine, error) {
e := &engine{
d: d,
l: l,
wg: &sync.WaitGroup{},
doneCh: make(chan struct{}),
}
e.wg.Add(1)
if err := d.Init(e); err != nil {
return nil, fmt.Errorf("driver: init: %w", err)
}
return e, nil
}
func (e *engine) Execute(stateCtx *StateCtx) error {
select {
case <-e.doneCh:
return fmt.Errorf("engine stopped")
default:
e.wg.Add(1)
defer e.wg.Done()
}
sessID := sessIDS.Add(1)
sessE := &execEngine{engine: e, sessID: sessID}
stateCtx.sessID = sessID
stateCtx.e = e
stateCtx.doneCh = e.doneCh
if stateCtx.Current.ID == `` {
return fmt.Errorf(`state id empty`)
}
for {
select {
case <-e.doneCh:
return nil
default:
}
if stateCtx.Current.Transition.ToID == `` {
return fmt.Errorf(`transition to id empty`)
}
f, err := e.getFlow(stateCtx)
if err != nil {
return err
}
logExecute(stateCtx, e.l)
cmd0, err := f.Execute(stateCtx, sessE)
if err != nil {
return err
}
cmd0.setSessID(sessID)
if cmd, ok := cmd0.(*ExecuteCommand); ok {
cmd.sync = true
}
conflictErr := &ErrCommitConflict{}
if err = e.doCmd(stateCtx.SessID(), cmd0); errors.As(err, conflictErr) {
e.l.Info("engine: do conflict",
"sess", cmd0.SessID(),
"conflict", err.Error(),
"id", stateCtx.Current.ID,
"rev", stateCtx.Current.Rev,
)
return nil
} else if err != nil {
return err
}
if nextStateCtx, err := e.continueExecution(cmd0); err != nil {
return err
} else if nextStateCtx != nil {
stateCtx = nextStateCtx
continue
}
return nil
}
}
func (e *engine) Do(cmds ...Command) error {
return e.do(0, cmds...)
}
func (e *engine) do(execSessID int64, cmds ...Command) error {
if len(cmds) == 0 {
return fmt.Errorf("no commands to do")
}
var sessID int64
for _, cmd := range cmds {
if cmd.SessID() == 0 {
if sessID == 0 {
sessID = sessIDS.Add(1)
}
cmd.setSessID(sessID)
if cmtCmd, ok := cmd.(*CommitCommand); ok {
for _, subCmd := range cmtCmd.Commands {
subCmd.setSessID(sessID)
}
}
}
if err := e.doCmd(execSessID, cmd); err != nil {
return err
}
}
return nil
}
func (e *engine) Shutdown(ctx context.Context) error {
select {
case <-e.doneCh:
return nil
default:
close(e.doneCh)
}
waitCh := make(chan struct{})
go func() {
e.wg.Wait()
close(waitCh)
}()
// undo the Add in NewEngine
e.wg.Done()
select {
case <-ctx.Done():
return ctx.Err()
case <-waitCh:
}
return e.d.Shutdown(ctx)
}
func (e *engine) doCmd(execSessID int64, cmd0 Command) error {
logDo(execSessID, cmd0, e.l)
switch cmd := cmd0.(type) {
case *ExecuteCommand:
if cmd.sync {
return nil
}
stateCtx := cmd.StateCtx.CopyTo(&StateCtx{})
go func() {
if err := e.Execute(stateCtx); err != nil {
e.l.Error("execute failed",
"sess", stateCtx.SessID(),
"error", err,
"id", stateCtx.Current.ID,
"rev", stateCtx.Current.Rev,
)
}
}()
return nil
case *CommitCommand:
return e.d.Do(cmd0)
default:
return e.d.Do(cmd0)
}
}
func (e *engine) getFlow(stateCtx *StateCtx) (Flow, error) {
cmd := GetFlow(stateCtx)
if err := e.d.Do(cmd); err != nil {
return nil, err
}
return cmd.Flow, nil
}
func (e *engine) continueExecution(cmd0 Command) (*StateCtx, error) {
switch cmd := cmd0.(type) {
case *CommitCommand:
if len(cmd.Commands) != 1 {
return nil, fmt.Errorf("commit command must have exactly one command")
}
return e.continueExecution(cmd.Commands[0])
case *ExecuteCommand:
return cmd.StateCtx, nil
case *TransitCommand:
return cmd.StateCtx, nil
case *ResumeCommand:
return cmd.StateCtx, nil
case *PauseCommand:
return nil, nil
case *DelayCommand:
return nil, nil
case *EndCommand:
return nil, nil
case *NoopCommand:
return nil, nil
default:
return nil, fmt.Errorf("unknown command 123 %T", cmd0)
}
}
type execEngine struct {
*engine
sessID int64
}
func (e *execEngine) Do(cmds ...Command) error {
return e.do(e.sessID, cmds...)
}