forked from amazon-archives/k8s-cloudwatch-adapter
/
main.go
78 lines (67 loc) · 1.89 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/awslabs/k8s-cloudwatch-adapter/pkg/aws"
)
func main() {
// Using the SDK's default configuration, loading additional config
// and credentials values from the environment variables, shared
// credentials, and shared configuration files
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
panic("unable to load SDK config, " + err.Error())
}
if cfg.Region == "" {
cfg.Region = aws.GetLocalRegion()
}
fmt.Println("using AWS Region:", cfg.Region)
svc := sqs.New(cfg)
// Initialize and create a SQS Queue named helloworld if it doesn't exist
queueName := os.Getenv("QUEUE")
if queueName == "" {
queueName = "helloworld"
}
fmt.Println("listening to queue:", queueName)
q, err := svc.GetQueueUrlRequest(&sqs.GetQueueUrlInput{
QueueName: &queueName,
}).Send(context.Background())
if err != nil {
// handle queue creation error
fmt.Println("cannot get queue:", err)
}
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill)
go func() {
<-signalChan
os.Exit(1)
}()
timeout := int64(20)
for {
msg, err := svc.ReceiveMessageRequest(&sqs.ReceiveMessageInput{
QueueUrl: q.QueueUrl,
WaitTimeSeconds: &timeout,
}).Send(context.Background())
if err != nil {
fmt.Println("error receiving message from queue:", err)
} else {
fmt.Println("message:", msg)
}
if len(msg.Messages) > 0 {
_, err = svc.DeleteMessageRequest(&sqs.DeleteMessageInput{
QueueUrl: q.QueueUrl,
ReceiptHandle: msg.Messages[0].ReceiptHandle,
}).Send(context.Background())
if err != nil {
fmt.Println("error deleting message from queue:", err)
}
}
// Implement some delay here to simulate processing time
time.Sleep(time.Duration(1000) * time.Millisecond)
}
}