forked from koron/sqs-notify
/
config.go
165 lines (147 loc) · 3.57 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
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 {
region string
worker int
nowait bool
ignoreFailure bool
retryMax int
msgcache int
redis string
logfile string
pidfile string
queue string
cmd string
args []string
}
func getConfig() (*config, error) {
var (
region string
worker int
nowait bool
ignoreFailure bool
retryMax int
msgcache int
redis string
logfile string
pidfile string
mode string
)
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(&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.Parse()
// 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")
}
// 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{
region: region,
worker: worker,
nowait: nowait,
ignoreFailure: ignoreFailure,
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)
}
// logfile and pidfile.
var l *log.Logger
if len(c.logfile) > 0 {
if c.logfile == "-" {
l = log.New(os.Stdout, "", log.LstdFlags)
} else {
w := hupwriter.New(c.logfile, c.pidfile)
l = log.New(w, "", log.LstdFlags)
}
}
notify := sqsnotify.New(auth, region, c.queue)
jobs, err := c.newJobs()
if err != nil {
return nil, err
}
return &app{
logger: l,
auth: auth,
region: region,
worker: c.worker,
nowait: c.nowait,
retryMax: c.retryMax,
jobs: jobs,
notify: notify,
cmd: c.cmd,
args: c.args,
}, nil
}
func (c *config) newJobs() (jobs, error) {
if c.redis != "" {
return loadRedisJobs(c.redis)
}
return newJobs(c.msgcache)
}
func loadRedisJobs(path string) (jobs, 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)
}