forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factories.go
319 lines (268 loc) · 9.28 KB
/
factories.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package machinery
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
neturl "net/url"
"github.com/sh0umik/machinery/v1/config"
amqpbroker "github.com/sh0umik/machinery/v1/brokers/amqp"
eagerbroker "github.com/sh0umik/machinery/v1/brokers/eager"
gcppubsubbroker "github.com/sh0umik/machinery/v1/brokers/gcppubsub"
brokeriface "github.com/sh0umik/machinery/v1/brokers/iface"
redisbroker "github.com/sh0umik/machinery/v1/brokers/redis"
sqsbroker "github.com/sh0umik/machinery/v1/brokers/sqs"
amqpbackend "github.com/sh0umik/machinery/v1/backends/amqp"
dynamobackend "github.com/sh0umik/machinery/v1/backends/dynamodb"
eagerbackend "github.com/sh0umik/machinery/v1/backends/eager"
backendiface "github.com/sh0umik/machinery/v1/backends/iface"
memcachebackend "github.com/sh0umik/machinery/v1/backends/memcache"
mongobackend "github.com/sh0umik/machinery/v1/backends/mongo"
nullbackend "github.com/sh0umik/machinery/v1/backends/null"
redisbackend "github.com/sh0umik/machinery/v1/backends/redis"
eagerlock "github.com/sh0umik/machinery/v1/locks/eager"
lockiface "github.com/sh0umik/machinery/v1/locks/iface"
redislock "github.com/sh0umik/machinery/v1/locks/redis"
)
// BrokerFactory creates a new object of iface.Broker
// Currently only AMQP/S broker is supported
func BrokerFactory(cnf *config.Config) (brokeriface.Broker, error) {
if strings.HasPrefix(cnf.Broker, "amqp://") {
return amqpbroker.New(cnf), nil
}
if strings.HasPrefix(cnf.Broker, "amqps://") {
return amqpbroker.New(cnf), nil
}
if strings.HasPrefix(cnf.Broker, "redis://") || strings.HasPrefix(cnf.Broker, "rediss://") {
var scheme string
if strings.HasPrefix(cnf.Broker, "redis://") {
scheme = "redis://"
} else {
scheme = "rediss://"
}
parts := strings.Split(cnf.Broker, scheme)
if len(parts) != 2 {
return nil, fmt.Errorf(
"Redis broker connection string should be in format %shost:port, instead got %s", scheme,
cnf.Broker,
)
}
brokers := strings.Split(parts[1], ",")
if len(brokers) > 1 {
return redisbroker.NewGR(cnf, brokers, 0), nil
} else {
redisHost, redisPassword, redisDB, err := ParseRedisURL(cnf.Broker)
if err != nil {
return nil, err
}
return redisbroker.New(cnf, redisHost, redisPassword, "", redisDB), nil
}
}
if strings.HasPrefix(cnf.Broker, "redis+socket://") {
redisSocket, redisPassword, redisDB, err := ParseRedisSocketURL(cnf.Broker)
if err != nil {
return nil, err
}
return redisbroker.New(cnf, "", redisPassword, redisSocket, redisDB), nil
}
if strings.HasPrefix(cnf.Broker, "eager") {
return eagerbroker.New(), nil
}
if _, ok := os.LookupEnv("DISABLE_STRICT_SQS_CHECK"); ok {
//disable SQS name check, so that users can use this with local simulated SQS
//where sql broker url might not start with https://sqs
//even when disabling strict SQS naming check, make sure its still a valid http URL
if strings.HasPrefix(cnf.Broker, "https://") || strings.HasPrefix(cnf.Broker, "http://") {
return sqsbroker.New(cnf), nil
}
} else {
if strings.HasPrefix(cnf.Broker, "https://sqs") {
return sqsbroker.New(cnf), nil
}
}
if strings.HasPrefix(cnf.Broker, "gcppubsub://") {
projectID, subscriptionName, err := ParseGCPPubSubURL(cnf.Broker)
if err != nil {
return nil, err
}
return gcppubsubbroker.New(cnf, projectID, subscriptionName)
}
return nil, fmt.Errorf("Factory failed with broker URL: %v", cnf.Broker)
}
// BackendFactory creates a new object of backends.Interface
// Currently supported backends are AMQP/S and Memcache
func BackendFactory(cnf *config.Config) (backendiface.Backend, error) {
if strings.HasPrefix(cnf.ResultBackend, "amqp://") {
return amqpbackend.New(cnf), nil
}
if strings.HasPrefix(cnf.ResultBackend, "amqps://") {
return amqpbackend.New(cnf), nil
}
if strings.HasPrefix(cnf.ResultBackend, "memcache://") {
parts := strings.Split(cnf.ResultBackend, "memcache://")
if len(parts) != 2 {
return nil, fmt.Errorf(
"Memcache result backend connection string should be in format memcache://server1:port,server2:port, instead got %s",
cnf.ResultBackend,
)
}
servers := strings.Split(parts[1], ",")
return memcachebackend.New(cnf, servers), nil
}
if strings.HasPrefix(cnf.ResultBackend, "redis://") || strings.HasPrefix(cnf.ResultBackend, "rediss://") {
var scheme string
if strings.HasPrefix(cnf.ResultBackend, "redis://") {
scheme = "redis://"
} else {
scheme = "rediss://"
}
parts := strings.Split(cnf.ResultBackend, scheme)
addrs := strings.Split(parts[1], ",")
if len(addrs) > 1 {
return redisbackend.NewGR(cnf, addrs, 0), nil
} else {
redisHost, redisPassword, redisDB, err := ParseRedisURL(cnf.ResultBackend)
if err != nil {
return nil, err
}
return redisbackend.New(cnf, redisHost, redisPassword, "", redisDB), nil
}
}
if strings.HasPrefix(cnf.ResultBackend, "redis+socket://") {
redisSocket, redisPassword, redisDB, err := ParseRedisSocketURL(cnf.ResultBackend)
if err != nil {
return nil, err
}
return redisbackend.New(cnf, "", redisPassword, redisSocket, redisDB), nil
}
if strings.HasPrefix(cnf.ResultBackend, "mongodb://") ||
strings.HasPrefix(cnf.ResultBackend, "mongodb+srv://") {
return mongobackend.New(cnf)
}
if strings.HasPrefix(cnf.ResultBackend, "eager") {
return eagerbackend.New(), nil
}
if strings.HasPrefix(cnf.ResultBackend, "null") {
return nullbackend.New(), nil
}
if strings.HasPrefix(cnf.ResultBackend, "https://dynamodb") {
return dynamobackend.New(cnf), nil
}
return nil, fmt.Errorf("Factory failed with result backend: %v", cnf.ResultBackend)
}
// ParseRedisURL ...
func ParseRedisURL(url string) (host, password string, db int, err error) {
// redis://pwd@host/db
var u *neturl.URL
u, err = neturl.Parse(url)
if err != nil {
return
}
if u.Scheme != "redis" && u.Scheme != "rediss" {
err = errors.New("No redis scheme found")
return
}
if u.User != nil {
var exists bool
password, exists = u.User.Password()
if !exists {
password = u.User.Username()
}
}
host = u.Host
parts := strings.Split(u.Path, "/")
if len(parts) == 1 {
db = 0 //default redis db
} else {
db, err = strconv.Atoi(parts[1])
if err != nil {
db, err = 0, nil //ignore err here
}
}
return
}
// LockFactory creates a new object of iface.Lock
// Currently supported lock is redis
func LockFactory(cnf *config.Config) (lockiface.Lock, error) {
if strings.HasPrefix(cnf.Lock, "eager") {
return eagerlock.New(), nil
}
if strings.HasPrefix(cnf.Lock, "redis://") {
parts := strings.Split(cnf.Lock, "redis://")
if len(parts) != 2 {
return nil, fmt.Errorf(
"Redis broker connection string should be in format redis://host:port, instead got %s",
cnf.Lock,
)
}
locks := strings.Split(parts[1], ",")
return redislock.New(cnf, locks, 0, 3), nil
}
// Lock is required for periodic tasks to work, therefor return in memory lock in case none is configured
return eagerlock.New(), nil
}
// ParseRedisSocketURL extracts Redis connection options from a URL with the
// redis+socket:// scheme. This scheme is not standard (or even de facto) and
// is used as a transitional mechanism until the the config package gains the
// proper facilities to support socket-based connections.
func ParseRedisSocketURL(url string) (path, password string, db int, err error) {
parts := strings.Split(url, "redis+socket://")
if parts[0] != "" {
err = errors.New("No redis scheme found")
return
}
// redis+socket://password@/path/to/file.soc:/db
if len(parts) != 2 {
err = fmt.Errorf("Redis socket connection string should be in format redis+socket://password@/path/to/file.sock:/db, instead got %s", url)
return
}
remainder := parts[1]
// Extract password if any
parts = strings.SplitN(remainder, "@", 2)
if len(parts) == 2 {
password = parts[0]
remainder = parts[1]
} else {
remainder = parts[0]
}
// Extract path
parts = strings.SplitN(remainder, ":", 2)
path = parts[0]
if path == "" {
err = fmt.Errorf("Redis socket connection string should be in format redis+socket://password@/path/to/file.sock:/db, instead got %s", url)
return
}
if len(parts) == 2 {
remainder = parts[1]
}
// Extract DB if any
parts = strings.SplitN(remainder, "/", 2)
if len(parts) == 2 {
db, _ = strconv.Atoi(parts[1])
}
return
}
// ParseGCPPubSubURL Parse GCP Pub/Sub URL
// url: gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME
func ParseGCPPubSubURL(url string) (string, string, error) {
parts := strings.Split(url, "gcppubsub://")
if parts[0] != "" {
return "", "", errors.New("No gcppubsub scheme found")
}
if len(parts) != 2 {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
remainder := parts[1]
parts = strings.Split(remainder, "/")
if len(parts) == 2 {
if parts[0] == "" {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
if parts[1] == "" {
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}
return parts[0], parts[1], nil
}
return "", "", fmt.Errorf("gcppubsub scheme should be in format gcppubsub://YOUR_GCP_PROJECT_ID/YOUR_PUBSUB_SUBSCRIPTION_NAME, instead got %s", url)
}