@@ -15,181 +15,86 @@ import (
15
15
16
16
var _ queue.Worker = (* Worker )(nil )
17
17
18
- // Option for queue system
19
- type Option func (* Worker )
20
-
21
18
// Worker for Redis
22
19
type Worker struct {
23
20
// redis config
24
- rdb redis.Cmdable
25
- pubsub * redis.PubSub
26
- channel <- chan * redis.Message
27
- addr string
28
- db int
29
- connectionString string
30
- password string
31
- channelName string
32
- channelSize int
33
- cluster bool
34
-
21
+ rdb redis.Cmdable
22
+ pubsub * redis.PubSub
23
+ channel <- chan * redis.Message
24
+ stopFlag int32
35
25
stopOnce sync.Once
36
26
stop chan struct {}
37
- runFunc func (context.Context , queue.QueuedMessage ) error
38
- logger queue.Logger
39
- stopFlag int32
40
- metric queue.Metric
27
+
28
+ opts options
41
29
}
42
30
43
31
func (w * Worker ) incBusyWorker () {
44
- w .metric .IncBusyWorker ()
32
+ w .opts . metric .IncBusyWorker ()
45
33
}
46
34
47
35
func (w * Worker ) decBusyWorker () {
48
- w .metric .DecBusyWorker ()
36
+ w .opts . metric .DecBusyWorker ()
49
37
}
50
38
51
39
// BusyWorkers return count of busy workers currently.
52
40
func (w * Worker ) BusyWorkers () uint64 {
53
- return w .metric .BusyWorkers ()
54
- }
55
-
56
- // WithAddr setup the addr of redis
57
- func WithAddr (addr string ) Option {
58
- return func (w * Worker ) {
59
- w .addr = addr
60
- }
61
- }
62
-
63
- // WithPassword redis password
64
- func WithDB (db int ) Option {
65
- return func (w * Worker ) {
66
- w .db = db
67
- }
68
- }
69
-
70
- // WithCluster redis cluster
71
- func WithCluster (enable bool ) Option {
72
- return func (w * Worker ) {
73
- w .cluster = enable
74
- }
75
- }
76
-
77
- // WithChannelSize redis channel size
78
- func WithChannelSize (size int ) Option {
79
- return func (w * Worker ) {
80
- w .channelSize = size
81
- }
82
- }
83
-
84
- // WithPassword redis password
85
- func WithPassword (passwd string ) Option {
86
- return func (w * Worker ) {
87
- w .password = passwd
88
- }
89
- }
90
-
91
- // WithConnectionString redis connection string
92
- func WithConnectionString (connectionString string ) Option {
93
- return func (w * Worker ) {
94
- w .connectionString = connectionString
95
- }
96
- }
97
-
98
- // WithChannel setup the channel of redis
99
- func WithChannel (channel string ) Option {
100
- return func (w * Worker ) {
101
- w .channelName = channel
102
- }
103
- }
104
-
105
- // WithRunFunc setup the run func of queue
106
- func WithRunFunc (fn func (context.Context , queue.QueuedMessage ) error ) Option {
107
- return func (w * Worker ) {
108
- w .runFunc = fn
109
- }
110
- }
111
-
112
- // WithLogger set custom logger
113
- func WithLogger (l queue.Logger ) Option {
114
- return func (w * Worker ) {
115
- w .logger = l
116
- }
117
- }
118
-
119
- // WithMetric set custom Metric
120
- func WithMetric (m queue.Metric ) Option {
121
- return func (w * Worker ) {
122
- w .metric = m
123
- }
41
+ return w .opts .metric .BusyWorkers ()
124
42
}
125
43
126
44
// NewWorker for struc
127
45
func NewWorker (opts ... Option ) * Worker {
128
46
var err error
129
47
w := & Worker {
130
- addr : "127.0.0.1:6379" ,
131
- channelName : "queue" ,
132
- channelSize : 1024 ,
133
- stop : make (chan struct {}),
134
- logger : queue .NewLogger (),
135
- runFunc : func (context.Context , queue.QueuedMessage ) error {
136
- return nil
137
- },
138
- metric : queue .NewMetric (),
139
- }
140
-
141
- // Loop through each option
142
- for _ , opt := range opts {
143
- // Call the option giving the instantiated
144
- opt (w )
48
+ opts : newOptions (opts ... ),
49
+ stop : make (chan struct {}),
145
50
}
146
51
147
- if w .connectionString != "" {
148
- options , err := redis .ParseURL (w .connectionString )
52
+ if w .opts . connectionString != "" {
53
+ options , err := redis .ParseURL (w .opts . connectionString )
149
54
if err != nil {
150
- w .logger .Fatal (err )
55
+ w .opts . logger .Fatal (err )
151
56
}
152
57
w .rdb = redis .NewClient (options )
153
- } else if w .addr != "" {
154
- if w .cluster {
58
+ } else if w .opts . addr != "" {
59
+ if w .opts . cluster {
155
60
w .rdb = redis .NewClusterClient (& redis.ClusterOptions {
156
- Addrs : strings .Split (w .addr , "," ),
157
- Password : w .password ,
61
+ Addrs : strings .Split (w .opts . addr , "," ),
62
+ Password : w .opts . password ,
158
63
})
159
64
} else {
160
65
options := & redis.Options {
161
- Addr : w .addr ,
162
- Password : w .password ,
163
- DB : w .db ,
66
+ Addr : w .opts . addr ,
67
+ Password : w .opts . password ,
68
+ DB : w .opts . db ,
164
69
}
165
70
w .rdb = redis .NewClient (options )
166
71
}
167
72
}
168
73
169
74
_ , err = w .rdb .Ping (context .Background ()).Result ()
170
75
if err != nil {
171
- w .logger .Fatal (err )
76
+ w .opts . logger .Fatal (err )
172
77
}
173
78
174
79
ctx := context .Background ()
175
80
176
81
switch v := w .rdb .(type ) {
177
82
case * redis.Client :
178
- w .pubsub = v .Subscribe (ctx , w .channelName )
83
+ w .pubsub = v .Subscribe (ctx , w .opts . channelName )
179
84
case * redis.ClusterClient :
180
- w .pubsub = v .Subscribe (ctx , w .channelName )
85
+ w .pubsub = v .Subscribe (ctx , w .opts . channelName )
181
86
}
182
87
183
88
var ropts []redis.ChannelOption
184
89
185
- if w .channelSize > 1 {
186
- ropts = append (ropts , redis .WithChannelSize (w .channelSize ))
90
+ if w .opts . channelSize > 1 {
91
+ ropts = append (ropts , redis .WithChannelSize (w .opts . channelSize ))
187
92
}
188
93
189
94
w .channel = w .pubsub .Channel (ropts ... )
190
95
// make sure the connection is successful
191
96
if err := w .pubsub .Ping (ctx ); err != nil {
192
- w .logger .Fatal (err )
97
+ w .opts . logger .Fatal (err )
193
98
}
194
99
195
100
return w
@@ -227,7 +132,7 @@ func (w *Worker) handle(job queue.Job) error {
227
132
}()
228
133
229
134
// run custom process function
230
- done <- w .runFunc (ctx , job )
135
+ done <- w .opts . runFunc (ctx , job )
231
136
}()
232
137
233
138
select {
@@ -292,7 +197,7 @@ func (w *Worker) Queue(job queue.QueuedMessage) error {
292
197
ctx := context .Background ()
293
198
294
199
// Publish a message.
295
- err := w .rdb .Publish (ctx , w .channelName , job .Bytes ()).Err ()
200
+ err := w .rdb .Publish (ctx , w .opts . channelName , job .Bytes ()).Err ()
296
201
if err != nil {
297
202
return err
298
203
}
@@ -319,16 +224,16 @@ func (w *Worker) Run() error {
319
224
}
320
225
321
226
if ! ok {
322
- return fmt .Errorf ("redis pubsub: channel=%s closed" , w .channelName )
227
+ return fmt .Errorf ("redis pubsub: channel=%s closed" , w .opts . channelName )
323
228
}
324
229
325
230
var data queue.Job
326
231
if err := json .Unmarshal ([]byte (m .Payload ), & data ); err != nil {
327
- w .logger .Error ("json unmarshal error: " , err )
232
+ w .opts . logger .Error ("json unmarshal error: " , err )
328
233
continue
329
234
}
330
235
if err := w .handle (data ); err != nil {
331
- w .logger .Error ("handle job error: " , err )
236
+ w .opts . logger .Error ("handle job error: " , err )
332
237
}
333
238
case <- w .stop :
334
239
return nil
0 commit comments