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.

71 lines
2.1 KiB

  1. // Package gocdk_pub_sub supports the Go CDK (Cloud Development Kit) PubSub API,
  2. // which in turn supports many providers, including Amazon SNS/SQS, Azure Service Bus,
  3. // Google Cloud PubSub, and RabbitMQ.
  4. //
  5. // In the config, select a provider and topic using a URL. See
  6. // https://godoc.org/gocloud.dev/pubsub and its sub-packages for details.
  7. //
  8. // The Go CDK PubSub API does not support administrative operations like topic
  9. // creation. Create the topic using a UI, CLI or provider-specific API before running
  10. // weed.
  11. //
  12. // The Go CDK obtains credentials via environment variables and other
  13. // provider-specific default mechanisms. See the provider's documentation for
  14. // details.
  15. package gocdk_pub_sub
  16. import (
  17. "context"
  18. "fmt"
  19. "github.com/golang/protobuf/proto"
  20. "gocloud.dev/pubsub"
  21. _ "gocloud.dev/pubsub/awssnssqs"
  22. "github.com/chrislusf/seaweedfs/weed/glog"
  23. "github.com/chrislusf/seaweedfs/weed/notification"
  24. "github.com/chrislusf/seaweedfs/weed/util"
  25. // _ "gocloud.dev/pubsub/azuresb"
  26. _ "gocloud.dev/pubsub/gcppubsub"
  27. _ "gocloud.dev/pubsub/natspubsub"
  28. _ "gocloud.dev/pubsub/rabbitpubsub"
  29. )
  30. func init() {
  31. notification.MessageQueues = append(notification.MessageQueues, &GoCDKPubSub{})
  32. }
  33. type GoCDKPubSub struct {
  34. topicURL string
  35. topic *pubsub.Topic
  36. }
  37. func (k *GoCDKPubSub) GetName() string {
  38. return "gocdk_pub_sub"
  39. }
  40. func (k *GoCDKPubSub) Initialize(configuration util.Configuration, prefix string) error {
  41. k.topicURL = configuration.GetString(prefix + "topic_url")
  42. glog.V(0).Infof("notification.gocdk_pub_sub.topic_url: %v", k.topicURL)
  43. topic, err := pubsub.OpenTopic(context.Background(), k.topicURL)
  44. if err != nil {
  45. glog.Fatalf("Failed to open topic: %v", err)
  46. }
  47. k.topic = topic
  48. return nil
  49. }
  50. func (k *GoCDKPubSub) SendMessage(key string, message proto.Message) error {
  51. bytes, err := proto.Marshal(message)
  52. if err != nil {
  53. return err
  54. }
  55. err = k.topic.Send(context.Background(), &pubsub.Message{
  56. Body: bytes,
  57. Metadata: map[string]string{"key": key},
  58. })
  59. if err != nil {
  60. return fmt.Errorf("send message via Go CDK pubsub %s: %v", k.topicURL, err)
  61. }
  62. return nil
  63. }