forked from project-flogo/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instances.go
217 lines (169 loc) · 5.47 KB
/
instances.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
package app
import (
"fmt"
"github.com/qingcloudhx/core/action"
"github.com/qingcloudhx/core/data/expression"
"github.com/qingcloudhx/core/data/mapper"
"github.com/qingcloudhx/core/data/resolve"
"github.com/qingcloudhx/core/support"
"github.com/qingcloudhx/core/support/log"
"github.com/qingcloudhx/core/support/managed"
"github.com/qingcloudhx/core/trigger"
)
func (a *App) createSharedActions(actionConfigs []*action.Config) (map[string]action.Action, error) {
actions := make(map[string]action.Action)
for _, config := range actionConfigs {
if config.Ref == "" && config.Type != "" {
log.RootLogger().Warnf("action configuration 'type' deprecated, use 'ref' in the future")
config.Ref = "#" + config.Type
}
if config.Ref == "" {
return nil, fmt.Errorf("ref not specified for action: %s", config.Id)
}
ref := config.Ref
if config.Ref[0] == '#' {
var ok bool
ref, ok = support.GetAliasRef("action", config.Ref)
if !ok {
return nil, fmt.Errorf("action '%s' not imported", config.Ref)
}
}
actionFactory := action.GetFactory(ref)
if actionFactory == nil {
return nil, fmt.Errorf("action factory '%s' not registered", ref)
}
act, err := actionFactory.New(config)
if err != nil {
return nil, err
}
actions[config.Id] = act
//if needsCleanup, ok := act.(support.NeedsCleanup); ok {
// a.toCleanup = append(a.toCleanup, needsCleanup)
//}
}
return actions, nil
}
func (a *App) createTriggers(tConfigs []*trigger.Config, runner action.Runner) (map[string]*triggerWrapper, error) {
triggers := make(map[string]*triggerWrapper)
mapperFactory := mapper.NewFactory(resolve.GetBasicResolver())
expressionFactory := expression.NewFactory(resolve.GetBasicResolver())
for _, tConfig := range tConfigs {
_, exists := triggers[tConfig.Id]
if exists {
return nil, fmt.Errorf("trigger with id '%s' already registered, trigger ids have to be unique", tConfig.Id)
}
if tConfig.Ref == "" && tConfig.Type != "" {
log.RootLogger().Warnf("trigger configuration 'type' deprecated, use 'ref' in the future")
tConfig.Ref = "#" + tConfig.Type
}
ref := tConfig.Ref
if tConfig.Ref == "" {
return nil, fmt.Errorf("ref not specified for trigger: %s", tConfig.Id)
}
if tConfig.Ref[0] == '#' {
var ok bool
ref, ok = support.GetAliasRef("trigger", tConfig.Ref)
if !ok {
return nil, fmt.Errorf("trigger '%s' not imported", tConfig.Ref)
}
}
triggerFactory := trigger.GetFactory(ref)
if triggerFactory == nil {
return nil, fmt.Errorf("trigger factory '%s' not registered", ref)
}
err := tConfig.FixUp(triggerFactory.Metadata())
if err != nil {
return nil, err
}
trg, err := triggerFactory.New(tConfig)
if err != nil {
return nil, err
}
if trg == nil {
return nil, fmt.Errorf("cannot create trigger nil for id '%s'", tConfig.Id)
}
logger := trigger.GetLogger(ref)
if log.CtxLoggingEnabled() {
logger = log.ChildLoggerWithFields(logger, log.FieldString("triggerId", tConfig.Id))
}
log.ChildLogger(logger, tConfig.Id)
initCtx := &initContext{logger: logger, handlers: make([]trigger.Handler, 0, len(tConfig.Handlers))}
//create handlers for that trigger and init
for _, hConfig := range tConfig.Handlers {
var acts []action.Action
var err error
if len(hConfig.Actions) == 0 {
return nil, fmt.Errorf("trigger '%s' has a handler with no action", tConfig.Id)
}
//use action if already associated with Handler
for _, act := range hConfig.Actions {
if act.Act != nil {
acts = append(acts, act.Act)
} else {
//todo action id is empty
if id := act.Id; id != "" {
act, _ := a.actions[id]
if act == nil {
return nil, fmt.Errorf("shared action '%s' does not exists", id)
}
acts = append(acts, act)
} else {
//create the action
if act.Ref == "" && act.Type != "" {
log.RootLogger().Warnf("action configuration 'type' deprecated, use 'ref' in the future")
act.Ref = "#" + act.Type
}
if act.Ref == "" {
return nil, fmt.Errorf("ref not specified for action in trigger '%s", tConfig.Id)
}
ref := act.Ref
if act.Ref[0] == '#' {
var ok bool
ref, ok = support.GetAliasRef("action", act.Ref)
if !ok {
return nil, fmt.Errorf("action '%s' not imported", act.Ref)
}
}
actionFactory := action.GetFactory(ref)
if actionFactory == nil {
return nil, fmt.Errorf("action factory '%s' not registered", ref)
}
act, err := actionFactory.New(act.Config)
if err != nil {
return nil, err
}
//if needsDisposal, ok := act.(support.NeedsCleanup); ok {
// a.toDispose = append(a.toDispose, needsDisposal)
//}
acts = append(acts, act)
}
}
}
handler, err := trigger.NewHandler(hConfig, acts, mapperFactory, expressionFactory, runner)
if err != nil {
return nil, err
}
initCtx.handlers = append(initCtx.handlers, handler)
}
err = trg.Initialize(initCtx)
if err != nil {
return nil, err
}
//eventMapper,err := mapperFactory.NewMapper(tConfig.Event)
//if err != nil {
// return nil, err
//}
triggers[tConfig.Id] = &triggerWrapper{ref: ref, trg: trg, status: &managed.StatusInfo{Name: tConfig.Id}}
}
return triggers, nil
}
type initContext struct {
handlers []trigger.Handler
logger log.Logger
}
func (ctx *initContext) GetHandlers() []trigger.Handler {
return ctx.handlers
}
func (ctx *initContext) Logger() log.Logger {
return ctx.logger
}