/
config.go
205 lines (184 loc) · 4.6 KB
/
config.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
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"log"
"os"
"strings"
"github.com/goamz/goamz/aws"
"github.com/koron/hupwriter"
"github.com/koron/sqs-notify/awsutil"
"github.com/koron/sqs-notify/sqsnotify"
)
const (
modeAtMostOnce = "at-most-once"
)
type config struct {
daemon bool
region string
worker int
nowait bool
ignoreFailure bool
messageCount int
retryMax int
msgcache int
redis string
logfile string
pidfile string
queue string
cmd string
args []string
l *log.Logger
}
func getConfig() (*config, error) {
var (
version bool
daemon bool
region string
worker int
nowait bool
ignoreFailure bool
messageCount int
retryMax int
msgcache int
redis string
logfile string
pidfile string
mode string
)
flag.BoolVar(&version, "version", false, "show version")
flag.BoolVar(&daemon, "daemon", false, "run as a daemon")
flag.StringVar(®ion, "region", "us-east-1", "AWS Region for queue")
flag.IntVar(&worker, "worker", 4, "Num of workers")
flag.BoolVar(&nowait, "nowait", false, "Don't wait end of command")
flag.BoolVar(&ignoreFailure, "ignorefailure", false, "Don't care command failures")
flag.IntVar(&messageCount, "messagecount", 10, "retrieve multiple messages at once")
flag.IntVar(&retryMax, "retrymax", 4, "Num of retry count")
flag.IntVar(&msgcache, "msgcache", 0, "Num of last messages in cache")
flag.StringVar(&redis, "redis", "", "Use redis as messages cache")
flag.StringVar(&logfile, "logfile", "", "Log file path")
flag.StringVar(&pidfile, "pidfile", "", "PID file path (require -logfile)")
flag.StringVar(&mode, "mode", "", "pre-defined set of options for specific usecases")
flag.Usage = usage
flag.Parse()
if version {
showVersion()
}
// Parse arguments.
args := flag.Args()
if len(args) < 2 {
usage()
}
// Check consistencies of options
if len(pidfile) > 0 && len(logfile) == 0 {
return nil, errors.New("`-pidfile' requires `-logfile' option")
}
if messageCount <= 0 {
return nil, errors.New("`-messagecount` should be > 0")
} else if messageCount > 10 {
return nil, errors.New("`-messagecount` should be <= 10")
}
// Apply modes.
switch strings.ToLower(mode) {
case modeAtMostOnce:
if nowait {
return nil, errors.New("`-nowait' conflicts with at-most-once")
}
if msgcache == 0 && redis == "" {
return nil, errors.New("`-msgcache' or `-redis' is required for at-most-once")
}
nowait = false
ignoreFailure = true
}
return &config{
daemon: daemon,
region: region,
worker: worker,
nowait: nowait,
ignoreFailure: ignoreFailure,
messageCount: messageCount,
retryMax: retryMax,
msgcache: msgcache,
redis: redis,
logfile: logfile,
pidfile: pidfile,
queue: args[0],
cmd: args[1],
args: args[2:],
}, nil
}
func (c *config) toApp() (*app, error) {
// Retrieve an AWS auth.
auth, err := awsutil.GetAuth("sqs-notify")
if err != nil {
return nil, err
}
// Determine a region.
region, ok := aws.Regions[c.region]
if !ok {
return nil, errors.New("unknown region:" + c.region)
}
sqsnotify.Logger = c.logger()
notify := sqsnotify.New(auth, region, c.queue)
// TODO: should another option be used? (github#19)
notify.FailMax = c.retryMax
jobs, err := c.newJobs()
if err != nil {
return nil, err
}
return &app{
logger: c.logger(),
auth: auth,
region: region,
worker: c.worker,
nowait: c.nowait,
ignoreFailure: c.ignoreFailure,
messageCount: c.messageCount,
retryMax: c.retryMax,
jobs: jobs,
notify: notify,
cmd: c.cmd,
args: c.args,
}, nil
}
func (c *config) logger() *log.Logger {
if c.l != nil {
return c.l
}
if len(c.logfile) > 0 {
if c.logfile == "-" {
c.l = log.New(os.Stdout, "", log.LstdFlags)
} else {
w := hupwriter.New(c.logfile, c.pidfile)
c.l = log.New(w, "", log.LstdFlags)
}
}
if c.l == nil {
c.l = log.New(ioutil.Discard, "", 0)
}
return c.l
}
func (c *config) newJobs() (jobs, error) {
if c.redis != "" {
rj, err := loadRedisJobs(c.redis)
if err != nil {
return nil, err
}
rj.logger = c.logger()
return rj, nil
}
return newJobs(c.msgcache)
}
func loadRedisJobs(path string) (*redisJobsManager, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var opt redisJobsOptions
if err := json.Unmarshal(b, &opt); err != nil {
return nil, err
}
return newRedisJobs(opt)
}