This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtask_state.go
114 lines (87 loc) · 2.33 KB
/
task_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
package machine
import (
"context"
"fmt"
"github.com/coinbase/step/handler"
"github.com/coinbase/step/jsonpath"
"github.com/coinbase/step/utils/to"
)
type TaskState struct {
stateStr // Include Defaults
Type *string
Comment *string `json:",omitempty"`
InputPath *jsonpath.Path `json:",omitempty"`
OutputPath *jsonpath.Path `json:",omitempty"`
ResultPath *jsonpath.Path `json:",omitempty"`
Parameters interface{} `json:",omitempty"`
Resource *string `json:",omitempty"`
Catch []*Catcher `json:",omitempty"`
Retry []*Retrier `json:",omitempty"`
// Maps a Lambda Handler Function
TaskHandler interface{} `json:"-"`
Next *string `json:",omitempty"`
End *bool `json:",omitempty"`
TimeoutSeconds int `json:",omitempty"`
HeartbeatSeconds int `json:",omitempty"`
}
func (s *TaskState) SetTaskHandler(resourcefn interface{}) {
s.TaskHandler = resourcefn
}
func (s *TaskState) process(ctx context.Context, input interface{}) (interface{}, *string, error) {
result, err := handler.CallHandlerFunction(s.TaskHandler, ctx, input)
if err != nil {
return nil, nil, err
}
result, err = to.FromJSON(result)
if err != nil {
return nil, nil, err
}
return result, nextState(s.Next, s.End), nil
}
// Input must include the Task name in $.Task
func (s *TaskState) Execute(ctx context.Context, input interface{}) (output interface{}, next *string, err error) {
return processError(s,
processCatcher(s.Catch,
processRetrier(s.Name(), s.Retry,
inputOutput(
s.InputPath,
s.OutputPath,
withParams(
s.Parameters,
result(s.ResultPath, s.process),
),
),
),
),
)(ctx, input)
}
func (s *TaskState) Validate() error {
s.SetType(to.Strp("Task"))
if err := ValidateNameAndType(s); err != nil {
return fmt.Errorf("%v %v", errorPrefix(s), err)
}
if err := endValid(s.Next, s.End); err != nil {
return fmt.Errorf("%v %v", errorPrefix(s), err)
}
if s.Resource == nil {
return fmt.Errorf("%v Requires Resource", errorPrefix(s))
}
if s.TaskHandler != nil {
if err := handler.ValidateHandler(s.TaskHandler); err != nil {
return err
}
}
if err := catchValid(s.Catch); err != nil {
return err
}
if err := retryValid(s.Retry); err != nil {
return err
}
return nil
}
func (s *TaskState) SetType(t *string) {
s.Type = t
}
func (s *TaskState) GetType() *string {
return s.Type
}