-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathaction.go
492 lines (411 loc) · 13.8 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package flow
import (
"context"
"errors"
"fmt"
"github.com/project-flogo/core/data/expression/script/gocc/ast"
"strings"
"time"
"github.com/project-flogo/core/action"
"github.com/project-flogo/core/app/resource"
"github.com/project-flogo/core/data/coerce"
"github.com/project-flogo/core/data/expression"
"github.com/project-flogo/core/data/mapper"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/support/service"
"github.com/project-flogo/core/support/trace"
"github.com/project-flogo/core/trigger"
"github.com/project-flogo/flow/definition"
"github.com/project-flogo/flow/instance"
"github.com/project-flogo/flow/model"
"github.com/project-flogo/flow/model/simple"
"github.com/project-flogo/flow/state"
flowsupport "github.com/project-flogo/flow/support"
"github.com/project-flogo/flow/util"
)
func init() {
_ = action.Register(&FlowAction{}, &ActionFactory{})
_ = resource.RegisterLoader(flowsupport.ResTypeFlow, &flowsupport.FlowLoader{})
}
const (
StateRecordingMode = "stateRecordingMode"
// Deprecated
RtSettingStepMode = "stepRecordingMode"
RtSettingSnapshotMode = "snapshotRecordingMode"
)
var idGenerator *support.Generator
var maxStepCount = util.GetMaxStepCount()
var actionMd = action.ToMetadata(&Settings{})
var logger log.Logger
var flowManager *flowsupport.FlowManager
var stateRecorder state.Recorder
var stateRecordingMode = state.RecordingModeOff
type ActionFactory struct {
resManager *resource.Manager
}
func (f *ActionFactory) Initialize(ctx action.InitContext) error {
f.resManager = ctx.ResourceManager()
logger = log.ChildLogger(log.RootLogger(), "flow")
if flowManager != nil {
return nil
}
sm := ctx.ServiceManager()
srService := sm.FindService(func(s service.Service) bool {
_, ok := s.(state.Recorder)
return ok
})
if len(ctx.RuntimeSettings()) > 0 {
mode, ok := ctx.RuntimeSettings()[StateRecordingMode]
if !ok {
// For backward compatible
sStepMode := ctx.RuntimeSettings()[RtSettingStepMode]
sSnapshotMode := ctx.RuntimeSettings()[RtSettingSnapshotMode]
stepMode, _ := coerce.ToString(sStepMode)
snapshotMode, _ := coerce.ToString(sSnapshotMode)
recordSteps := strings.EqualFold("full", stepMode)
recordSnapshot := strings.EqualFold("full", snapshotMode)
if recordSteps && recordSnapshot {
stateRecordingMode = state.RecordingModeFull
} else if recordSteps {
stateRecordingMode = state.RecordingModeStep
} else if recordSnapshot {
stateRecordingMode = state.RecordingModeSnapshot
} else {
stateRecordingMode = state.RecordingModeOff
}
} else {
var err error
stateRecordingMode, err = state.ToRecordingMode(mode)
if err != nil {
return nil
}
}
}
if srService != nil {
stateRecorder = srService.(state.Recorder)
if state.RecordSteps(stateRecordingMode) {
instance.EnableChangeTracking(true, stateRecordingMode)
}
}
exprFactory := expression.NewFactory(definition.GetDataResolver())
mapperFactory := mapper.NewFactory(definition.GetDataResolver())
definition.SetMapperFactory(mapperFactory)
definition.SetExprFactory(exprFactory)
if idGenerator == nil {
idGenerator, _ = support.NewGenerator()
}
//todo fix the following
model.RegisterDefault(simple.New())
flowManager = flowsupport.NewFlowManager(nil)
flowsupport.InitDefaultDefLookup(flowManager, ctx.ResourceManager())
return nil
}
func (f *ActionFactory) New(config *action.Config) (action.Action, error) {
flowAction := &FlowAction{}
settings := &Settings{}
err := metadata.MapToStruct(config.Settings, settings, true)
if err != nil {
return nil, fmt.Errorf("action settings error: %s", err.Error())
}
flowAction.flowURI = settings.FlowURI
def, res, err := flowsupport.GetDefinition(flowAction.flowURI)
if err != nil {
return nil, err
}
if def == nil {
return nil, errors.New("unable to resolve flow: " + flowAction.flowURI)
}
flowAction.ioMetadata = def.Metadata()
if res {
flowAction.resFlow = def
}
return flowAction, nil
}
type FlowAction struct {
flowURI string
resFlow *definition.Definition
ioMetadata *metadata.IOMetadata
info *action.Info
}
func (fa *FlowAction) Info() *action.Info {
return fa.info
}
// Metadata get the Action's metadata
func (fa *FlowAction) Metadata() *action.Metadata {
return actionMd
}
func (fa *FlowAction) IOMetadata() *metadata.IOMetadata {
return fa.ioMetadata
}
// Run implements action.Action.Run
func (fa *FlowAction) Run(ctx context.Context, inputs map[string]interface{}, handler action.ResultHandler) error {
var err error
op := instance.OpStart
retID := false
var initialState *instance.IndependentInstance
var flowURI string
var preserveInstanceId, originalInstanceId string
var initStepId int
var rerun, detachExecution bool
runOptions, exists := inputs["_run_options"]
var execOptions *instance.ExecOptions
if exists {
ro, ok := runOptions.(*instance.RunOptions)
if ok {
op = ro.Op
retID = ro.ReturnID
preserveInstanceId = ro.PreservedInstanceId
initialState = ro.InitialState
flowURI = ro.FlowURI
execOptions = ro.ExecOptions
initStepId = ro.InitStepId
rerun = ro.Rerun
originalInstanceId = ro.OriginalInstanceId
detachExecution = ro.DetachExecution
}
}
delete(inputs, "_run_options")
if flowURI == "" {
flowURI = fa.flowURI
}
if flowURI == "" {
return fmt.Errorf("cannot run flow, flowURI not specified")
}
logger.Debugf("Running FlowAction for URI: '%s'", flowURI)
//todo: catch panic
//todo: consider switch to URI to dictate flow operation (ex. flow://blah/resume)
var inst *instance.IndependentInstance
switch op {
case instance.OpStart:
flowDef := fa.resFlow
if flowDef == nil {
flowDef, err = flowManager.GetFlow(flowURI)
if err != nil {
return err
}
if flowDef == nil {
return errors.New("flow not found for URI: " + flowURI)
}
}
var instanceID string
if len(preserveInstanceId) > 0 {
instanceID = preserveInstanceId
} else {
instanceID = idGenerator.NextAsString()
}
logger.Debug("Creating Flow Instance: ", instanceID)
logger.Debugf("Creating Flow Instance [%s] for event id [%s] ", instanceID, trigger.GetHandlerEventIdFromContext(ctx))
instLogger := logger
if log.CtxLoggingEnabled() {
instLogger = log.ChildLoggerWithFields(logger, log.FieldString("flowName", flowDef.Name()), log.FieldString("flowId", instanceID), log.FieldString("eventId", trigger.GetHandlerEventIdFromContext(ctx)))
}
inst, err = instance.NewIndependentInstance(instanceID, flowURI, flowDef, instance.NewStateInstanceRecorder(stateRecorder, stateRecordingMode, rerun), instLogger)
if err != nil {
return err
}
case instance.OpRestart:
if initialState != nil {
inst = initialState
var instanceID string
if len(preserveInstanceId) > 0 {
instanceID = preserveInstanceId
} else {
instanceID = idGenerator.NextAsString()
}
logger.Debug("Restarting Flow Instance: ", instanceID)
instLogger := logger
if log.CtxLoggingEnabled() {
instLogger = log.ChildLoggerWithFields(logger, log.FieldString("flowName", inst.Name()), log.FieldString("flowId", instanceID))
}
inst.SetInstanceRecorder(instance.NewStateInstanceRecorder(stateRecorder, stateRecordingMode, rerun))
//Engine should set init step id one step before current restart step
err := inst.Restart(instLogger, instanceID, initStepId-1)
if err != nil {
return err
}
} else {
return errors.New("unable to restart instance, initial state not provided")
}
case instance.OpResume:
if initialState != nil {
inst = initialState
logger.Debug("Resuming Flow Instance: ", inst.ID())
//instLogger := logger
//
//if log.CtxLoggingEnabled() {
// instLogger = log.ChildLoggerWithFields(logger, log.FieldString("flowName", inst.Name()), log.FieldString("flowId", instanceID))
//}
} else {
return errors.New("unable to resume instance, initial state not provided")
}
}
if execOptions != nil {
logger.Debugf("Applying Exec Options to instance: %s", inst.ID())
instance.ApplyExecOptions(inst, execOptions)
}
//Update flow starting time
inst.UpdateStartTime()
if stateRecorder != nil {
flowState := inst.GetFlowState(inputs)
flowState.OriginalInstanceId = originalInstanceId
stateRecorder.RecordStart(flowState)
}
eventID := trigger.GetHandlerEventIdFromContext(ctx)
if eventID != "" {
// Add eventId to the instance
_ = inst.SetValue(instance.EventIdAttr, eventID)
}
if trace.Enabled() {
tc, err := trace.GetTracer().StartTrace(inst.SpanConfig(), trace.ExtractTracingContext(ctx))
if err != nil {
return err
}
if eventID != "" {
tc.SetTag("flogo_event_id", eventID)
}
inst.SetTracingContext(tc)
}
//todo how do we check if debug is enabled?
//logInputs(inputs)
logger.Infof("Executing Flow Instance [%s] for event id [%s]", inst.ID(), trigger.GetHandlerEventIdFromContext(ctx))
if op == instance.OpStart {
inst.Start(inputs)
} else {
inst.UpdateAttrs(inputs)
}
//initStepId cannot less than 1. restart must start with 1 to xxxx
stepCount := 0
if initStepId > 0 {
stepCount = initStepId - 1
}
hasWork := true
inst.SetResultHandler(handler)
if stateRecorder != nil {
//We don't need record step 0 if restart from activity
if initStepId <= 0 {
inst.RecordState(time.Now().UTC())
} else {
//Just increase the step number
inst.CurrentStep(true)
}
}
go func() {
if detachExecution {
// In detached mode, no reply expected. So, notifying handler.
handler.Done()
} else {
defer handler.Done()
}
if retID {
results := map[string]interface{}{
"id": inst.ID(),
}
handler.HandleResult(results, nil)
}
for hasWork && inst.Status() < model.FlowStatusCompleted && stepCount < maxStepCount {
stepCount++
logger.Debugf("Step: %d", stepCount)
taskStartTime := time.Now().UTC()
hasWork = inst.DoStep()
if stateRecorder != nil {
inst.RecordState(taskStartTime)
}
}
if stepCount == maxStepCount && inst.Status() != model.FlowStatusCompleted {
err := fmt.Errorf("Flow instance [%s] failed due to max step count [%d] reached. Increase step count by setting [%s] to higher value", inst.ID(), maxStepCount, util.FlogoStepCountEnv)
if inst.TracingContext() != nil {
_ = trace.GetTracer().FinishTrace(inst.TracingContext(), err)
}
handler.HandleResult(nil, err)
return
}
if inst.Status() == model.FlowStatusCompleted {
returnData, err := inst.GetReturnData()
if inst.TracingContext() != nil {
_ = trace.GetTracer().FinishTrace(inst.TracingContext(), nil)
}
for k, v := range returnData {
inst.SetValue(k, v)
}
fa.applyAssertionInterceptor(inst, flowsupport.AssertionActivity)
handler.HandleResult(returnData, err)
} else if inst.Status() == model.FlowStatusFailed {
hasFlowExceptionAssert := fa.applyAssertionInterceptor(inst, flowsupport.AssertionException)
if inst.TracingContext() != nil {
_ = trace.GetTracer().FinishTrace(inst.TracingContext(), inst.GetError())
}
// If we are in unit testing mode and flow has assertion exception then we dont make the flow fail
// The testcase will be passed or failed based on the assertions executed.
if hasFlowExceptionAssert {
handler.HandleResult(nil, nil)
}
handler.HandleResult(nil, inst.GetError())
}
logger.Debugf("Executing flow instance [%s] for event id [%s] - Status: %d", inst.ID(), trigger.GetHandlerEventIdFromContext(ctx), inst.Status())
if inst.Status() == model.FlowStatusCompleted {
logger.Infof("Flow Instance [%s] for event id [%s] completed in %s", inst.ID(), trigger.GetHandlerEventIdFromContext(ctx), inst.ExecutionTime().String())
} else if inst.Status() == model.FlowStatusFailed {
logger.Infof("Flow Instance [%s] for event id [%s] failed in %s", inst.ID(), trigger.GetHandlerEventIdFromContext(ctx), inst.ExecutionTime().String())
}
if stateRecorder != nil {
stateRecorder.RecordDone(inst.GetFlowState(inputs))
}
}()
return nil
}
func (fa *FlowAction) applyAssertionInterceptor(inst *instance.IndependentInstance, assertType int) bool {
if !inst.HasInterceptor() {
return false
}
hasFlowExceptionAssertion := false
if inst.GetInterceptor() != nil {
interceptor := inst.GetInterceptor().GetTaskInterceptor(inst.Instance.Name() + "-_flowOutput")
if interceptor != nil {
interceptor.Result = flowsupport.Pass
ef := expression.NewFactory(definition.GetDataResolver())
for id, assertion := range interceptor.Assertions {
if interceptor.Type != assertType {
interceptor.Assertions[id].Result = flowsupport.AssertionNotExecuted
continue
}
if assertion.Expression == "" {
interceptor.Assertions[id].Message = "Empty expression"
interceptor.Assertions[id].Result = flowsupport.NotExecuted
continue
}
expr, _ := ef.NewExpr(fmt.Sprintf("%v", assertion.Expression))
if expr == nil {
interceptor.Assertions[id].Result = flowsupport.Fail
interceptor.Assertions[id].Message = "Failed to validate expression"
continue
}
result, err := expr.Eval(inst.Instance)
if err != nil {
interceptor.Assertions[id].Result = flowsupport.Fail
interceptor.Assertions[id].Message = "Failed to evaluate expression"
} else {
exp, ok := expr.(ast.ExprEvalResult)
var resultData ast.ExprEvalData
if ok {
resultData = exp.Detail()
}
res, _ := coerce.ToBool(result)
if res {
interceptor.Assertions[id].Result = flowsupport.Pass
interceptor.Assertions[id].Message = "Comparison success"
} else {
interceptor.Assertions[id].Result = flowsupport.Fail
interceptor.Assertions[id].Message = "Comparison failure"
}
interceptor.Assertions[id].EvalResult = resultData
if assertType == flowsupport.AssertionException {
hasFlowExceptionAssertion = true
}
}
}
}
}
return hasFlowExceptionAssertion
}