/
action.go
200 lines (150 loc) · 4.32 KB
/
action.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
package activity
import (
"context"
"encoding/json"
"fmt"
"runtime/debug"
"github.com/TIBCOSoftware/flogo-lib/core/action"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
const (
ACTION_REF = "github.com/TIBCOSoftware/flogo-contrib/action/activity"
)
type ActivityAction struct {
act activity.Activity
md *action.Metadata
ioMetadata *data.IOMetadata
}
type actionInstance struct {
act activity.Activity
inputs map[string]*data.Attribute
outputScope *data.FixedScope
}
type ActionData struct {
Ref string `json:"ref"`
}
type ActionFactory struct {
}
//todo fix this
var metadata = &action.Metadata{ID: ACTION_REF, Async: false}
func init() {
action.RegisterFactory(ACTION_REF, &ActionFactory{})
}
func (ff *ActionFactory) Init() error {
return nil
}
func (ff *ActionFactory) New(config *action.Config) (action.Action, error) {
activityAction := &ActivityAction{}
var actionData ActionData
err := json.Unmarshal(config.Data, &actionData)
if err != nil {
return nil, fmt.Errorf("failed to read activity action data '%s' error '%s'", config.Id, err.Error())
}
act := activity.Get(actionData.Ref)
if act == nil {
return nil, fmt.Errorf("failed to load activity, activity '%s' not registered", config.Id)
}
activityAction.act = act
iomd := &data.IOMetadata{}
iomd.Input = act.Metadata().Input
iomd.Output = act.Metadata().Output
activityAction.ioMetadata = iomd
return activityAction, nil
}
// Metadata get the Action's metadata
func (a *ActivityAction) Metadata() *action.Metadata {
return metadata
}
// IOMetadata get the Action's IO metadata
func (a *ActivityAction) IOMetadata() *data.IOMetadata {
return a.ioMetadata
}
func (a *ActivityAction) Run(ctx context.Context, inputs map[string]*data.Attribute) (map[string]*data.Attribute, error) {
defer func() {
if r := recover(); r != nil {
logger.Warnf("Unhandled Error executing activity [%s] : %v\n", a.act.Metadata().ID, r)
// todo: useful for debugging
logger.Debugf("StackTrace: %s", debug.Stack())
//if evalErr == nil {
// evalErr = NewActivityEvalError(a.task.Name(), "unhandled", fmt.Sprintf("%v", r))
// done = false
//}
}
//if evalErr != nil {
// logger.Errorf("Execution failed for Activity[%s] in Flow[%s] - %s", a.task.Name(), a.flowInst.flowDef.Name(), evalErr.Error())
//}
}()
ai := &actionInstance{inputs: inputs, outputScope: data.NewFixedScope(a.act.Metadata().Output)}
_, evalErr := a.act.Eval(ai)
if evalErr != nil {
return nil, evalErr
}
return ai.outputScope.GetAttrs(), nil
}
/////////////////////////////////////////
// activity.Context Implementation
func (ai *actionInstance) ActivityHost() activity.Host {
return ai
}
func (ai *actionInstance) Name() string {
return ""
}
func (ai *actionInstance) GetSetting(setting string) (value interface{}, exists bool) {
return nil, false
}
func (ai *actionInstance) GetInitValue(key string) (value interface{}, exists bool) {
return nil, false
}
// GetInput implements activity.Context.GetInput
func (ai *actionInstance) GetInput(name string) interface{} {
val, found := ai.inputs[name]
if found {
return val.Value()
}
return nil
}
// GetOutput implements activity.Context.GetOutput
func (ai *actionInstance) GetOutput(name string) interface{} {
val, found := ai.outputScope.GetAttr(name)
if found {
return val.Value()
}
return nil
}
// SetOutput implements activity.Context.SetOutput
func (ai *actionInstance) SetOutput(name string, value interface{}) {
ai.outputScope.SetAttrValue(name, value)
}
//Deprecated
func (ai *actionInstance) TaskName() string {
//ignore
return ""
}
//Deprecated
func (ai *actionInstance) FlowDetails() activity.FlowDetails {
//ignore
return nil
}
/////////////////////////////////////////
// activity.Host Implementation
func (ai *actionInstance) ID() string {
//ignore
return ""
}
func (ai *actionInstance) IOMetadata() *data.IOMetadata {
return nil
}
func (ai *actionInstance) Reply(replyData map[string]*data.Attribute, err error) {
// ignore
}
func (ai *actionInstance) Return(returnData map[string]*data.Attribute, err error) {
//ignore
}
func (ai *actionInstance) WorkingData() data.Scope {
return nil
}
func (ai *actionInstance) GetResolver() data.Resolver {
return data.GetBasicResolver()
}