forked from project-flogo/stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
186 lines (142 loc) · 4.47 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
package stream
import (
"context"
"errors"
"fmt"
"strings"
"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/mapper"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/engine/channels"
"github.com/project-flogo/core/support"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/stream/pipeline"
)
func init() {
_ = action.Register(&StreamAction{}, &ActionFactory{})
}
var manager *pipeline.Manager
var actionMd = action.ToMetadata(&Settings{})
var logger log.Logger
var idGenerator *support.Generator
type Settings struct {
StreamURI string `md:"streamURI"`
PipelineURI string `md:"pipelineURI"`
GroupBy string `md:"groupBy"`
OutputChannel string `md:"outputChannel"`
}
type ActionFactory struct {
resManager *resource.Manager
}
func (f *ActionFactory) Initialize(ctx action.InitContext) error {
f.resManager = ctx.ResourceManager()
logger = log.ChildLogger(log.RootLogger(), "stream")
if manager != nil {
return nil
}
mapperFactory := mapper.NewFactory(pipeline.GetDataResolver())
if idGenerator == nil {
idGenerator, _ = support.NewGenerator()
}
manager = pipeline.NewManager()
err := resource.RegisterLoader(pipeline.ResType, pipeline.NewResourceLoader(mapperFactory, pipeline.GetDataResolver()))
err = resource.RegisterLoader(pipeline.ResTypeOld, pipeline.NewResourceLoader(mapperFactory, pipeline.GetDataResolver()))
return err
}
func (f *ActionFactory) New(config *action.Config) (action.Action, error) {
settings := &Settings{}
err := metadata.MapToStruct(config.Settings, settings, true)
if err != nil {
return nil, err
}
if settings.PipelineURI != "" && settings.StreamURI == "" {
settings.StreamURI = settings.PipelineURI
}
if settings.StreamURI == "" {
return nil, fmt.Errorf("stream URI not specified")
}
streamAction := &StreamAction{}
if strings.HasPrefix(settings.StreamURI, resource.UriScheme) {
res := f.resManager.GetResource(settings.StreamURI)
if res != nil {
def, ok := res.Object().(*pipeline.Definition)
if !ok {
return nil, errors.New("unable to resolve stream: " + settings.StreamURI)
}
streamAction.definition = def
} else {
return nil, errors.New("unable to resolve stream: " + settings.StreamURI)
}
} else {
def, err := manager.GetPipeline(settings.StreamURI)
if err != nil {
return nil, err
} else {
if def == nil {
return nil, errors.New("unable to resolve stream: " + settings.StreamURI)
}
}
streamAction.definition = def
}
streamAction.ioMetadata = streamAction.definition.Metadata()
if settings.OutputChannel != "" {
ch := channels.Get(settings.OutputChannel)
if ch == nil {
return nil, fmt.Errorf("engine channel `%s` not registered", settings.OutputChannel)
}
streamAction.outChannel = ch
}
instId := idGenerator.NextAsString()
logger.Debug("Creating Stream Instance: ", instId)
instLogger := logger
if log.CtxLoggingEnabled() {
instLogger = log.ChildLoggerWithFields(logger, log.FieldString("pipelineName", streamAction.definition.Name()), log.FieldString("pipelineId", instId))
}
//note: single pipeline instance for the moment
inst := pipeline.NewInstance(streamAction.definition, instId, settings.GroupBy == "", streamAction.outChannel, instLogger)
streamAction.inst = inst
return streamAction, nil
}
type StreamAction struct {
ioMetadata *metadata.IOMetadata
definition *pipeline.Definition
outChannel channels.Channel
inst *pipeline.Instance
groupBy string
}
func (s *StreamAction) Info() *action.Info {
panic("implement me")
}
func (s *StreamAction) Metadata() *action.Metadata {
return actionMd
}
func (s *StreamAction) IOMetadata() *metadata.IOMetadata {
return s.ioMetadata
}
func (s *StreamAction) Run(context context.Context, inputs map[string]interface{}, handler action.ResultHandler) error {
discriminator := ""
if s.groupBy != "" {
//navigate input
//note: for now groupings are determined by inputs to the action
value, ok := inputs[s.groupBy]
if ok {
discriminator, _ = coerce.ToString(value)
}
}
logger.Debugf("Running pipeline")
go func() {
defer handler.Done()
retData, status, err := s.inst.Run(discriminator, inputs)
if err != nil {
handler.HandleResult(nil, err)
} else {
handler.HandleResult(retData, err)
}
if s.outChannel != nil && status == pipeline.ExecStatusCompleted {
s.outChannel.Publish(retData)
}
}()
return nil
}