-
Notifications
You must be signed in to change notification settings - Fork 80
/
options.go
257 lines (211 loc) · 5.5 KB
/
options.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package machinery
import (
"crypto/tls"
"time"
"github.com/RichardKnop/machinery/v2/config"
"github.com/RichardKnop/machinery/v2/tasks"
)
type BrokerType int
type BackendType int
type LockType int
const (
BrokerTypeRedis BrokerType = iota // Redis
BrokerTypeAmqp // AMQP
BrokerTypeGcpPubSub // GCP Pub/Sub
BrokerTypeSQS // AWS SQS
)
const (
BackendTypeRedis BackendType = iota // Redis
BackendTypeAmqp // AMQP
BackendTypeMemcache // Memcache
BackendTypeMongoDB // MongoDB
BackendTypeDynamoDB // Amazon DynamoDB
)
const (
LockTypeRedis LockType = iota
)
type brokerOption struct {
brokerType BrokerType
db int
projectID, subscriptionName string
}
type backendOption struct {
backendType BackendType
db int
}
type lockOption struct {
lockType LockType
db int
retries int
}
type consumerOption struct {
consumerTag string // 消费者的标记
concurrency int // 并发数, 0表示不限制
queue string
}
//////////////////////////////////////////////////////////////////////////////////////////////////
type ServerOption func(o *Server)
// WithYamlConfig read config from yaml file.
func WithYamlConfig(cnfPath string, keepReloading bool) ServerOption {
return func(s *Server) {
cnf, err := config.NewFromYaml(cnfPath, keepReloading)
if err != nil {
LogErrorf("load yaml config [%s] failed: %s", cnfPath, err.Error())
}
s.cfg = cnf
}
}
// WithEnvironmentConfig read config from env.
func WithEnvironmentConfig() ServerOption {
return func(s *Server) {
cnf, err := config.NewFromEnvironment()
if err != nil {
LogErrorf("load environment config failed: %s", err.Error())
}
s.cfg = cnf
}
}
func WithTLSConfig(c *tls.Config) ServerOption {
return func(s *Server) {
if s.cfg == nil {
s.cfg = &config.Config{}
}
s.cfg.TLSConfig = c
}
}
// WithEnableKeepAlive enable keep alive
func WithEnableKeepAlive(enable bool) ServerOption {
return func(s *Server) {
s.enableKeepAlive = enable
}
}
func WithBrokerAddress(addr string, db int, brokerType BrokerType) ServerOption {
return func(s *Server) {
s.cfg.Broker = addr
s.brokerOption.db = db
s.brokerOption.brokerType = brokerType
}
}
func WithResultBackendAddress(addr string, db int, backendType BackendType) ServerOption {
return func(s *Server) {
s.cfg.ResultBackend = addr
s.backendOption.db = db
s.backendOption.backendType = backendType
}
}
func WithLockAddress(addr string, db, retries int, lockType LockType) ServerOption {
return func(s *Server) {
s.cfg.Lock = addr
s.lockOption.db = db
s.lockOption.retries = retries
s.lockOption.lockType = lockType
}
}
func WithRedisConfig(cfg *config.RedisConfig) ServerOption {
return func(s *Server) {
s.cfg.Redis = cfg
}
}
func WithAMQPConfig(cfg *config.AMQPConfig) ServerOption {
return func(s *Server) {
s.cfg.AMQP = cfg
}
}
func WithSQSConfig(cfg *config.SQSConfig) ServerOption {
return func(s *Server) {
s.cfg.SQS = cfg
}
}
func WithGCPPubSubConfig(cfg *config.GCPPubSubConfig) ServerOption {
return func(s *Server) {
s.cfg.GCPPubSub = cfg
}
}
func WithMongoDBConfig(cfg *config.MongoDBConfig) ServerOption {
return func(s *Server) {
s.cfg.MongoDB = cfg
}
}
func WithDynamoDBConfig(cfg *config.DynamoDBConfig) ServerOption {
return func(s *Server) {
s.cfg.DynamoDB = cfg
}
}
func WithConsumerOption(consumerTag string, concurrency int, queue string) ServerOption {
return func(s *Server) {
s.consumerOption.consumerTag = consumerTag
s.consumerOption.concurrency = concurrency
s.consumerOption.queue = queue
}
}
func WithDefaultQueue(name string) ServerOption {
return func(s *Server) {
s.cfg.DefaultQueue = name
}
}
func WithResultsExpireIn(expire int) ServerOption {
return func(s *Server) {
s.cfg.ResultsExpireIn = expire
}
}
func WithNoUnixSignals(noUnixSignals bool) ServerOption {
return func(s *Server) {
s.cfg.NoUnixSignals = noUnixSignals
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
type TaskOption func(o *tasks.Signature)
func WithDelayTime(delayTime time.Time) TaskOption {
return func(o *tasks.Signature) {
o.ETA = &delayTime
}
}
func WithRetryCount(count int) TaskOption {
return func(o *tasks.Signature) {
o.RetryCount = count
}
}
func WithRetryTimeout(timeout int) TaskOption {
return func(o *tasks.Signature) {
o.RetryTimeout = timeout
}
}
func WithHeaders(headers tasks.Headers) TaskOption {
return func(o *tasks.Signature) {
o.Headers = headers
}
}
func WithHeader(key string, value interface{}) TaskOption {
return func(o *tasks.Signature) {
if o.Headers == nil {
o.Headers = tasks.Headers{}
}
o.Headers[key] = value
}
}
func WithRoutingKey(key string) TaskOption {
return func(o *tasks.Signature) {
o.RoutingKey = key
}
}
func WithPriority(priority uint8) TaskOption {
return func(o *tasks.Signature) {
o.Priority = priority
}
}
func WithArgument(typeName string, value interface{}) TaskOption {
return func(o *tasks.Signature) {
o.Args = append(o.Args, tasks.Arg{Type: typeName, Value: value})
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
type TasksOption func(o *[]*tasks.Signature)
func WithTask(typeName string, opts ...TaskOption) TasksOption {
return func(s *[]*tasks.Signature) {
signature := &tasks.Signature{Name: typeName}
for _, o := range opts {
o(signature)
}
*s = append(*s, signature)
}
}