forked from cortezaproject/corteza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
181 lines (143 loc) · 3.05 KB
/
state.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
package wfexec
import (
"encoding/json"
"time"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/expr"
)
type (
// state holds information about Session ID
State struct {
created time.Time
completed *time.Time
// state identifier
stateId uint64
// who's running this?
owner auth.Identifiable
// Session identifier
sessionId uint64
// parent, parent step
parent Step
// current step
step Step
// next steps
next Steps
// step error (if any)
err error
// input variables that were sent to resume the session
input *expr.Vars
// scope
scope *expr.Vars
// step execution results
results *expr.Vars
// error handling step
errHandler Step
// error handled flag, this gets restarted on every new state!
errHandled bool
loops []Iterator
action string
}
)
func NewState(ses *Session, owner auth.Identifiable, caller, current Step, scope *expr.Vars) *State {
return &State{
stateId: nextID(),
owner: owner,
sessionId: ses.id,
created: *now(),
parent: caller,
step: current,
scope: scope,
loops: make([]Iterator, 0, 4),
}
}
func FinalState(ses *Session, scope *expr.Vars) *State {
return &State{
stateId: nextID(),
sessionId: ses.id,
created: *now(),
completed: now(),
scope: scope,
}
}
func (s State) Next(current Step, scope *expr.Vars) *State {
return &State{
stateId: nextID(),
owner: s.owner,
sessionId: s.sessionId,
parent: s.step,
errHandler: s.errHandler,
loops: s.loops,
step: current,
scope: scope,
}
}
func (s State) MakeRequest() *ExecRequest {
return &ExecRequest{
SessionID: s.sessionId,
StateID: s.stateId,
Scope: s.scope,
Input: s.input,
Parent: s.parent,
}
}
func (s *State) newLoop(i Iterator) {
s.loops = append(s.loops, i)
}
// ends loop and returns step that leads out of the loop
func (s *State) loopEnd() (out Steps) {
l := len(s.loops) - 1
if l < 0 {
panic("not inside a loop")
}
out = Steps{s.loops[l].Break()}
s.loops = s.loops[:l]
return
}
func (s State) loopCurr() Iterator {
l := len(s.loops)
if l > 0 {
return s.loops[l-1]
}
return nil
}
func (s State) MakeFrame() *Frame {
var (
// might not be the most optimal way but we need to
// un-reference scope, input, result variables
unref = func(vars *expr.Vars) *expr.Vars {
out := &expr.Vars{}
tmp, _ := json.Marshal(vars)
_ = json.Unmarshal(tmp, out)
return out
}
)
f := &Frame{
CreatedAt: s.created,
SessionID: s.sessionId,
StateID: s.stateId,
Input: unref(s.input),
Scope: unref(s.scope),
Results: unref(s.results),
NextSteps: s.next.IDs(),
Action: s.action,
}
if s.err != nil {
f.Error = s.err.Error()
}
if s.step != nil {
f.StepID = s.step.ID()
}
if s.parent != nil {
f.ParentID = s.parent.ID()
}
if s.completed != nil {
f.StepTime = uint(s.completed.Sub(s.created) / time.Millisecond)
}
return f
}
func (s *State) Error() string {
if s.err == nil {
return ""
}
return s.err.Error()
}