forked from EvilLord666/step
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution.go
124 lines (99 loc) · 2.77 KB
/
execution.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
package machine
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/sfn/types"
"time"
"github.com/coinbase/step/utils/to"
)
type HistoryEvent struct {
types.HistoryEvent
}
type Execution struct {
Output map[string]interface{}
OutputJSON string
Error error
LastOutput map[string]interface{} // interim output
LastOutputJSON string
LastError error // interim error
ExecutionHistory []HistoryEvent
}
func (sm *Execution) SetOutput(output interface{}, err error) {
switch output.(type) {
case map[string]interface{}:
sm.Output = output.(map[string]interface{})
sm.OutputJSON, _ = to.PrettyJSON(output)
}
if err != nil {
sm.Error = err
}
}
func (sm *Execution) SetLastOutput(output interface{}, err error) {
switch output.(type) {
case map[string]interface{}:
sm.LastOutput = output.(map[string]interface{})
sm.LastOutputJSON, _ = to.PrettyJSON(output)
}
if err != nil {
sm.LastError = err
}
}
func (sm *Execution) EnteredEvent(s State, input interface{}) {
sm.ExecutionHistory = append(sm.ExecutionHistory, createEnteredEvent(s, input))
}
func (sm *Execution) ExitedEvent(s State, output interface{}) {
sm.ExecutionHistory = append(sm.ExecutionHistory, createExitedEvent(s, output))
}
func (sm *Execution) Start() {
sm.ExecutionHistory = []HistoryEvent{createEvent("ExecutionStarted")}
}
func (sm *Execution) Failed() {
sm.ExecutionHistory = append(sm.ExecutionHistory, createEvent("ExecutionFailed"))
}
func (sm *Execution) Succeeded() {
sm.ExecutionHistory = append(sm.ExecutionHistory, createEvent("ExecutionSucceeded"))
}
// Path returns the Path of States, ignoreing TaskFn states
func (sm *Execution) Path() []string {
path := []string{}
for _, er := range sm.ExecutionHistory {
if er.StateEnteredEventDetails != nil {
name := *er.StateEnteredEventDetails.Name
path = append(path, name)
}
}
return path
}
func createEvent(name string) HistoryEvent {
t := time.Now()
return HistoryEvent{
types.HistoryEvent{
Type: types.HistoryEventType(name),
Timestamp: &t,
},
}
}
func createEnteredEvent(state State, input interface{}) HistoryEvent {
event := createEvent(fmt.Sprintf("%vStateEntered", *state.GetType()))
json_raw, err := json.Marshal(input)
if err != nil {
json_raw = []byte{}
}
event.StateEnteredEventDetails = &types.StateEnteredEventDetails{
Name: state.Name(),
Input: to.Strp(string(json_raw)),
}
return event
}
func createExitedEvent(state State, output interface{}) HistoryEvent {
event := createEvent(fmt.Sprintf("%vStateExited", *state.GetType()))
json_raw, err := json.Marshal(output)
if err != nil {
json_raw = []byte{}
}
event.StateExitedEventDetails = &types.StateExitedEventDetails{
Name: state.Name(),
Output: to.Strp(string(json_raw)),
}
return event
}