-
Notifications
You must be signed in to change notification settings - Fork 66
/
trigger.go
91 lines (70 loc) · 1.75 KB
/
trigger.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
package channel
import (
"context"
"fmt"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/engine/channels"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/trigger"
)
var triggerMd = trigger.NewMetadata(&HandlerSettings{}, &Output{})
func init() {
_ = trigger.Register(&Trigger{}, &Factory{})
}
type Factory struct {
}
// Metadata implements trigger.Factory.Metadata
func (*Factory) Metadata() *trigger.Metadata {
return triggerMd
}
// New implements trigger.Factory.New
func (*Factory) New(config *trigger.Config) (trigger.Trigger, error) {
return &Trigger{}, nil
}
type Trigger struct {
}
func (t *Trigger) Initialize(ctx trigger.InitContext) error {
// validate handlers
for _, handler := range ctx.GetHandlers() {
s := &HandlerSettings{}
err := metadata.MapToStruct(handler.Settings(), s, true)
if err != nil {
return err
}
ch := channels.Get(s.Channel)
if ch == nil {
return fmt.Errorf("unknown engine channel '%s'", s.Channel)
}
l := &Listener{handler: handler, logger:ctx.Logger()}
err = ch.RegisterCallback(l.OnMessage)
if err != nil {
return err
}
}
return nil
}
// Stop implements util.Managed.Start
func (t *Trigger) Start() error {
return nil
}
// Stop implements util.Managed.Stop
func (t *Trigger) Stop() error {
return nil
}
type Listener struct {
handler trigger.Handler
logger log.Logger
}
func (l *Listener) OnMessage(msg interface{}) {
triggerData := make(map[string]interface{})
if values, ok := msg.(map[string]interface{}); ok {
triggerData[ovData] = values
} else {
triggerData[ovData] = msg
}
//todo what should we do with the results?
_, err := l.handler.Handle(context.TODO(), triggerData)
if err != nil {
l.logger.Error(err)
}
}