2018-10-31 08:11:19 +00:00
|
|
|
package sub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2018-11-01 08:12:21 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2018-10-31 08:11:19 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sqs"
|
2018-11-01 08:12:21 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2018-10-31 08:11:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
NotificationInputs = append(NotificationInputs, &AwsSqsInput{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type AwsSqsInput struct {
|
|
|
|
svc *sqs.SQS
|
|
|
|
queueUrl string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *AwsSqsInput) GetName() string {
|
|
|
|
return "aws_sqs"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (k *AwsSqsInput) Initialize(configuration util.Configuration, prefix string) error {
|
|
|
|
glog.V(0).Infof("replication.notification.aws_sqs.region: %v", configuration.GetString(prefix+"region"))
|
|
|
|
glog.V(0).Infof("replication.notification.aws_sqs.sqs_queue_name: %v", configuration.GetString(prefix+"sqs_queue_name"))
|
2018-10-31 08:11:19 +00:00
|
|
|
return k.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"aws_access_key_id"),
|
|
|
|
configuration.GetString(prefix+"aws_secret_access_key"),
|
|
|
|
configuration.GetString(prefix+"region"),
|
|
|
|
configuration.GetString(prefix+"sqs_queue_name"),
|
2018-10-31 08:11:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-19 03:24:34 +00:00
|
|
|
func (k *AwsSqsInput) initialize(awsAccessKeyId, awsSecretAccessKey, region, queueName string) (err error) {
|
2018-10-31 08:11:19 +00:00
|
|
|
|
|
|
|
config := &aws.Config{
|
|
|
|
Region: aws.String(region),
|
|
|
|
}
|
2019-11-19 03:24:34 +00:00
|
|
|
if awsAccessKeyId != "" && awsSecretAccessKey != "" {
|
|
|
|
config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
|
2018-10-31 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create aws session: %v", err)
|
|
|
|
}
|
|
|
|
k.svc = sqs.New(sess)
|
|
|
|
|
|
|
|
result, err := k.svc.GetQueueUrl(&sqs.GetQueueUrlInput{
|
|
|
|
QueueName: aws.String(queueName),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
|
|
|
|
return fmt.Errorf("unable to find queue %s", queueName)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("get queue %s url: %v", queueName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.queueUrl = *result.QueueUrl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:08:44 +00:00
|
|
|
func (k *AwsSqsInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, onSuccessFn func(), onFailureFn func(), err error) {
|
2018-10-31 08:11:19 +00:00
|
|
|
|
|
|
|
// receive message
|
|
|
|
result, err := k.svc.ReceiveMessage(&sqs.ReceiveMessageInput{
|
|
|
|
AttributeNames: []*string{
|
|
|
|
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
|
|
|
|
},
|
|
|
|
MessageAttributeNames: []*string{
|
|
|
|
aws.String(sqs.QueueAttributeNameAll),
|
|
|
|
},
|
|
|
|
QueueUrl: &k.queueUrl,
|
|
|
|
MaxNumberOfMessages: aws.Int64(1),
|
|
|
|
VisibilityTimeout: aws.Int64(20), // 20 seconds
|
|
|
|
WaitTimeSeconds: aws.Int64(20),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("receive message from sqs %s: %v", k.queueUrl, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(result.Messages) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// process the message
|
2020-04-10 22:27:47 +00:00
|
|
|
// fmt.Printf("messages: %+v\n", result.Messages[0])
|
|
|
|
keyValue := result.Messages[0].MessageAttributes["key"]
|
|
|
|
key = *keyValue.StringValue
|
2018-10-31 08:11:19 +00:00
|
|
|
text := *result.Messages[0].Body
|
|
|
|
message = &filer_pb.EventNotification{}
|
|
|
|
err = proto.UnmarshalText(text, message)
|
|
|
|
|
|
|
|
// delete the message
|
|
|
|
_, err = k.svc.DeleteMessage(&sqs.DeleteMessageInput{
|
|
|
|
QueueUrl: &k.queueUrl,
|
|
|
|
ReceiptHandle: result.Messages[0].ReceiptHandle,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.V(1).Infof("delete message from sqs %s: %v", k.queueUrl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|