notification_gocdk_pub_sub.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package sub
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/util/log"
  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. log.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, err error) {
  35. msg, err := k.sub.Receive(context.Background())
  36. key = msg.Metadata["key"]
  37. message = &filer_pb.EventNotification{}
  38. err = proto.Unmarshal(msg.Body, message)
  39. if err != nil {
  40. return "", nil, err
  41. }
  42. return key, message, nil
  43. }