-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
155 lines (133 loc) · 3.25 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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
var (
cfg Config
queue SQS
)
// try to gracefully stop when signalled to stop
func watchSignal() {
sigc := make(chan os.Signal, 1)
signal.Notify(
sigc,
os.Interrupt,
os.Kill,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGKILL,
syscall.SIGQUIT,
)
go func() {
s := <-sigc
fmt.Println(s)
// TODO log this
time.Sleep(time.Second)
os.Exit(0)
}()
}
// Post message to POST_HOST + POST_ENDPOINT and delete from queue if successfull
// if unsuccessful do nothing and the message will be pulled from the queue again
// until the default amount set by the queue
func processMessage(message *sqs.Message, wg *sync.WaitGroup) {
defer wg.Done()
// Post to service
client := http.Client{
Timeout: cfg.ConnectionTimeout,
}
req, err := http.NewRequest("POST", cfg.PostHost+cfg.PostEndpoint, strings.NewReader(*message.Body))
if err != nil {
fmt.Printf("Error: %s processing %s", err, *message.ReceiptHandle)
return
}
req.Close = true
req.Header.Set("Content-Type", cfg.ContentType)
res, err := client.Do(req)
if err != nil {
fmt.Printf("Error: %s, Post to service failed on MessageId: %s", err, *message.ReceiptHandle)
return
}
if res.StatusCode != 200 {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
body = []byte("")
}
fmt.Printf("Error Failed Message. StatusCode: %d, MessageId: %s, ResponseBody: %s", res.StatusCode, *message.ReceiptHandle, body)
return
}
// delete message
if err = queue.Complete(cfg.SQSURL, *message.ReceiptHandle); err != nil {
fmt.Printf("Error: %s, Failed to delete MessageId: %s from SQS", err, *message.ReceiptHandle)
return
}
}
func backoff(dur time.Duration) time.Duration {
newWait := dur.Nanoseconds() * 2
if newWait > cfg.MaxSleep.Nanoseconds() || newWait < 1 {
return cfg.MaxSleep
}
return time.Duration(newWait)
}
// Run the main loop in which messages are processed
func work() {
dur := time.Millisecond * 10
wg := &sync.WaitGroup{}
creds, err := queue.sqsService.Config.Credentials.Get()
if err == nil {
fmt.Printf("Using aws provider: %s\n", creds.ProviderName)
}
for {
time.Sleep(dur)
dur = backoff(dur)
// get next message batch
messages, err := queue.NextMessages(cfg.SQSURL)
if err != nil {
// TODO log or something
fmt.Printf("Error: %s, Failed to get next messaged in queue\n", err)
if strings.Contains(err.Error(), "NoCredentialProviders") {
os.Exit(1)
}
continue
}
// No messages
if len(messages) == 0 {
fmt.Println("No Messages. Backing off ", dur)
continue
}
fmt.Printf("Processing %d messages...\n", len(messages))
// Process messages
for _, message := range messages {
wg.Add(1)
go processMessage(message, wg)
}
wg.Wait()
// TODO log success or something
dur = time.Millisecond * 10
}
}
func main() {
initConfig()
sesh := session.New()
awsConfig := aws.Config{}
awsConfig.WithRegion(cfg.Region)
awsConfig.MaxRetries = aws.Int(10)
queue = SQS{
cfg: cfg,
}
queue.sqsService = sqs.New(sesh, &awsConfig)
watchSignal()
work()
}