Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for moving messages to another region #7

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ Download the appropriate binary from the

## Configuration

The `AWS_SECRET_ACCESS_KEY`, `AWS_ACCESS_KEY_ID`, and ,`AWS_REGION`
environment variables must be set.

The `AWS_SECRET_ACCESS_KEY` and `AWS_ACCESS_KEY_ID` environment variables must be set, except for EC2 instances with IAM roles.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the reference to EC2, and keep line length < 80 chars.

## Usage

Expand Down
34 changes: 14 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"flag"
"fmt"
"log"
"os"
"strings"
"sync"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -13,30 +13,20 @@ import (
)

func main() {
src := flag.String("src", "", "source queue")
dest := flag.String("dest", "", "destination queue")
src := flag.String("src", "", "source queue url")
dest := flag.String("dest", "", "destination queue url")
flag.Parse()

if *src == "" || *dest == "" {
flag.Usage()
os.Exit(1)
}

if os.Getenv("AWS_REGION") == "" {
fmt.Printf("AWS_REGION not set")
os.Exit(1)
}
log.Printf("source queue url: %v", *src)
log.Printf("destination queue url: %v", *dest)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the output lines.


region := os.Getenv("AWS_REGION")

log.Printf("source queue : %v", *src)
log.Printf("destination queue : %v", *dest)

config := &aws.Config{
Region: &region,
}

client := sqs.New(session.New(), config)
srcClient := sqs.New(session.New(), aws.NewConfig().WithRegion(getRegionFromQueueURL(*src)))
destClient := sqs.New(session.New(), aws.NewConfig().WithRegion(getRegionFromQueueURL(*dest)))

maxMessages := int64(10)
waitTime := int64(0)
Expand All @@ -51,7 +41,7 @@ func main() {

// loop as long as there are messages on the queue
for {
resp, err := client.ReceiveMessage(rmin)
resp, err := srcClient.ReceiveMessage(rmin)

if err != nil {
panic(err)
Expand All @@ -78,7 +68,7 @@ func main() {
QueueUrl: dest,
}

_, err := client.SendMessage(&smi)
_, err := destClient.SendMessage(&smi)

if err != nil {
log.Printf("ERROR sending message to destination %v", err)
Expand All @@ -91,7 +81,7 @@ func main() {
ReceiptHandle: m.ReceiptHandle,
}

if _, err := client.DeleteMessage(dmi); err != nil {
if _, err := srcClient.DeleteMessage(dmi); err != nil {
log.Printf("ERROR dequeueing message ID %v : %v",
*m.ReceiptHandle,
err)
Expand All @@ -103,3 +93,7 @@ func main() {
wg.Wait()
}
}

func getRegionFromQueueURL(url string) string {
return strings.Split(url, ".")[1]
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line at end of file please :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done