-
Notifications
You must be signed in to change notification settings - Fork 34
/
queue.go
176 lines (161 loc) · 4.39 KB
/
queue.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
package lifecycled
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
)
const (
longPollingWaitTimeSeconds = 20
queuePolicy = `
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":"*",
"Action":"sqs:SendMessage",
"Resource":"*",
"Condition":{
"ArnEquals":{
"aws:SourceArn":"%s"
}
}
}
]
}
`
)
// SQSClient for testing purposes
//go:generate mockgen -destination=mocks/mock_sqs_client.go -package=mocks github.com/buildkite/lifecycled SQSClient
type SQSClient sqsiface.SQSAPI
// SNSClient for testing purposes
//go:generate mockgen -destination=mocks/mock_sns_client.go -package=mocks github.com/buildkite/lifecycled SNSClient
type SNSClient snsiface.SNSAPI
// Queue manages the SQS queue and SNS subscription.
type Queue struct {
name string
url string
arn string
topicArn string
subscriptionArn string
sqsClient SQSClient
snsClient SNSClient
}
// NewQueue returns a new... Queue.
func NewQueue(queueName, topicArn string, sqsClient SQSClient, snsClient SNSClient) *Queue {
return &Queue{
name: queueName,
topicArn: topicArn,
sqsClient: sqsClient,
snsClient: snsClient,
}
}
// Create the SQS queue.
func (q *Queue) Create() error {
out, err := q.sqsClient.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(q.name),
Attributes: map[string]*string{
"Policy": aws.String(fmt.Sprintf(queuePolicy, q.topicArn)),
"ReceiveMessageWaitTimeSeconds": aws.String(strconv.Itoa(longPollingWaitTimeSeconds)),
},
})
if err != nil {
return err
}
q.url = aws.StringValue(out.QueueUrl)
return nil
}
// GetArn for the SQS queue.
func (q *Queue) getArn() (string, error) {
if q.arn == "" {
out, err := q.sqsClient.GetQueueAttributes(&sqs.GetQueueAttributesInput{
AttributeNames: aws.StringSlice([]string{"QueueArn"}),
QueueUrl: aws.String(q.url),
})
if err != nil {
return "", err
}
arn, ok := out.Attributes["QueueArn"]
if !ok {
return "", errors.New("no attribute QueueArn")
}
q.arn = aws.StringValue(arn)
}
return q.arn, nil
}
// Subscribe the queue to an SNS topic
func (q *Queue) Subscribe() error {
arn, err := q.getArn()
if err != nil {
return err
}
out, err := q.snsClient.Subscribe(&sns.SubscribeInput{
TopicArn: aws.String(q.topicArn),
Protocol: aws.String("sqs"),
Endpoint: aws.String(arn),
})
if err != nil {
return err
}
q.subscriptionArn = aws.StringValue(out.SubscriptionArn)
return nil
}
// GetMessages long polls for messages from the SQS queue.
func (q *Queue) GetMessages(ctx context.Context) ([]*sqs.Message, error) {
out, err := q.sqsClient.ReceiveMessageWithContext(ctx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(q.url),
MaxNumberOfMessages: aws.Int64(1),
WaitTimeSeconds: aws.Int64(longPollingWaitTimeSeconds),
VisibilityTimeout: aws.Int64(0),
})
if err != nil {
// Ignore error if the context was cancelled (i.e. we are shutting down)
if e, ok := err.(awserr.Error); ok && e.Code() == request.CanceledErrorCode {
return nil, nil
}
return nil, err
}
return out.Messages, nil
}
// DeleteMessage from the queue.
func (q *Queue) DeleteMessage(ctx context.Context, receiptHandle string) error {
_, err := q.sqsClient.DeleteMessageWithContext(ctx, &sqs.DeleteMessageInput{
QueueUrl: aws.String(q.url),
ReceiptHandle: aws.String(receiptHandle),
})
if err != nil {
if e, ok := err.(awserr.Error); ok && e.Code() == request.CanceledErrorCode {
return nil
}
return err
}
return nil
}
// Unsubscribe the queue from the SNS topic.
func (q *Queue) Unsubscribe() error {
_, err := q.snsClient.Unsubscribe(&sns.UnsubscribeInput{
SubscriptionArn: aws.String(q.subscriptionArn),
})
return err
}
// Delete the SQS queue.
func (q *Queue) Delete() error {
_, err := q.sqsClient.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: aws.String(q.url),
})
if err != nil {
// Ignore error if queue does not exist (which is what we want)
if e, ok := err.(awserr.Error); !ok || e.Code() != sqs.ErrCodeQueueDoesNotExist {
return err
}
}
return nil
}