notification_aws_sqs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // fmt.Printf("messages: %+v\n", result.Messages[0])
  81. keyValue := result.Messages[0].MessageAttributes["key"]
  82. key = *keyValue.StringValue
  83. text := *result.Messages[0].Body
  84. message = &filer_pb.EventNotification{}
  85. err = proto.UnmarshalText(text, message)
  86. // delete the message
  87. _, err = k.svc.DeleteMessage(&sqs.DeleteMessageInput{
  88. QueueUrl: &k.queueUrl,
  89. ReceiptHandle: result.Messages[0].ReceiptHandle,
  90. })
  91. if err != nil {
  92. glog.V(1).Infof("delete message from sqs %s: %v", k.queueUrl, err)
  93. }
  94. return
  95. }