-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (79 loc) · 2.08 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
package main
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)
var queueChannel = make(chan bool)
func main() {
//Cria e busca o client
svc, resultURL := getClientQueue()
for {
go func() { getMesageQueue(svc, resultURL) }()
switch <-queueChannel {
case true:
queueChannel = make(chan bool)
continue
default:
log.Fatal("Erro na fila :(")
}
}
}
func getClientQueue() (*sqs.SQS, *sqs.GetQueueUrlOutput) {
queueName := "teste"
//Cria uma nova sessao com a aws
//pegando as credenciais de variavel de ambiente AWS_ACCESS_KEY_ID e AWS_SECRET_ACCESS_KEY
sess, err := session.NewSession(&aws.Config{
Region: aws.String("sa-east-1")},
)
//Cria client para comunicação
svc := sqs.New(sess)
//Busca url da queue pelo o nome
resultURL, err := svc.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: aws.String(queueName),
})
if err != nil {
log.Fatal("Error to connect queue :(")
}
return svc, resultURL
}
func getMesageQueue(svc *sqs.SQS, ulr *sqs.GetQueueUrlOutput) {
//Recebe mensagem da fila
result, err := svc.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: ulr.QueueUrl,
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqs.QueueAttributeNameAll),
},
MaxNumberOfMessages: aws.Int64(1),
VisibilityTimeout: aws.Int64(36000),
WaitTimeSeconds: aws.Int64(10),
})
if err != nil {
queueChannel <- false
log.Fatal("Error read message")
}
fmt.Printf("%d Messages received.\n", len(result.Messages))
//verifica se veio alguma mensagem
if len(result.Messages) > 0 {
fmt.Println(result.Messages)
//Delete a mensagem
resultDelete, err := svc.DeleteMessage(&sqs.DeleteMessageInput{
QueueUrl: ulr.QueueUrl,
ReceiptHandle: result.Messages[0].ReceiptHandle,
})
if err != nil {
fmt.Println("Delete Error", err)
queueChannel <- false
return
}
fmt.Println("Message Deleted", resultDelete)
queueChannel <- true
} else {
queueChannel <- true
}
}