forked from koron/sqs-notify
/
main.go
225 lines (198 loc) · 4.44 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"crypto/md5"
"errors"
"fmt"
"io"
"log"
"math/rand"
"os"
"os/exec"
"strconv"
"time"
"github.com/goamz/goamz/aws"
"github.com/koron/sqs-notify/sqsnotify"
)
const progname = "sqs-notify"
type app struct {
logger *log.Logger
auth aws.Auth
region aws.Region
worker int
nowait bool
ignoreFailure bool
retryMax int
jobs jobs
notify *sqsnotify.SQSNotify
cmd string
args []string
}
func usage() {
fmt.Printf(`Usage: %s [OPTIONS] {queue name} {command and args...}
OPTIONS:
-region {region} : name of region (default: us-east-1)
-worker {num} : num of workers (default: 4)
-nowait : don't wait end of command to delete message
-ignorefailure: don't care command results, treat it as success always
-retrymax {num} : num of retry count (default: 4)
-msgcache {num} : num of last received message in cache (default: 0)
-redis {path} : use redis as message cache (default: disabled)
-logfile {path} : log file path ("-" for stdout)
-pidfile {path} : pid file path (available with -logfile)
-mode {mode} : pre-defined set of options for specific usecases
Source: https://github.com/koron/sqs-notify
`, progname)
os.Exit(1)
}
func retryDuration(c int) time.Duration {
limit := (1 << uint(c)) - 1
if limit > 50 {
limit = 50
}
v := rand.Intn(limit)
return time.Duration(v*200) * time.Millisecond
}
func (a *app) log(v ...interface{}) {
if a.logger == nil {
return
}
a.logger.Print(v)
}
func (a *app) logOk(m string, r workerResult) {
if a.logger == nil {
return
}
// Log as OK.
a.logger.Printf("\tEXECUTED\tqueue:%s\tbody:%#v\tcmd:%s\tstatus:%d",
a.notify.Name(), m, a.cmd, r.Code)
}
func (a *app) logSkip(m string) {
if a.logger == nil {
return
}
// Log as SKIP.
a.logger.Printf("\tSKIPPED\tqueue:%s\tbody:%#v\t", a.notify.Name(), m)
}
func (a *app) logNg(m string, err error) {
if a.logger == nil {
return
}
a.logger.Printf("\tNOT_EXECUTED\tqueue:%s\tbody:%#v\terror:%s",
a.notify.Name(), m, err)
}
func (a *app) deleteSQSMessage(m *sqsnotify.SQSMessage) {
// FIXME: log it when failed to delete.
m.Delete()
}
func (a *app) digest(s string) string {
h := md5.New()
return fmt.Sprintf("%x", h.Sum([]byte(s)))
}
func (a *app) run() (err error) {
// Open a queue.
err = a.notify.Open()
if err != nil {
return
}
// Listen queue.
c, err := a.notify.Listen()
if err != nil {
return
}
w := newWorkers(a.worker)
// Receive *sqsnotify.SQSMessage via channel.
retryCount := 0
for m := range c {
if m.Error != nil {
if retryCount >= a.retryMax {
a.log("abort:", m.Error)
log.Println("sqs-notify (abort):", m.Error)
return errors.New("Over retry: " + strconv.Itoa(retryCount))
}
a.log("retry:", m.Error)
log.Println("sqs-notify (retry):", m.Error)
retryCount++
// sleep before retry.
time.Sleep(retryDuration(retryCount))
continue
} else {
retryCount = 0
}
body := *m.Body()
jid := a.digest(body)
switch a.jobs.StartTry(jid) {
case jobRunning:
a.logSkip(body)
continue
case jobCompleted:
a.logSkip(body)
a.deleteSQSMessage(m)
continue
}
// Create and setup a exec.Cmd.
cmd := exec.Command(a.cmd, a.args...)
stdin, err := cmd.StdinPipe()
if err != nil {
a.logNg(body, err)
a.jobs.Fail(jid)
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
a.logNg(body, err)
a.jobs.Fail(jid)
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
a.logNg(body, err)
a.jobs.Fail(jid)
return err
}
if a.nowait {
a.deleteSQSMessage(m)
w.Run(workerJob{cmd, func(r workerResult) {
a.logOk(body, r)
if r.Success() || a.ignoreFailure {
a.jobs.Complete(jid)
} else {
a.jobs.Fail(jid)
}
}})
} else {
w.Run(workerJob{cmd, func(r workerResult) {
a.logOk(body, r)
if r.Success() || a.ignoreFailure {
a.jobs.Complete(jid)
a.deleteSQSMessage(m)
} else {
a.jobs.Fail(jid)
}
}})
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
go func() {
stdin.Write([]byte(body))
stdin.Close()
}()
}
return
}
func main() {
c, err := getConfig()
if err != nil {
log.Fatalln("sqs-notify:", err)
}
a, err := c.toApp()
if err != nil {
log.Fatalln("sqs-notify:", err)
}
if a.jobs != nil {
defer a.jobs.Close()
}
err = a.run()
if err != nil {
log.Fatalln("sqs-notify:", err)
}
}