-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
284 lines (231 loc) · 6.9 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"runtime"
"syscall"
"github.com/aws/aws-lambda-go/events" // does not really use lambda, just the S3 event struct
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
jobqueue "github.com/dirkaholic/kyoo"
"github.com/dirkaholic/kyoo/job"
"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
)
const (
EnvironmentLocal = "local"
)
func main() {
log.SetOutput(os.Stdout)
var cfg Config
err := envconfig.Process("s3fileprocessor", &cfg)
if err != nil {
log.Fatal(err.Error())
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
sess := constructAWSSession(&cfg)
sqsSvc := sqs.New(sess)
s3Svc := s3.New(sess)
c := Consumer{
SQSSvc: sqsSvc,
S3Svc: s3Svc,
QueueName: cfg.Queue,
WorkerPool: runtime.NumCPU() * 2,
MaxMessages: 10, // 10 is current maximum value for MaxNumberOfMessages
}
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
c.Stop()
os.Exit(1)
}()
cErr := c.Consume()
if cErr != nil {
log.Fatalln("Error while running sqs consumer: ", cErr)
}
}
// constructAWSSession constructs an AWS session from the config parameters given
func constructAWSSession(cfg *Config) *session.Session {
defaultResolver := endpoints.DefaultResolver()
awsRegion := cfg.Region
if cfg.Environment == EnvironmentLocal {
awsRegion = "us-east-1"
}
resolverFn := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if cfg.Environment != EnvironmentLocal {
return defaultResolver.EndpointFor(service, region, optFns...)
}
var svcURL string
switch service {
case endpoints.SqsServiceID:
svcURL = "http://localhost:4576"
case endpoints.S3ServiceID:
svcURL = "http://localhost:4572"
default:
return defaultResolver.EndpointFor(service, region, optFns...)
}
log.Debugf("Retrieving local %s endpoint ...", service)
return endpoints.ResolvedEndpoint{
URL: svcURL,
SigningRegion: awsRegion,
}, nil
}
credVerboseErrors := true
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(awsRegion),
EndpointResolver: endpoints.ResolverFunc(resolverFn),
CredentialsChainVerboseErrors: &credVerboseErrors,
},
}))
if cfg.DebugAWS {
sess.Config.MergeIn(&aws.Config{
LogLevel: aws.LogLevel(aws.LogDebug),
})
}
return sess
}
// Config represents configuration parameter read from the OS environment
type Config struct {
Debug bool `default:"false"`
DebugAWS bool `default:"false"`
Environment string `default:"prod"`
Region string `default:"eu-central-1"`
Queue string `required:"true"`
}
// Consumer defines SQS queue consumer
type Consumer struct {
SQSSvc sqsiface.SQSAPI
S3Svc s3iface.S3API
MaxMessages int64
WorkerPool int
QueueName string
queueURL *string
queue *jobqueue.JobQueue
}
// Consume starts consuming messages from SQS
func (c *Consumer) Consume() error {
err := c.resolveQueueURL()
if err != nil {
return err
}
// start job queue and pool of workers
c.queue = jobqueue.NewJobQueue(c.WorkerPool)
c.queue.Start()
log.Info("Start consuming messages with maxMessages set to: ", c.MaxMessages)
for !c.queue.Stopped() {
output, err := c.retrieveMessages(c.MaxMessages)
if err != nil {
log.Errorf("Error while retrieving messages: %s", err)
continue
}
log.Debugf("Retrieved %d messages from queue", len(output.Messages))
for _, message := range output.Messages {
m := message // local copy of the mesage, avoid data race
c.queue.Submit(&job.FuncExecutorJob{Func: func() error {
c.processSQSMessage(m)
return nil
}})
}
}
return nil
}
// Stop stops all workers and the queue gracefully
func (c *Consumer) Stop() {
log.Debugln("Stopping consumer ...")
c.queue.Stop()
}
// ConsumeSingleMessage consumes a single message from SQS
func (c *Consumer) ConsumeSingleMessage() error {
err := c.resolveQueueURL()
if err != nil {
return err
}
output, err := c.retrieveMessages(1)
if err != nil {
log.Errorf("Error while retrieving messages: %s", err)
return err
}
if len(output.Messages) > 0 {
c.processSQSMessage(output.Messages[0])
}
return nil
}
func (c *Consumer) resolveQueueURL() error {
qURL, err := c.SQSSvc.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(c.QueueName),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
return fmt.Errorf("Unable to find queue %q", c.QueueName)
}
return fmt.Errorf("Unable to get URL for queue %q, %v", c.QueueName, err)
}
log.Debugf("Resolved queue URL output: %v", qURL)
c.queueURL = qURL.QueueUrl
return nil
}
func (c *Consumer) retrieveMessages(maxMessages int64) (*sqs.ReceiveMessageOutput, error) {
result, err := c.SQSSvc.ReceiveMessage(&sqs.ReceiveMessageInput{
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqs.QueueAttributeNameAll),
},
QueueUrl: c.queueURL,
MaxNumberOfMessages: aws.Int64(maxMessages),
VisibilityTimeout: aws.Int64(60), // 60 seconds
WaitTimeSeconds: aws.Int64(20), // current maximum in order to use longpolling
})
if err != nil {
return &sqs.ReceiveMessageOutput{}, err
}
return result, nil
}
func (c *Consumer) deleteMessage(message *sqs.Message) (*sqs.DeleteMessageOutput, error) {
resultDelete, err := c.SQSSvc.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: c.queueURL,
ReceiptHandle: message.ReceiptHandle,
})
if err != nil {
return &sqs.DeleteMessageOutput{}, err
}
log.Debugf("Message %s deleted", *message.MessageId)
return resultDelete, nil
}
func (c *Consumer) processSQSMessage(m *sqs.Message) {
log.Debugf("Getting body: %s", *m.Body)
var snsMsg sns.PublishInput
snsErr := json.Unmarshal([]byte(*m.Body), &snsMsg)
if snsErr != nil {
log.Errorf("Error while unmarshaling SNS message: %s", snsErr)
}
log.Debugln("SNS message:", *snsMsg.Message)
var s3Event events.S3Event
err := json.Unmarshal([]byte(*snsMsg.Message), &s3Event)
if err != nil {
log.Errorf("Error while unmarshaling S3 event: %s", err)
}
for _, record := range s3Event.Records {
log.Debugf("Processing S3 event from bucket: %s", record.S3.Bucket.Name)
log.Debugf("Processing S3 object: %s", record.S3.Object.Key)
// Do further file processing here
}
// Message consumed
if _, err := c.deleteMessage(m); err != nil {
log.Errorf("Error while deleting message: %s", err)
}
}