forked from mxenabled/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
127 lines (102 loc) · 3.06 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
package workers
import (
"crypto/tls"
"errors"
"log"
"os"
"strings"
"time"
"github.com/go-redis/redis/v8"
"github.com/pesta/go-workers2/storage"
)
// Options contains the set of configuration options for a manager and/or producer
type Options struct {
ProcessID string
Namespace string
PollInterval time.Duration
Database int
Password string
PoolSize int
// Provide one of ServerAddr or (SentinelAddrs + RedisMasterName)
ServerAddr string
SentinelAddrs string
RedisMasterName string
RedisTLSConfig *tls.Config
// Optional display name used when displaying manager stats
ManagerDisplayName string
Heartbeat bool
// Log
Logger *log.Logger
client *redis.Client
store storage.Store
}
func processOptions(options Options) (Options, error) {
options, err := validateGeneralOptions(options)
if err != nil {
return Options{}, err
}
//redis options
if options.PoolSize == 0 {
options.PoolSize = 1
}
redisIdleTimeout := 240 * time.Second
if options.ServerAddr != "" {
options.client = redis.NewClient(&redis.Options{
IdleTimeout: redisIdleTimeout,
Password: options.Password,
DB: options.Database,
PoolSize: options.PoolSize,
Addr: options.ServerAddr,
TLSConfig: options.RedisTLSConfig,
})
} else if options.SentinelAddrs != "" {
if options.RedisMasterName == "" {
return Options{}, errors.New("Sentinel configuration requires a master name")
}
options.client = redis.NewFailoverClient(&redis.FailoverOptions{
IdleTimeout: redisIdleTimeout,
Password: options.Password,
DB: options.Database,
PoolSize: options.PoolSize,
SentinelAddrs: strings.Split(options.SentinelAddrs, ","),
MasterName: options.RedisMasterName,
TLSConfig: options.RedisTLSConfig,
})
} else {
return Options{}, errors.New("Options requires either the Server or Sentinels option")
}
if options.Logger == nil {
options.Logger = log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds)
}
redisStore := storage.NewRedisStore(options.Namespace, options.client, options.Logger)
options.store = redisStore
return options, nil
}
func processOptionsWithRedisClient(options Options, client *redis.Client) (Options, error) {
options, err := validateGeneralOptions(options)
if err != nil {
return Options{}, err
}
if client == nil {
return Options{}, errors.New("Redis client is nil; Redis client is not configured")
}
options.client = client
if options.Logger == nil {
options.Logger = log.New(os.Stdout, "go-workers2: ", log.Ldate|log.Lmicroseconds)
}
redisStore := storage.NewRedisStore(options.Namespace, options.client, options.Logger)
options.store = redisStore
return options, nil
}
func validateGeneralOptions(options Options) (Options, error) {
if options.ProcessID == "" {
return Options{}, errors.New("Options requires a ProcessID, which uniquely identifies this instance")
}
if options.Namespace != "" {
options.Namespace += ":"
}
if options.PollInterval <= 0 {
options.PollInterval = 15 * time.Second
}
return options, nil
}