forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
161 lines (149 loc) · 4.08 KB
/
flags.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
// Running goworker
//
// After building your workers, you will have an
// executable that you can run which will
// automatically poll a Redis server and call
// your workers as jobs arrive.
//
// Flags
//
// There are several flags which control the
// operation of the goworker client.
//
// -queues="comma,delimited,queues"
// — This is the only required flag. The
// recommended practice is to separate your
// Resque workers from your goworkers with
// different queues. Otherwise, Resque worker
// classes that have no goworker analog will
// cause the goworker process to fail the jobs.
// Because of this, there is no default queue,
// nor is there a way to select all queues (à la
// Resque's * queue). Queues are processed in
// the order they are specififed.
// If you have multiple queues you can assign
// them weights. A queue with a weight of 2 will
// be checked twice as often as a queue with a
// weight of 1: -queues='high=2,low=1'.
//
// -interval=5.0
// — Specifies the wait period between polling if
// no job was in the queue the last time one was
// requested.
//
// -concurrency=25
// — Specifies the number of concurrently
// executing workers. This number can be as low
// as 1 or rather comfortably as high as 100,000,
// and should be tuned to your workflow and the
// availability of outside resources.
//
// -connections=2
// — Specifies the maximum number of Redis
// connections that goworker will consume between
// the poller and all workers. There is not much
// performance gain over two and a slight penalty
// when using only one. This is configurable in
// case you need to keep connection counts low
// for cloud Redis providers who limit plans on
// maxclients.
//
// -uri=redis://localhost:6379/
// — Specifies the URI of the Redis database from
// which goworker polls for jobs. Accepts URIs of
// the format redis://user:pass@host:port/db or
// unix:///path/to/redis.sock. The flag may also
// be set by the environment variable
// $($REDIS_PROVIDER) or $REDIS_URL. E.g. set
// $REDIS_PROVIDER to REDISTOGO_URL on Heroku to
// let the Redis To Go add-on configure the Redis
// database.
//
// -namespace=resque:
// — Specifies the namespace from which goworker
// retrieves jobs and stores stats on workers.
//
// -exit-on-complete=false
// — Exits goworker when there are no jobs left
// in the queue. This is helpful in conjunction
// with the time command to benchmark different
// configurations.
//
// You can also configure your own flags for use
// within your workers. Be sure to set them
// before calling goworker.Main(). It is okay to
// call flags.Parse() before calling
// goworker.Main() if you need to do additional
// processing on your flags.
package goworker
import (
"flag"
"strconv"
"strings"
)
var (
queuesString string
queues queuesFlag
intervalFloat float64
interval intervalFlag
concurrency int
connections int
uri string
namespace string
exitOnComplete bool
isStrict bool
)
func Config(values map[string]string) {
var err error
if x, ok := values["queues"]; ok {
queuesString = x
}
if x, ok := values["interval"]; ok {
if intervalFloat, err = strconv.ParseFloat(x, 64); err != nil {
panic(err)
}
}
if x, ok := values["concurrency"]; ok {
if concurrency, err = strconv.Atoi(x); err != nil {
panic(err)
}
}
if x, ok := values["connections"]; ok {
if connections, err = strconv.Atoi(x); err != nil {
panic(err)
}
}
if x, ok := values["uri"]; ok {
uri = x
}
if x, ok := values["namespace"]; ok {
namespace = x
}
if x, ok := values["exitOnComplete"]; ok {
if exitOnComplete, err = strconv.ParseBool(x); err != nil {
panic(err)
}
}
}
func init() {
queuesString = ""
intervalFloat = 5.0
concurrency = 25
connections = 2
uri = "redis://localhost:6379/"
namespace = "resque:"
exitOnComplete = false
}
func flags() error {
if !flag.Parsed() {
flag.Parse()
}
if err := queues.Set(queuesString); err != nil {
return err
}
if err := interval.SetFloat(intervalFloat); err != nil {
return err
}
isStrict = strings.IndexRune(queuesString, '=') == -1
return nil
}