-
Notifications
You must be signed in to change notification settings - Fork 16
/
middleware.go
162 lines (133 loc) · 5.49 KB
/
middleware.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
package semaphore
import (
"context"
"github.com/jexia/semaphore/v2/pkg/broker"
"github.com/jexia/semaphore/v2/pkg/flow"
"github.com/jexia/semaphore/v2/pkg/functions"
"github.com/jexia/semaphore/v2/pkg/references"
"github.com/jexia/semaphore/v2/pkg/specs"
)
// WithMiddleware initialises the given middleware and defines all options
func WithMiddleware(middleware Middleware) Option {
return func(ctx *broker.Context, options *Options) {
options.Middleware = append(options.Middleware, middleware)
}
}
// WithBeforeConstructor the passed function gets called before new specifications are constructed
func WithBeforeConstructor(wrapper BeforeConstructorHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.BeforeConstructor == nil {
options.BeforeConstructor = wrapper(func(*broker.Context, functions.Collection, Options) error { return nil })
return
}
options.BeforeConstructor = wrapper(options.BeforeConstructor)
}
}
// BeforeManagerDo the passed function gets called before a request gets handled by a flow manager
func BeforeManagerDo(wrapper flow.BeforeManagerHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.BeforeManagerDo == nil {
options.BeforeManagerDo = wrapper(func(ctx context.Context, manager *flow.Manager, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.BeforeManagerDo = wrapper(options.BeforeManagerDo)
}
}
// BeforeManagerRollback the passed function gets called before a rollback request gets handled by a flow manager
func BeforeManagerRollback(wrapper flow.BeforeManagerHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.BeforeManagerRollback == nil {
options.BeforeManagerRollback = wrapper(func(ctx context.Context, manager *flow.Manager, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.BeforeManagerRollback = wrapper(options.BeforeManagerRollback)
}
}
// AfterManagerDo the passed function gets after a flow has been handled by the flow manager
func AfterManagerDo(wrapper flow.AfterManagerHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.AfterManagerDo == nil {
options.AfterManagerDo = wrapper(func(ctx context.Context, manager *flow.Manager, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.AfterManagerDo = wrapper(options.AfterManagerDo)
}
}
// AfterManagerRollback the passed function gets after a flow rollback has been handled by the flow manager
func AfterManagerRollback(wrapper flow.AfterManagerHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.AfterManagerRollback == nil {
options.AfterManagerRollback = wrapper(func(ctx context.Context, manager *flow.Manager, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.AfterManagerRollback = wrapper(options.AfterManagerRollback)
}
}
// BeforeNodeDo the passed function gets called before a node is executed
func BeforeNodeDo(wrapper flow.BeforeNodeHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.BeforeNodeDo == nil {
options.BeforeNodeDo = wrapper(func(ctx context.Context, node *flow.Node, tracker flow.Tracker, processes *flow.Processes, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.BeforeNodeDo = wrapper(options.BeforeNodeDo)
}
}
// BeforeNodeRollback the passed function gets called before a node rollback is executed
func BeforeNodeRollback(wrapper flow.BeforeNodeHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.BeforeNodeRollback == nil {
options.BeforeNodeRollback = wrapper(func(ctx context.Context, node *flow.Node, tracker flow.Tracker, processes *flow.Processes, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.BeforeNodeRollback = wrapper(options.BeforeNodeRollback)
}
}
// AfterNodeDo the passed function gets called after a node is executed
func AfterNodeDo(wrapper flow.AfterNodeHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.AfterNodeDo == nil {
options.AfterNodeDo = wrapper(func(ctx context.Context, node *flow.Node, tracker flow.Tracker, processes *flow.Processes, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.AfterNodeDo = wrapper(options.AfterNodeDo)
}
}
// AfterNodeRollback the passed function gets called after a node rollback is executed
func AfterNodeRollback(wrapper flow.AfterNodeHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.AfterNodeRollback == nil {
options.AfterNodeRollback = wrapper(func(ctx context.Context, node *flow.Node, tracker flow.Tracker, processes *flow.Processes, store references.Store) (context.Context, error) {
return ctx, nil
})
return
}
options.AfterNodeRollback = wrapper(options.AfterNodeRollback)
}
}
// AfterFlowConstructor the passed function gets called before a flow manager gets constructed
func AfterFlowConstructor(wrapper AfterFlowConstructionHandler) Option {
return func(ctx *broker.Context, options *Options) {
if options.AfterFlowConstruction == nil {
options.AfterFlowConstruction = wrapper(func(*broker.Context, specs.FlowInterface, *flow.Manager) error {
return nil
})
return
}
options.AfterFlowConstruction = wrapper(options.AfterFlowConstruction)
}
}