Skip to content

Commit c842c2c

Browse files
committed
Add batch limit option, use default region from config
1 parent bd73c60 commit c842c2c

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

README.MD

+21-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
## Features
1010

11-
* Reliable delivery. Messages are only deleted from the source queue if they were enqueued to the destination.
12-
* Messages are sent and received in batches for faster processing.
11+
* Reliable delivery. SQS Mover will only delete messages from the source queue after they were enqueued to the destination.
12+
* Receives and sends messages in batches for faster processing.
1313
* Progress indicator.
1414
* User friendly info and error messages.
1515
* Queue name resolution. For ease of use, you only need to provide a queue name and not the full `arn` address.
16-
* Message Attributes are copied over.
16+
* Message attributes copy.
1717
* Support for FIFO queues. MessageGroupId and MessageDeduplicationId are copied over to the destination messages.
18-
* Optional flag to limit the number of messages to move.
18+
* An optional flag to limit the number of messages to move.
1919

2020
## Getting Started
2121

@@ -38,6 +38,12 @@ aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY>
3838

3939
The [default] heading defines credentials for the default profile, which the SQSMover will use unless you configure it to use another profile.
4040

41+
Optionally you can configure default region in `~/.aws/config`
42+
```
43+
[default]
44+
region=us-west-2
45+
```
46+
4147
#### Environment variables
4248

4349
As an alternative, you can setup AWS credentials in the environment variables.
@@ -117,11 +123,12 @@ usage: sqsmover --source=SOURCE --destination=DESTINATION [<flags>]
117123
Flags:
118124
-h, --help Show context-sensitive help (also try
119125
--help-long and --help-man).
120-
-s, --source=SOURCE Source queue name to move messages from.
121-
-d, --destination=DESTINATION Destination queue name to move messages to.
122-
-r, --region="us-west-2" AWS region for source and destination queues.
126+
-s, --source=SOURCE The source queue name to move messages from.
127+
-d, --destination=DESTINATION The destination queue name to move messages to.
128+
-r, --region="us-west-2" The AWS region for source and destination queues.
123129
-p, --profile="" Use a specific profile from AWS credentials file.
124-
-l, --limit=0 Limits number of messages moved. No limit is set by default.
130+
-l, --limit=0 Limits total number of messages moved. No limit is set by default.
131+
-b, --batch=10 The maximum number of messages to move at a time.
125132
-v, --version Show application version.
126133
```
127134

@@ -148,3 +155,9 @@ Limit number of moved messages to 10
148155
sqsmover -s my_source_queue_name -d my_destination_queuename -l 10
149156
```
150157

158+
By default, `sqsmover` will try to move 10 messages at a time. However, if the total size of messages
159+
in a batch exceeds 256kb (262,144 bytes) you will receive an error: `Batch requests cannot be longer than 262144 bytes. You have sent x bytes.`
160+
To resolve, reduce the batch size by setting `-b` flag.
161+
```
162+
sqsmover -s my_source_queue_name -d my_destination_queuename -b 3
163+
```

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module github.com/mercury2269/sqsmover
22

3+
go 1.14
4+
35
require (
46
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
57
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect

main.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ var (
2525
)
2626

2727
var (
28-
sourceQueue = kingpin.Flag("source", "Source queue name to move messages from.").Short('s').Required().String()
29-
destinationQueue = kingpin.Flag("destination", "Destination queue name to move messages to.").Short('d').Required().String()
30-
region = kingpin.Flag("region", "AWS region for source and destination queues.").Short('r').Default("us-west-2").String()
31-
profile = kingpin.Flag("profile", "Use a specific profile from AWS credentials file.").Short('p').Default("").String()
32-
limit = kingpin.Flag("limit", "Limits number of messages moved. No limit is set by default.").Short('l').Default("0").Int()
28+
sourceQueue = kingpin.Flag("source", "The source queue name to move messages from.").Short('s').Required().String()
29+
destinationQueue = kingpin.Flag("destination", "The destination queue name to move messages to.").Short('d').Required().String()
30+
region = kingpin.Flag("region", "The AWS region for source and destination queues.").Short('r').Default("").String()
31+
profile = kingpin.Flag("profile", "Use a specific profile from AWS credentials file.").Short('p').String()
32+
limit = kingpin.Flag("limit", "Limits total number of messages moved. No limit is set by default.").Short('l').Default("0").Int()
33+
maxBatchSize = kingpin.Flag("batch", "The maximum number of messages to move at a time").Short('b').Default("10").Int64()
3334
)
3435

3536
func main() {
@@ -45,13 +46,16 @@ func main() {
4546

4647
kingpin.Parse()
4748

48-
sess, err := session.NewSessionWithOptions(
49-
session.Options{
50-
Config: aws.Config{Region: aws.String(*region)},
51-
Profile: *profile,
52-
SharedConfigState: session.SharedConfigEnable,
53-
},
54-
)
49+
options := session.Options{
50+
Profile: *profile,
51+
SharedConfigState: session.SharedConfigEnable,
52+
}
53+
54+
if region != nil {
55+
options.Config = aws.Config{Region: aws.String(*region)}
56+
}
57+
58+
sess, err := session.NewSessionWithOptions(options)
5559

5660
if err != nil {
5761
log.Error(color.New(color.FgRed).Sprintf("Unable to create AWS session for region \r\n", *region))
@@ -163,17 +167,16 @@ func convertSuccessfulMessageToBatchRequestEntry(messages []*sqs.Message) []*sqs
163167
}
164168

165169
func moveMessages(sourceQueueUrl string, destinationQueueUrl string, svc *sqs.SQS, totalMessages int) {
166-
params := &sqs.ReceiveMessageInput{
170+
var params = &sqs.ReceiveMessageInput{
167171
QueueUrl: aws.String(sourceQueueUrl),
168172
VisibilityTimeout: aws.Int64(2),
169173
WaitTimeSeconds: aws.Int64(0),
170-
MaxNumberOfMessages: aws.Int64(10),
174+
MaxNumberOfMessages: aws.Int64(*maxBatchSize),
171175
MessageAttributeNames: []*string{aws.String(sqs.QueueAttributeNameAll)},
172176
AttributeNames: []*string{
173177
aws.String(sqs.MessageSystemAttributeNameMessageGroupId),
174178
aws.String(sqs.MessageSystemAttributeNameMessageDeduplicationId)},
175179
}
176-
177180
log.Info(color.New(color.FgCyan).Sprintf("Starting to move messages..."))
178181
fmt.Println()
179182

0 commit comments

Comments
 (0)