-
Notifications
You must be signed in to change notification settings - Fork 40
/
state-eventsxor.go
142 lines (107 loc) · 3.06 KB
/
state-eventsxor.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
package flow
import (
"context"
"encoding/json"
"errors"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/vorteil/direktiv/pkg/model"
)
type eventsXorStateLogic struct {
state *model.EventsXorState
workflow *model.Workflow
}
func initEventsXorStateLogic(wf *model.Workflow, state model.State) (stateLogic, error) {
eventsXor, ok := state.(*model.EventsXorState)
if !ok {
return nil, NewInternalError(errors.New("bad state object"))
}
sl := new(eventsXorStateLogic)
sl.state = eventsXor
sl.workflow = wf
return sl, nil
}
func (sl *eventsXorStateLogic) Type() string {
return model.StateTypeEventsXor.String()
}
func (sl *eventsXorStateLogic) Deadline(ctx context.Context, engine *engine, im *instanceMemory) time.Time {
return deadlineFromString(ctx, engine, im, sl.state.Timeout)
}
func (sl *eventsXorStateLogic) ErrorCatchers() []model.ErrorDefinition {
return sl.state.ErrorDefinitions()
}
func (sl *eventsXorStateLogic) ID() string {
return sl.state.ID
}
func (sl *eventsXorStateLogic) LivingChildren(ctx context.Context, engine *engine, im *instanceMemory) []stateChild {
return nil
}
func (sl *eventsXorStateLogic) listenForEvents(ctx context.Context, engine *engine, im *instanceMemory) error {
if im.GetMemory() != nil {
return NewInternalError(errors.New("got unexpected savedata"))
}
var events []*model.ConsumeEventDefinition
for i := range sl.state.Events {
var err error
event := new(model.ConsumeEventDefinition)
event.Type = sl.state.Events[i].Event.Type
event.Context = make(map[string]interface{})
for k, v := range sl.state.Events[i].Event.Context {
var x interface{}
x, err = jqOne(im.data, v)
if err != nil {
err = NewUncatchableError("direktiv.event.jq", "failed to process event context key '%s': %v", k, err)
return err
}
event.Context[k] = x
}
events = append(events, event)
}
err := engine.events.deleteInstanceEventListeners(ctx, im.in)
if err != nil {
return err
}
err = engine.events.listenForEvents(ctx, im, events, false)
if err != nil {
return err
}
return nil
}
func (sl *eventsXorStateLogic) LogJQ() interface{} {
return sl.state.Log
}
func (sl *eventsXorStateLogic) Run(ctx context.Context, engine *engine, im *instanceMemory, wakedata []byte) (transition *stateTransition, err error) {
if len(wakedata) == 0 {
err = sl.listenForEvents(ctx, engine, im)
return
}
events := make([]*cloudevents.Event, 0)
err = json.Unmarshal(wakedata, &events)
if err != nil {
return
}
if len(events) != 1 {
err = NewInternalError(errors.New("incorrect number of events returned"))
return
}
for _, event := range events {
err = im.StoreData(event.Type(), event)
if err != nil {
return
}
for i := 0; i < len(sl.state.Events); i++ {
if sl.state.Events[i].Event.Type == event.Type() {
transition = &stateTransition{
Transform: sl.state.Events[i].Transform,
NextState: sl.state.Events[i].Transition,
}
break
}
}
}
if transition == nil {
err = NewInternalError(errors.New("got the wrong type of event back"))
return
}
return
}