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.

145 lines
3.7 KiB

6 years ago
4 years ago
  1. package sub
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/streadway/amqp"
  9. "gocloud.dev/pubsub"
  10. _ "gocloud.dev/pubsub/awssnssqs"
  11. "net/url"
  12. "path"
  13. "strings"
  14. // _ "gocloud.dev/pubsub/azuresb"
  15. _ "gocloud.dev/pubsub/gcppubsub"
  16. _ "gocloud.dev/pubsub/natspubsub"
  17. _ "gocloud.dev/pubsub/rabbitpubsub"
  18. )
  19. func init() {
  20. NotificationInputs = append(NotificationInputs, &GoCDKPubSubInput{})
  21. }
  22. func getPath(rawUrl string) string {
  23. parsedUrl, _ := url.Parse(rawUrl)
  24. return path.Join(parsedUrl.Host, parsedUrl.Path)
  25. }
  26. func QueueDeclareAndBind(conn *amqp.Connection, exchangeUrl string, queueUrl string) error {
  27. exchangeName := getPath(exchangeUrl)
  28. queueName := getPath(queueUrl)
  29. exchangeNameDLX := "DLX." + exchangeName
  30. queueNameDLX := "DLX." + queueName
  31. ch, err := conn.Channel()
  32. if err != nil {
  33. glog.Error(err)
  34. return err
  35. }
  36. defer ch.Close()
  37. if err := ch.ExchangeDeclare(
  38. exchangeNameDLX, "fanout", false, false, false, false, nil); err != nil {
  39. glog.Error(err)
  40. return err
  41. }
  42. if err := ch.ExchangeDeclare(
  43. exchangeName, "fanout", false, false, false, false, nil); err != nil {
  44. glog.Error(err)
  45. return err
  46. }
  47. if _, err := ch.QueueDeclare(
  48. queueName, false, false, false, false,
  49. amqp.Table{"x-dead-letter-exchange": exchangeNameDLX}); err != nil {
  50. glog.Error(err)
  51. return err
  52. }
  53. if err := ch.QueueBind(queueName, "", exchangeName, false, nil); err != nil {
  54. glog.Error(err)
  55. return err
  56. }
  57. if _, err := ch.QueueDeclare(
  58. queueNameDLX, false, false, false, false,
  59. amqp.Table{"x-dead-letter-exchange": exchangeName, "x-message-ttl": 600000}); err != nil {
  60. glog.Error(err)
  61. return err
  62. }
  63. if err := ch.QueueBind(queueNameDLX, "", exchangeNameDLX, false, nil); err != nil {
  64. glog.Error(err)
  65. return err
  66. }
  67. return nil
  68. }
  69. type GoCDKPubSubInput struct {
  70. sub *pubsub.Subscription
  71. }
  72. func (k *GoCDKPubSubInput) GetName() string {
  73. return "gocdk_pub_sub"
  74. }
  75. func (k *GoCDKPubSubInput) Initialize(configuration util.Configuration, prefix string) error {
  76. topicUrl := configuration.GetString(prefix + "topic_url")
  77. subURL := configuration.GetString(prefix + "sub_url")
  78. glog.V(0).Infof("notification.gocdk_pub_sub.sub_url: %v", subURL)
  79. sub, err := pubsub.OpenSubscription(context.Background(), subURL)
  80. if err != nil {
  81. return err
  82. }
  83. var conn *amqp.Connection
  84. if sub.As(&conn) {
  85. ch, err := conn.Channel()
  86. if err != nil {
  87. return err
  88. }
  89. defer ch.Close()
  90. _, err = ch.QueueInspect(getPath(subURL))
  91. if err != nil {
  92. if strings.HasPrefix(err.Error(), "Exception (404) Reason") {
  93. if err := QueueDeclareAndBind(conn, topicUrl, subURL); err != nil {
  94. return err
  95. }
  96. } else {
  97. return err
  98. }
  99. }
  100. }
  101. k.sub = sub
  102. return nil
  103. }
  104. func (k *GoCDKPubSubInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, onSuccessFn func(), onFailureFn func(), err error) {
  105. msg, err := k.sub.Receive(context.Background())
  106. if err != nil {
  107. return
  108. }
  109. onFailureFn = func() {
  110. if msg.Nackable() {
  111. isRedelivered := false
  112. var delivery amqp.Delivery
  113. if msg.As(&delivery) {
  114. isRedelivered = delivery.Redelivered
  115. glog.Warningf("onFailureFn() metadata: %+v, redelivered: %v", msg.Metadata, delivery.Redelivered)
  116. }
  117. if isRedelivered {
  118. if err := delivery.Nack(false, false); err != nil {
  119. glog.Error(err)
  120. }
  121. } else {
  122. msg.Nack()
  123. }
  124. }
  125. }
  126. onSuccessFn = func() {
  127. msg.Ack()
  128. }
  129. key = msg.Metadata["key"]
  130. message = &filer_pb.EventNotification{}
  131. err = proto.Unmarshal(msg.Body, message)
  132. if err != nil {
  133. return "", nil, onSuccessFn, onFailureFn, err
  134. }
  135. return key, message, onSuccessFn, onFailureFn, nil
  136. }