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.

61 lines
1.5 KiB

6 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. "gocloud.dev/pubsub"
  9. _ "gocloud.dev/pubsub/awssnssqs"
  10. // _ "gocloud.dev/pubsub/azuresb"
  11. _ "gocloud.dev/pubsub/gcppubsub"
  12. _ "gocloud.dev/pubsub/natspubsub"
  13. _ "gocloud.dev/pubsub/rabbitpubsub"
  14. )
  15. func init() {
  16. NotificationInputs = append(NotificationInputs, &GoCDKPubSubInput{})
  17. }
  18. type GoCDKPubSubInput struct {
  19. sub *pubsub.Subscription
  20. }
  21. func (k *GoCDKPubSubInput) GetName() string {
  22. return "gocdk_pub_sub"
  23. }
  24. func (k *GoCDKPubSubInput) Initialize(configuration util.Configuration, prefix string) error {
  25. subURL := configuration.GetString(prefix + "sub_url")
  26. glog.V(0).Infof("notification.gocdk_pub_sub.sub_url: %v", subURL)
  27. sub, err := pubsub.OpenSubscription(context.Background(), subURL)
  28. if err != nil {
  29. return err
  30. }
  31. k.sub = sub
  32. return nil
  33. }
  34. func (k *GoCDKPubSubInput) ReceiveMessage() (key string, message *filer_pb.EventNotification, onSuccessFn func(), onFailureFn func(), err error) {
  35. msg, err := k.sub.Receive(context.Background())
  36. if err != nil {
  37. return
  38. }
  39. onSuccessFn = func() {
  40. msg.Ack()
  41. }
  42. onFailureFn = func() {
  43. if msg.Nackable() {
  44. msg.Nack()
  45. }
  46. }
  47. key = msg.Metadata["key"]
  48. message = &filer_pb.EventNotification{}
  49. err = proto.Unmarshal(msg.Body, message)
  50. if err != nil {
  51. return "", nil, onSuccessFn, onFailureFn, err
  52. }
  53. return key, message, onSuccessFn, onFailureFn, nil
  54. }