-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
138 lines (113 loc) · 2.98 KB
/
main.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
// Package main is entry point for scout
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
)
var (
app *cli.App
signals chan os.Signal
)
func init() {
app = cli.NewApp()
app.Name = "scout"
app.Usage = `SQS Listener
Poll SQS queues specified in a config and enqueue Sidekiq jobs with the queue items.
It gracefully stops when sent SIGTERM.`
app.Version = "v1.6.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load config from `FILE`, required",
},
cli.Int64Flag{
Name: "freq, f",
Value: 100,
Usage: "Poll SQS every `N` milliseconds",
},
cli.StringFlag{
Name: "log-level, l",
Usage: "Sets log level. Accepts one of: debug, info, warn, error",
},
cli.BoolFlag{
Name: "json, j",
Usage: "Log in json format",
},
}
app.Action = runApp
signals = make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
}
func main() {
app.Run(os.Args)
}
func runApp(ctx *cli.Context) error {
configFile := ctx.String("config")
frequency := ctx.Int64("freq")
if ctx.Bool("json") {
log.SetFormatter(&log.JSONFormatter{})
}
logLevel := ctx.String("log-level")
if logLevel == "" {
logLevel = "info"
}
level, err := log.ParseLevel(logLevel)
if err != nil {
return cli.NewExitError("Could not parse log level", 1)
}
log.SetLevel(level)
if configFile == "" {
return cli.NewExitError("Missing required flag --config. Run `scout --help` for more information", 1)
}
log.Infof("Reading config from %s", configFile)
log.Infof("Polling every %d milliseconds", frequency)
config, err := ReadConfig(configFile)
if err != nil {
return cli.NewExitError("Failed to parse config file", 1)
}
maxNumberOfMessages, _ := strconv.ParseInt(os.Getenv("SCOUT_SQS_MAX_NUMBER_OF_MESSAGES"), 10, 64)
if maxNumberOfMessages != 0 {
config.SQS.maxNumberOfMessages = maxNumberOfMessages
} else {
config.SQS.maxNumberOfMessages = 10
}
waitTimeSeconds, _ := strconv.ParseInt(os.Getenv("SCOUT_SQS_WAIT_TIME_SECONDS"), 10, 64)
if waitTimeSeconds != 0 {
config.SQS.waitTimeSeconds = waitTimeSeconds
}
visibilityTimeout, _ := strconv.ParseInt(os.Getenv("SCOUT_SQS_VISIBILITY_TIMEOUT"), 10, 64)
if visibilityTimeout != 0 {
config.SQS.visibilityTimeout = visibilityTimeout
}
queue, err := NewQueue(config)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Initialization error: %s", err.Error()), 1)
}
log.Info("Now listening on queue: ", config.Queue.Name)
for topic, worker := range config.Queue.Topics {
log.Infof("%s -> %s", topic, worker)
}
Listen(queue, time.Tick(time.Duration(frequency)*time.Millisecond))
return nil
}
// Listen does the work. It only returns if we get a signal
func Listen(queue Queue, freq <-chan time.Time) {
for {
select {
case <-signals:
log.Info("Got TERM")
queue.Semaphore().Wait()
return
case tick := <-freq:
log.Debug("Polling at: ", tick)
queue.Semaphore().Add(1)
go queue.Poll()
}
}
}