-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflow.go
183 lines (139 loc) · 3.33 KB
/
flow.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
package flow
import (
"fmt"
"sync"
"github.com/gogap/config"
"github.com/gogap/context"
"github.com/sirupsen/logrus"
)
var (
defaultFlow = New()
)
type Flow struct {
handlers map[string]HandlerFunc
registerdHandlerNames []string
lock sync.RWMutex
}
type FlowTrans struct {
flow *Flow
ctx context.Context
firstFn HandlerFunc
err error
conf config.Configuration
}
func New() *Flow {
return &Flow{
handlers: make(map[string]HandlerFunc),
}
}
func (p *Flow) RegisterHandler(name string, handler HandlerFunc) (err error) {
if len(name) == 0 {
err = fmt.Errorf("handler name is emtpy")
return
}
if handler == nil {
err = fmt.Errorf("handler func could not be nil")
return
}
p.lock.Lock()
defer p.lock.Unlock()
_, exist := p.handlers[name]
if exist {
err = fmt.Errorf("handler %s already registered", name)
return
}
p.handlers[name] = handler
p.registerdHandlerNames = append(p.registerdHandlerNames, name)
return
}
func (p *Flow) ListHandlers() []string {
return p.registerdHandlerNames
}
func (p *Flow) Begin(ctx context.Context, opts ...config.Option) *FlowTrans {
conf := config.NewConfig(opts...)
return &FlowTrans{
flow: p,
ctx: ctx,
firstFn: voidTransBegin,
conf: conf,
}
}
func (p *Flow) Run(name string, ctx context.Context, opts ...config.Option) (err error) {
p.lock.RLock()
h, exist := p.handlers[name]
p.lock.RUnlock()
if !exist {
err = fmt.Errorf("handler %s not exist", name)
return
}
conf := config.NewConfig(opts...)
err = h.Run(ctx, conf)
return
}
func RegisterHandler(name string, handler HandlerFunc) (err error) {
return defaultFlow.RegisterHandler(name, handler)
}
func ListHandlers() []string {
return defaultFlow.ListHandlers()
}
func Run(name string, ctx context.Context, opts ...config.Option) (err error) {
return defaultFlow.Run(name, ctx, opts...)
}
func Begin(ctx context.Context, opts ...config.Option) *FlowTrans {
return defaultFlow.Begin(ctx, opts...)
}
func (p *FlowTrans) Then(name string, opts ...config.Option) *FlowTrans {
if p.err != nil {
return p
}
h, exist := p.flow.handlers[name]
if !exist {
p.err = fmt.Errorf("handler %s not exist", name)
return p
}
p.firstFn = p.firstFn.Then(handlerHook(name, h, opts...), p.conf)
return p
}
func handlerHook(name string, fn HandlerFunc, opts ...config.Option) HandlerFunc {
return func(ctx context.Context, conf config.Configuration) error {
var nextConf config.Configuration
if len(opts) == 0 {
nextConf = conf
} else {
nextConf = config.NewConfig(opts...)
logrus.WithField("name", name).Debugln("Config loaded")
}
logrus.WithField("name", name).Debugln("Before execute")
err := fn(ctx, nextConf)
logrus.WithField("name", name).Debugln("After execute")
return err
}
}
func (p *FlowTrans) Subscribe(subscribers ...SubscriberFunc) *FlowTrans {
if p.err != nil {
return p
}
if p.firstFn == nil {
return p
}
p.firstFn = p.firstFn.Subscribe(subscribers...)
return p
}
func (p *FlowTrans) Commit() error {
if p.err != nil {
return p.err
}
var ctx context.Context
if p.ctx == nil {
ctx = context.NewContext()
} else {
ctx = p.ctx
}
return p.firstFn.Run(ctx, nil)
}
func (p *FlowTrans) Output() []NameValue {
return ListOutput(p.ctx)
}
func voidTransBegin(ctx context.Context, conf config.Configuration) error {
return nil
}