You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

6 years ago
6 years ago
5 years ago
5 years ago
  1. package sub
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. "github.com/aws/aws-sdk-go/aws/credentials"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/aws/aws-sdk-go/service/sqs"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/golang/protobuf/proto"
  13. )
  14. func init() {
  15. NotificationInputs = append(NotificationInputs, &AwsSqsInput{})
  16. }
  17. type AwsSqsInput struct {
  18. svc *sqs.SQS
  19. queueUrl string
  20. }
  21. func (k *AwsSqsInput) GetName() string {
  22. return "aws_sqs"
  23. }
  24. func (k *AwsSqsInput) Initialize(configuration util.Configuration, prefix string) error {
  25. glog.V(0).Infof("replication.notification.aws_sqs.region: %v", configuration.GetString(prefix+"region"))
  26. glog.V(0).Infof("replication.notification.aws_sqs.sqs_queue_name: %v", configuration.GetString(prefix+"sqs_queue_name"))
  27. return k.initialize(
  28. configuration.GetString(prefix+"aws_access_key_id"),
  29. configuration.GetString(prefix+"aws_secret_access_key"),
  30. configuration.GetString(prefix+"region"),
  31. configuration.GetString(prefix+"sqs_queue_name"),
  32. )
  33. }
  34. func (k *AwsSqsInput) initialize(awsAccessKeyId, awsSecretAccessKey, region, queueName string) (err error) {
  35. config := &aws.Config{
  36. Region: aws.String(region),
  37. }
  38. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  39. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  40. }
  41. sess, err := session.NewSession(config)
  42. if err != nil {
  43. return fmt.Errorf("create aws session: %v", err)
  44. }
  45. k.svc = sqs.New(sess)
  46. result, err := k.svc.GetQueueUrl(&sqs.GetQueueUrlInput{
  47. QueueName: aws.String(queueName),
  48. })
  49. if err != nil {
  50. if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
  51. return fmt.Errorf("unable to find queue %s", queueName)
  52. }
  53. return fmt.Errorf("get queue %s url: %v", queueName, err)
  54. }
  55. k.queueUrl = *result.QueueUrl
  56. return nil
  57. }
  58. func (k *AwsSqsInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, err error) {
  59. // receive message
  60. result, err := k.svc.ReceiveMessage(&sqs.ReceiveMessageInput{
  61. AttributeNames: []*string{
  62. aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
  63. },
  64. MessageAttributeNames: []*string{
  65. aws.String(sqs.QueueAttributeNameAll),
  66. },
  67. QueueUrl: &k.queueUrl,
  68. MaxNumberOfMessages: aws.Int64(1),
  69. VisibilityTimeout: aws.Int64(20), // 20 seconds
  70. WaitTimeSeconds: aws.Int64(20),
  71. })
  72. if err != nil {
  73. err = fmt.Errorf("receive message from sqs %s: %v", k.queueUrl, err)
  74. return
  75. }
  76. if len(result.Messages) == 0 {
  77. return
  78. }
  79. // process the message
  80. key = *result.Messages[0].Attributes["key"]
  81. text := *result.Messages[0].Body
  82. message = &filer_pb.EventNotification{}
  83. err = proto.UnmarshalText(text, message)
  84. // delete the message
  85. _, err = k.svc.DeleteMessage(&sqs.DeleteMessageInput{
  86. QueueUrl: &k.queueUrl,
  87. ReceiptHandle: result.Messages[0].ReceiptHandle,
  88. })
  89. if err != nil {
  90. glog.V(1).Infof("delete message from sqs %s: %v", k.queueUrl, err)
  91. }
  92. return
  93. }