forked from TIBCOSoftware/flogo-contrib
/
action.go
367 lines (294 loc) · 8.96 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package flow
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"sync"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/definition"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/extension"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/instance"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/model"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/provider"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/tester"
"github.com/TIBCOSoftware/flogo-lib/core/action"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/TIBCOSoftware/flogo-lib/util"
)
const (
FLOW_REF = "github.com/TIBCOSoftware/flogo-contrib/action/flow"
)
// ActionOptions are the options for the FlowAction
type ActionOptions struct {
MaxStepCount int
Record bool
}
type FlowAction struct {
idGenerator *util.Generator
actionOptions *ActionOptions
config *action.Config
}
// Provides the different extension points to the Flow Action
type ExtensionProvider interface {
GetFlowProvider() provider.Provider
GetFlowModel() *model.FlowModel
GetStateRecorder() instance.StateRecorder
GetMapperFactory() definition.MapperFactory
GetLinkExprManagerFactory() definition.LinkExprManagerFactory
GetFlowTester() *tester.RestEngineTester
}
var actionMu sync.Mutex
var ep ExtensionProvider
//var flowAction *FlowAction
var idGenerator *util.Generator
var record bool
func init() {
action.RegisterFactory(FLOW_REF, &FlowFactory{})
}
func SetExtensionProvider(provider ExtensionProvider) {
actionMu.Lock()
defer actionMu.Unlock()
ep = provider
}
type FlowFactory struct{}
func (ff *FlowFactory) New(config *action.Config) action.Action {
actionMu.Lock()
defer actionMu.Unlock()
var flowAction *FlowAction
if flowAction == nil {
options := &ActionOptions{Record: record}
if ep == nil {
testerEnabled := os.Getenv(tester.ENV_ENABLED)
if strings.ToLower(testerEnabled) == "true" {
ep = tester.NewExtensionProvider()
sm := util.GetDefaultServiceManager()
sm.RegisterService(ep.GetFlowTester())
record = true
options.Record = true
} else {
ep = extension.New()
}
definition.SetMapperFactory(ep.GetMapperFactory())
definition.SetLinkExprManagerFactory(ep.GetLinkExprManagerFactory())
}
if idGenerator == nil {
idGenerator, _ = util.NewGenerator()
}
if options.MaxStepCount < 1 {
options.MaxStepCount = int(^uint16(0))
}
flowAction = &FlowAction{config: config}
flowAction.actionOptions = options
flowAction.idGenerator = idGenerator
}
//temporary hack to support dynamic process running by tester
if config.Data == nil {
return flowAction
}
var flavor Flavor
err := json.Unmarshal(config.Data, &flavor)
if err != nil {
errorMsg := fmt.Sprintf("Error while loading flow '%s' error '%s'", config.Id, err.Error())
logger.Errorf(errorMsg)
panic(errorMsg)
}
if len(flavor.Flow) > 0 {
// It is an uncompressed and embedded flow
err := ep.GetFlowProvider().AddUncompressedFlow(config.Id, flavor.Flow)
if err != nil {
errorMsg := fmt.Sprintf("Error while loading uncompressed flow '%s' error '%s'", config.Id, err.Error())
logger.Errorf(errorMsg)
panic(errorMsg)
}
return flowAction
}
if len(flavor.FlowCompressed) > 0 {
// It is a compressed and embedded flow
err := ep.GetFlowProvider().AddCompressedFlow(config.Id, string(flavor.FlowCompressed[:]))
if err != nil {
errorMsg := fmt.Sprintf("Error while loading compressed flow '%s' error '%s'", config.Id, err.Error())
logger.Errorf(errorMsg)
panic(errorMsg)
}
return flowAction
}
if len(flavor.FlowURI) > 0 {
// It is a URI flow
err := ep.GetFlowProvider().AddFlowURI(config.Id, string(flavor.FlowURI[:]))
if err != nil {
errorMsg := fmt.Sprintf("Error while loading flow URI '%s' error '%s'", config.Id, err.Error())
logger.Errorf(errorMsg)
panic(errorMsg)
}
return flowAction
}
errorMsg := fmt.Sprintf("No flow found in action data for id '%s'", config.Id)
logger.Errorf(errorMsg)
panic(errorMsg)
return flowAction
}
//Config get the Action's config
func (fa *FlowAction) Config() *action.Config {
return fa.config
}
//Metadata get the Action's metadata
func (fa *FlowAction) Metadata() *action.Metadata {
return nil
}
// Run implements action.Action.Run
//func (fa *FlowAction) Run(context context.Context, uri string, options interface{}, handler action.ResultHandler) error {
func (fa *FlowAction) Run(context context.Context, inputs []*data.Attribute, options map[string]interface{}, handler action.ResultHandler) error {
op := instance.OpStart
retID := false
var initialState *instance.Instance
var flowURI string
mh := data.GetMapHelper()
oldOptions, old := options["deprecated_options"]
var execOptions *instance.ExecOptions
if old {
ro, ok := oldOptions.(*instance.RunOptions)
if ok {
op = ro.Op
retID = ro.ReturnID
initialState = ro.InitialState
flowURI = ro.FlowURI
execOptions = ro.ExecOptions
}
} else {
//todo enable support for ExecOption when using new action options
if v, ok := mh.GetInt(options, "op"); ok {
op = v
}
if v, ok := mh.GetBool(options, "returnId"); ok {
retID = v
}
if v, ok := mh.GetString(options, "flowURI"); ok {
flowURI = v
}
if v, ok := options["initialState"]; ok {
if v, ok := v.(*instance.Instance); ok {
initialState = v
}
}
}
if flowURI == "" {
flowURI = fa.Config().Id
}
logger.Infof("In Flow Run uri: '%s'", flowURI)
//todo: catch panic
//todo: consider switch to URI to dictate flow operation (ex. flow://blah/resume)
var inst *instance.Instance
switch op {
case instance.OpStart:
flowDef, err := ep.GetFlowProvider().GetFlow(flowURI)
if err != nil {
return err
}
instanceID := fa.idGenerator.NextAsString()
logger.Debug("Creating Instance: ", instanceID)
inst = instance.New(instanceID, flowURI, flowDef, ep.GetFlowModel())
case instance.OpResume:
if initialState != nil {
inst = initialState
logger.Debug("Resuming Instance: ", inst.ID())
} else {
return errors.New("unable to resume instance, initial state not provided")
}
case instance.OpRestart:
if initialState != nil {
inst = initialState
instanceID := fa.idGenerator.NextAsString()
inst.Restart(instanceID, ep.GetFlowProvider())
logger.Debug("Restarting Instance: ", instanceID)
} else {
return errors.New("unable to restart instance, initial state not provided")
}
}
if execOptions != nil {
logger.Debugf("Applying Exec Options to instance: %s\n", inst.ID())
instance.ApplyExecOptions(inst, execOptions)
}
//todo how do we check if debug is enabled?
logInputs(inputs)
if op == instance.OpStart {
inst.Start(inputs)
} else {
inst.UpdateAttrs(inputs)
}
logger.Debugf("Executing instance: %s\n", inst.ID())
stepCount := 0
hasWork := true
inst.InitActionContext(fa.config, handler)
go func() {
defer handler.Done()
if !inst.Flow.ExplicitReply() || retID {
idAttr, _ := data.NewAttribute("id", data.STRING, inst.ID())
results := map[string]*data.Attribute{
"id": idAttr,
}
if old {
dataAttr, _ := data.NewAttribute("data", data.OBJECT, &instance.IDResponse{ID: inst.ID()})
results["data"] = dataAttr
codeAttr, _ := data.NewAttribute("code", data.INTEGER, 200)
results["code"] = codeAttr
}
handler.HandleResult(results, nil)
}
for hasWork && inst.Status() < instance.StatusCompleted && stepCount < fa.actionOptions.MaxStepCount {
stepCount++
logger.Debugf("Step: %d\n", stepCount)
hasWork = inst.DoStep()
if fa.actionOptions.Record {
ep.GetStateRecorder().RecordSnapshot(inst)
ep.GetStateRecorder().RecordStep(inst)
}
}
if inst.Status() == instance.StatusCompleted {
returnData, err := inst.GetReturnData()
handler.HandleResult(returnData, err)
}
//if retID {
//
// resp := map[string]*data.Attribute {
// "id": data.NewAttribute("id", data.STRING, inst.ID()),
// }
//
// if old {
// resp["data"] = data.NewAttribute("data", data.OBJECT, &instance.IDResponse{ID: inst.ID()})
// resp["code"] = data.NewAttribute("code", data.INTEGER, 200)
// }
//
// handler.HandleResult(resp, nil)
//}
logger.Debugf("Done Executing A.instance [%s] - Status: %d\n", inst.ID(), inst.Status())
if inst.Status() == instance.StatusCompleted {
logger.Infof("Flow [%s] Completed", inst.ID())
}
}()
return nil
}
func logInputs(attrs []*data.Attribute) {
if len(attrs) > 0 {
logger.Debug("Input Attributes:")
for _, attr := range attrs {
if attr == nil {
logger.Error("Nil Attribute passed as input")
} else {
logger.Debugf(" Attr:%s, Type:%s, Value:%v", attr.Name(), attr.Type().String(), attr.Value())
}
}
}
}
func extractAttributes(inputs map[string]interface{}) []*data.Attribute {
size := len(inputs)
attrs := make([]*data.Attribute, 0, size)
//todo do special handling for complex_object metadata (merge or ref it)
for _, value := range inputs {
attr, _ := value.(*data.Attribute)
attrs = append(attrs, attr)
}
return attrs
}