notification_aws_sqs.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. S3DisableContentMD5Validation: aws.Bool(true),
  38. }
  39. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  40. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  41. }
  42. sess, err := session.NewSession(config)
  43. if err != nil {
  44. return fmt.Errorf("create aws session: %v", err)
  45. }
  46. k.svc = sqs.New(sess)
  47. result, err := k.svc.GetQueueUrl(&sqs.GetQueueUrlInput{
  48. QueueName: aws.String(queueName),
  49. })
  50. if err != nil {
  51. if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
  52. return fmt.Errorf("unable to find queue %s", queueName)
  53. }
  54. return fmt.Errorf("get queue %s url: %v", queueName, err)
  55. }
  56. k.queueUrl = *result.QueueUrl
  57. return nil
  58. }
  59. func (k *AwsSqsInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, onSuccessFn func(), onFailureFn func(), err error) {
  60. // receive message
  61. result, err := k.svc.ReceiveMessage(&sqs.ReceiveMessageInput{
  62. AttributeNames: []*string{
  63. aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
  64. },
  65. MessageAttributeNames: []*string{
  66. aws.String(sqs.QueueAttributeNameAll),
  67. },
  68. QueueUrl: &k.queueUrl,
  69. MaxNumberOfMessages: aws.Int64(1),
  70. VisibilityTimeout: aws.Int64(20), // 20 seconds
  71. WaitTimeSeconds: aws.Int64(20),
  72. })
  73. if err != nil {
  74. err = fmt.Errorf("receive message from sqs %s: %v", k.queueUrl, err)
  75. return
  76. }
  77. if len(result.Messages) == 0 {
  78. return
  79. }
  80. // process the message
  81. // fmt.Printf("messages: %+v\n", result.Messages[0])
  82. keyValue := result.Messages[0].MessageAttributes["key"]
  83. key = *keyValue.StringValue
  84. text := *result.Messages[0].Body
  85. message = &filer_pb.EventNotification{}
  86. err = proto.UnmarshalText(text, message)
  87. // delete the message
  88. _, err = k.svc.DeleteMessage(&sqs.DeleteMessageInput{
  89. QueueUrl: &k.queueUrl,
  90. ReceiptHandle: result.Messages[0].ReceiptHandle,
  91. })
  92. if err != nil {
  93. glog.V(1).Infof("delete message from sqs %s: %v", k.queueUrl, err)
  94. }
  95. return
  96. }