├── sqsdrain.go ├── LICENSE └── README.markdown /sqsdrain.go: -------------------------------------------------------------------------------- 1 | package sqsdrain 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | "github.com/crowdmob/goamz/aws" 8 | "github.com/crowdmob/goamz/sqs" 9 | ) 10 | 11 | const ( 12 | MAX_SQS_MESSAGES = 10 13 | ) 14 | 15 | func Drain(accessKey string, secretKey string, region aws.Region, queueName string, sleep time.Duration, runForever bool) { 16 | auth := aws.Auth{AccessKey: accessKey, SecretKey: secretKey} 17 | client := sqs.New(auth, region) 18 | queue, err := client.GetQueue(queueName) 19 | if err != nil { 20 | log.Fatalf("sqsdrain: could not connect to queue: %s\n", err) 21 | } 22 | log.Printf("sqsdrain: connected to queue: %s\n", queue.Url) 23 | // Drain 24 | for { 25 | response, _ := queue.ReceiveMessage(MAX_SQS_MESSAGES) 26 | if len(response.Messages) == 0 && !runForever { 27 | break 28 | } 29 | for _, m := range response.Messages { 30 | queue.DeleteMessage(&m) 31 | } 32 | term := "message" 33 | if len(response.Messages) <= 1 { 34 | term += "s" 35 | } 36 | log.Printf("sqsdrain: dequeued %d %s\n", len(response.Messages), term) 37 | time.Sleep(sleep) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Kunal Anand 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # sqsdrain 2 | 3 | sqsdrain is a Go package that removes all messages from an SQS queue. It's incredibly useful for test environments where purging a queue is necessary. 4 | 5 | 6 | ## Installation 7 | 8 | Run the following command from your terminal: 9 | 10 | go get github.com/anandkunal/sqsdrain 11 | 12 | This package requires goamz, particularly the repository managed by CrowdMob. You can get that dependency by executing the following terminal commands: 13 | 14 | go get github.com/crowdmob/goamz/aws 15 | go get github.com/crowdmob/goamz/sqs 16 | 17 | 18 | ## Example 19 | 20 | Go ahead and couch the following into an `example.go`. 21 | 22 | package main 23 | 24 | import ( 25 | "time" 26 | 27 | "github.com/anandkunal/sqsdrain" 28 | "github.com/crowdmob/goamz/aws" 29 | ) 30 | 31 | var ( 32 | accessKey = "access key goes here" 33 | secretKey = "secret key goes here" 34 | region = aws.USWest2 35 | queueName = "queue-name-goes-here" 36 | sleep = time.Second * 5 37 | runForever = true 38 | ) 39 | 40 | func main() { 41 | sqsdrain.Drain(accessKey, secretKey, region, queueName, sleep, runForever) 42 | } 43 | 44 | You can run the above with a simple `go run example.go`. You should see output like: 45 | 46 | 2014/03/07 21:37:59 sqsdrain: connected to queue: https://sqs.us-west-2.amazonaws.com/48239042/queue-name-goes-here 47 | 2014/03/07 21:37:59 sqsdrain: dequeued 0 messages 48 | 2014/03/07 21:38:05 sqsdrain: dequeued 4 message 49 | 2014/03/07 21:38:10 sqsdrain: dequeued 0 messages 50 | 51 | 52 | ## Contributions 53 | 54 | sqsdrain was developed by [Kunal Anand][0] and is completely free. Have fun. 55 | 56 | 57 | [0]: https://twitter.com/ka 58 | [2]: http://mit-license.org/ --------------------------------------------------------------------------------