-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
401 lines (318 loc) · 9.76 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"runtime/debug"
"strconv"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/google/uuid"
"github.com/itchyny/gojq"
"go.uber.org/ratelimit"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
var (
dry = flag.Bool("dry", false, "dry run (only print messages that would be processed)")
delete = flag.Bool("delete", false, "delete messages from source queue")
move = flag.Bool("move", false, "move messages from source queue to target queue")
targetQueue = flag.String("target", "", "the queue URL, e.g. https://sqs.us-east-1.amazonaws.com/123456789012/my-queue")
sourceQueue = flag.String("source", "", "the queue URL, e.g. https://sqs.us-east-1.amazonaws.com/123456789012/my-queue")
beforeFilter = flag.String("before", "", "get only messages sent before a certain date in format 2006-01-02T15:04:05Z07:00")
afterFilter = flag.String("after", "", "get only messages sent after a certain date in format 2006-01-02T15:04:05Z07:00")
numMessages = flag.Int("count", 0, "maximum number of messages to process")
numWorkers = flag.Int("workers", 8, "number of workers")
bodyFilter = flag.String("body-filter", "", "filter messages by JSON body field content using JQ expression")
attributeFilter = flag.String("attribute-filter", "", "filter messages by a certain attribute using JQ expression")
messageAttributeFilter = flag.String("message-attribute-filter", "", "filter messages by a certain message attribute using JQ expression")
rateLimit = flag.Int("rate-limit", 10, "Max number of messages processed per second")
pollingDuration = flag.Duration("polling-duration", 30*time.Second, "Polling duration")
versionFlag = flag.Bool("version", false, "Print version and exit")
)
var (
after time.Time
before time.Time
attributesQuery *gojq.Code
bodyQuery *gojq.Code
messageAttributesQuery *gojq.Code
)
const maxSQSBatchSize = 10
func main() {
flag.Parse()
if *versionFlag {
printVersion()
return
}
if err := validateFlags(); err != nil {
printVersion()
fmt.Println(err)
fmt.Println()
flag.Usage()
return
}
ctx, cancel := context.WithTimeout(context.Background(), *pollingDuration)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
log.Println("Configuration error", err)
return
}
sqsClient := sqs.NewFromConfig(cfg)
batches := make(chan []types.Message, *numMessages)
wg := &sync.WaitGroup{}
for i := 0; i < *numWorkers; i++ {
wg.Add(1)
go worker(ctx, sqsClient, batches, wg)
}
startPolling(ctx, sqsClient, batches)
close(batches)
wg.Wait()
log.Println("Finished processing")
}
func printVersion() {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
fmt.Printf("%s@%s commit %s (%s) %s\n", buildInfo.Path, version, commit, date, buildInfo.GoVersion)
}
}
func validateFlags() error {
if *move == *delete {
return fmt.Errorf("Must specify either -delete or -move")
}
if *sourceQueue == "" {
return fmt.Errorf("Missing source queue")
}
if *move && *targetQueue == "" {
return fmt.Errorf("Missing target queue")
}
var err error
if *attributeFilter != "" {
attributesQuery, err = parseAndCompileJQ(*attributeFilter)
if err != nil {
return fmt.Errorf("Invalid attribute filter %w", err)
}
}
if *messageAttributeFilter != "" {
messageAttributesQuery, err = parseAndCompileJQ(*messageAttributeFilter)
if err != nil {
return fmt.Errorf("Invalid message attribute filter %w", err)
}
}
if *bodyFilter != "" {
bodyQuery, err = parseAndCompileJQ(*bodyFilter)
if err != nil {
return fmt.Errorf("Invalid body filter %w", err)
}
}
if *beforeFilter != "" {
before, err = time.Parse(time.RFC3339, *beforeFilter)
if err != nil {
return fmt.Errorf("Invalid before date %w", err)
}
}
if *afterFilter != "" {
after, err = time.Parse(time.RFC3339, *afterFilter)
if err != nil {
return fmt.Errorf("Invalid after date %w", err)
}
}
return nil
}
func parseAndCompileJQ(jq string) (*gojq.Code, error) {
query, err := gojq.Parse(jq)
if err != nil {
return nil, err
}
code, err := gojq.Compile(query)
if err != nil {
return nil, err
}
return code, nil
}
func worker(ctx context.Context, sqsClient *sqs.Client, batches <-chan []types.Message, wg *sync.WaitGroup) {
defer wg.Done()
for batch := range batches {
switch {
case *dry:
printSQSMessages(batch)
case *move:
redriveSQSMessages(ctx, batch, sqsClient)
case *delete:
deleteSQSMessages(ctx, batch, sqsClient)
}
}
}
func startPolling(ctx context.Context, sqsClient *sqs.Client, batches chan []types.Message) {
var processedCount int
var receivedCount int
rateLimit := ratelimit.New(*rateLimit)
defer func() {
log.Printf("%d/%d messages were processed in total\n", processedCount, receivedCount)
}()
for {
if processedCount >= *numMessages {
return
}
select {
case <-ctx.Done():
return
default:
polledBatch, err := pollForMessageBatch(ctx, sqsClient)
if err != nil {
log.Printf("Failed to receive message: %v", err)
return
}
receivedCount += len(polledBatch)
if polledBatch == nil {
continue
}
filteredBatch := filterBatch(polledBatch)
if len(filteredBatch) == 0 {
log.Printf("0/%d messages to process, skipping\n", len(polledBatch))
continue
}
processedCount += len(filteredBatch)
rateLimit.Take()
batches <- filteredBatch
}
}
}
func filterBatch(polledBatch []types.Message) []types.Message {
filteredBatch := make([]types.Message, 0, maxSQSBatchSize)
for _, msg := range polledBatch {
if shouldProcessMessage(msg) {
filteredBatch = append(filteredBatch, msg)
}
}
return filteredBatch
}
func printSQSMessages(batch []types.Message) {
for _, msg := range batch {
log.Printf("SentTimestamp:%v\n Attributes: %v\n MessageAttributes: %v\n Body: %v\n", getTimestamp(msg), toJSONMap(msg.Attributes), toJSONMap(msg.MessageAttributes), *msg.Body)
}
}
func deleteSQSMessages(ctx context.Context, batch []types.Message, sqsClient *sqs.Client) {
sqsBatch := make([]types.DeleteMessageBatchRequestEntry, 0, maxSQSBatchSize)
for _, msg := range batch {
sqsBatch = append(sqsBatch, types.DeleteMessageBatchRequestEntry{Id: msg.MessageId, ReceiptHandle: msg.ReceiptHandle})
}
result, err := sqsClient.DeleteMessageBatch(ctx, &sqs.DeleteMessageBatchInput{
QueueUrl: aws.String(*sourceQueue),
Entries: sqsBatch,
})
if err != nil {
log.Printf("Error deleting message: %v", err)
return
}
log.Printf("Deleted %d Failed %d", len(result.Successful), len(result.Failed))
}
func redriveSQSMessages(ctx context.Context, batch []types.Message, sqsClient *sqs.Client) {
sqsBatch := make([]types.SendMessageBatchRequestEntry, 0, maxSQSBatchSize)
for _, msg := range batch {
sqsBatch = append(sqsBatch, types.SendMessageBatchRequestEntry{
Id: aws.String(uuid.New().String()),
MessageAttributes: msg.MessageAttributes,
MessageBody: msg.Body,
})
}
result, err := sqsClient.SendMessageBatch(ctx, &sqs.SendMessageBatchInput{
QueueUrl: aws.String(*targetQueue),
Entries: sqsBatch,
})
if err != nil {
log.Printf("Error redriving message: %v", err)
}
log.Printf("Sent %d Failed %d", len(result.Successful), len(result.Failed))
}
func getTimestamp(msg types.Message) time.Time {
if msgTimestamp, ok := msg.Attributes["SentTimestamp"]; ok {
parsedTimestamp, err := strconv.ParseInt(msgTimestamp, 10, 64)
if err != nil {
log.Printf("Error parsing message timestamp: %v", err)
}
return time.Unix(parsedTimestamp/1000, 0)
}
return time.Time{}
}
func shouldProcessMessage(msg types.Message) bool {
timestamp := getTimestamp(msg)
if !before.IsZero() && timestamp.Unix() >= before.Unix() {
return false
}
if !after.IsZero() && timestamp.Unix() <= after.Unix() {
return false
}
if attributesQuery != nil && !matchesJqQuery(toJSONMap(msg.Attributes), attributesQuery) {
return false
}
if messageAttributesQuery != nil && !matchesJqQuery(toJSONMap(msg.MessageAttributes), messageAttributesQuery) {
return false
}
if bodyQuery != nil && !matchesJqQuery(toJSONMap(msg.Body), bodyQuery) {
return false
}
return true
}
func matchesJqQuery(input map[string]any, query *gojq.Code) bool {
if query == nil {
return false
}
iter := query.Run(input)
if result, ok := iter.Next(); ok {
if _, ok := result.(error); ok {
return false
}
if result, ok := result.(bool); ok {
return result
}
}
return false
}
func toJSONMap(input any) (jsonMap map[string]any) {
if input == nil {
return nil
}
var bytes []byte
var err error
switch input := input.(type) {
case *string:
bytes = []byte(*input)
case string:
bytes = []byte(input)
case json.RawMessage:
bytes = []byte(input)
default:
bytes, err = json.Marshal(input)
if err != nil {
log.Printf("Could not marshal: %v %v", input, err)
}
}
if err = json.Unmarshal(bytes, &jsonMap); err != nil {
log.Printf("Could not convert to JSON map: %v %v", string(bytes), err)
}
return
}
func pollForMessageBatch(ctx context.Context, sqsClient *sqs.Client) ([]types.Message, error) {
result, err := sqsClient.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(*sourceQueue),
MaxNumberOfMessages: maxSQSBatchSize,
AttributeNames: []types.QueueAttributeName{types.QueueAttributeNameAll},
MessageAttributeNames: []string{string(types.QueueAttributeNameAll)},
})
if err != nil {
return nil, err
}
if len(result.Messages) == 0 {
return nil, nil
}
return result.Messages, nil
}